Override Vagrant configuration settings locally (per-dev)

前端 未结 8 1530
说谎
说谎 2021-01-30 02:51

I\'d like the question to be answered in general, but to illustrate it, here\'s a use case:

I\'m using Vagrant for a simple LMAP project. I use standalone Puppet for pro

8条回答
  •  渐次进展
    2021-01-30 03:53

    Consider using vagrant-proxyconf plugin. It allows to set proxy for all Vagrant VMs globally.

    Another solution is to run external shell script during provisioning. I use separate config.vm.provision section at the beginning of Vagrantfile to do it:

    # reset: true below is needed to reset the connection to the VM so that new
    # environment variables set in /etc/environment will be picked up in next
    # provisioning steps
    config.vm.provision "shell", reset: true, inline: <<-SHELL
    
      if [ -f /vagrant/Vagrantfile-settings.sh ]
      then
        /vagrant/Vagrantfile-settings.sh
      fi
    SHELL
    

    Then just put a Vagrantfile-settings.sh file next to Vagrantfile, add it to .gitignore (or whatever) and put any script inside, for example to set proxy for interactive terminal, all daemons and docker containers:

    # Proxy for interactive terminals
    echo "http_proxy=http://PROXY_ADDRESS:PROXY_PORT" >> /etc/environment
    echo "https_proxy=http://PROXY_ADDRESS:PROXY_PORT" >> /etc/environment
    echo "no_proxy=127.0.0.1,localhost" >> /etc/environment
    # Proxy for daemons (e.g. Docker deamon - used to pull images, apt - run from default daily cron job)
    mkdir /etc/systemd/system.conf.d
    echo [Manager] > /etc/systemd/system.conf.d/01-http-proxy.conf
    echo "DefaultEnvironment=\"http_proxy=PROXY_ADDRESS:PROXY_PORT\"" >> /etc/systemd/system.conf.d/01-http-proxy.conf
    echo "DefaultEnvironment=\"https_proxy=PROXY_ADDRESS:PROXY_PORT\"" >> /etc/systemd/system.conf.d/01-http-proxy.conf
    echo "DefaultEnvironment=\"no_proxy=127.0.0.1,localhost\"" >> /etc/systemd/system.conf.d/01-http-proxy.conf
    echo "# Docker requires upper-case http proxy environment variables..." >> /etc/systemd/system.conf.d/01-http-proxy.conf
    echo "DefaultEnvironment=\"HTTP_PROXY=http://PROXY_ADDRESS:PROXY_PORT2\"" >> /etc/systemd/system.conf.d/01-http-proxy.conf
    echo "DefaultEnvironment=\"HTTPS_PROXY=http://PROXY_ADDRESS:PROXY_PORT\"" >> /etc/systemd/system.conf.d/01-http-proxy.conf
    echo "DefaultEnvironment=\"NO_PROXY=127.0.0.1,localhost\"" >> /etc/systemd/system.conf.d/01-http-proxy.conf
    # Proxy for docker containers started with `docker run`
    mkdir /home/vagrant/.docker
    cat < /home/vagrant/.docker/config.json
    {
      "proxies": {
        "default": {
          "httpProxy": "http:/PROXY_ADDRESS:PROXY_PORT",
          "httpsProxy": "http://PROXY_ADDRESS:PROXY_PORT",
          "noProxy": "127.0.0.1,localhost"
        }
      }
    }
    EOF
    chown -R vagrant:vagrant /home/vagrant/.docker
    

提交回复
热议问题