Search code examples
proxyvagrantvagrant-windows

How to use Vagrant-proxyconf in multiple environments


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?


Solution

  • 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