2

At work I am behind a proxy server and have configured vagrant to use it through the vagrant-proxyconf plugin. It works great and no problems or complaints there.

Vagrant.configure(2) do |config|
  if Vagrant.has_plugin?("vagrant-proxyconf")
    config.proxy.http      = "http://proxy.server.com:8080"
    config.proxy.https     = "http://proxy.server.com:8080"
    config.proxy.no_proxy  = "localhost, 127.0.0.1"
  else
    raise Vagrant::Errors::VagrantError.new, "Plugin missing: vagrant-proxyconf"
  end

The problem that I'm having is when I take my computer home to do some work. Is there a way to easily turn off the proxy settings?

Dan
  • 119
  • 7

1 Answers1

1

You can turn off proxy by adding

config.proxy.enabled = false 

to your Vagrantfile but you need to edit the file to make the change (true/false flag). you can also use external config file if you already have but it still requires a file edit

what I would try based on this answer is something like

vagrant true/false up

and in your Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

proxy_val = ARGV[0]

Vagrant.configure(2) do |config|
  if Vagrant.has_plugin?("vagrant-proxyconf")
    config.proxy.enabled   = proxy_val
    config.proxy.http      = "http://proxy.server.com:8080"
    config.proxy.https     = "http://proxy.server.com:8080"
    config.proxy.no_proxy  = "localhost, 127.0.0.1"
  else
    raise Vagrant::Errors::VagrantError.new, "Plugin missing: vagrant-proxyconf"
  end

If you have some ruby skills you can even come up with something nicer but this gives you an idea

Note turns out even if proxy is disabled, the proxy value are still set as mentioned from the doc

This disabling keeps proxy configurations for applications on the guest. The configurations must be cleared before disabling if needed.

so another possibility using the above proposal is to do something like

# -*- mode: ruby -*-
# vi: set ft=ruby :

proxy_val = ARGV[0]

Vagrant.configure(2) do |config|
  if Vagrant.has_plugin?("vagrant-proxyconf")
    config.proxy.enabled   = proxy_val
    if (proxy_val) 
      config.proxy.http      = "http://proxy.server.com:8080"
      config.proxy.https     = "http://proxy.server.com:8080"
      config.proxy.no_proxy  = "localhost, 127.0.0.1"
    else
      config.proxy.http      = ""
      config.proxy.https     = ""
      config.proxy.no_proxy  = ""
  else
    raise Vagrant::Errors::VagrantError.new, "Plugin missing: vagrant-proxyconf"
  end
Community
  • 1
  • 1
Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • Thanks for the answer, I'll try it in a bit. I have one other question for you if you have the time. If I configured the network in the vagrant file to be a public network, shouldn't that piggyback off of my host machine's connection? (config.vm.network "public_network") I tried this and it doesn't work so I think I might be misunderstanding something. – Dan Nov 18 '15 at 17:52
  • I just tried your suggestion and found that it's half way there. proxy.enable enables/disables the plugin, but it doesn't overwrite the proxy values that it sets. What I had to do instead was pass an argument into the shell script provisioner like you suggested, and then based on if that was true of false, set the proxies (config.proxy.http="http://proxy.server.com:8080") or unset them (config.proxy.http = ""). If you edit your answer you'll mark it accepted. – Dan Nov 18 '15 at 21:49
  • Thanks Dan, I edited accordingly - back to the additional question, I am not quite sure to understand what you mean - you should be able to access your VM from any machine on the same network, there are already questions on SO on this topic if thats what you mean – Frederic Henri Nov 18 '15 at 22:12