Override Vagrant configuration settings locally (per-dev)

前端 未结 8 1255
说谎
说谎 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:50

    Here's an idea. It may be "ugly" and "wrong", but, at least, it works :)

    # file2.rb, this is your per-dev configuration file
    puts "included external file which uses outer var: #{foo}"
    
    # file1.rb, this would be your Vagrantfile
    puts 'first'
    foo = 'bar'
    
    external = File.read 'file2.rb'
    eval external
    puts 'second'
    

    Let's run that

    $ ruby file1.rb
    first
    included external file which uses outer var: bar
    second
    

    Adapting to your example, file2.rb would contain only usage of config without defining it (config will be provided from outer context)

      config.vm.provision :puppet do |puppet|
        puppet.facter = { "proxy" => "proxy.host:80" }
      end
    

    And your Vagrant file may look like this:

    Vagrant::Config.run do |config|
      external = File.read 'Vagrantfile.local'
      eval external
    
      # proceed with general settings here
      config.vm.provision :puppet do |puppet|
        puppet.facter = { "proxy" => "proxy.host:80" }
      end
    end
    

    Update (another, "data-driven" approach)

    # Vagranfile.local
    config_values[:puppet][:facter][:proxy] = 'proxy.host:80'
    
    # Vargantfile
    Vagrant::Config.run do |config|
      config_values = {
        puppet: {
          facter: {
            proxy: nil
          },
          manifests_file: 'my_manifest.pp'
    
        }
      }
      external = File.read 'Vagrantfile.local'
      eval external # this should overwrite proxy config
    
      # proceed with general settings here
      config.vm.provision :puppet do |puppet|
        if config_values[:puppet][:facter][:proxy]
          puppet.facter = { "proxy" => config_values[:puppet][:facter][:proxy] } 
        end
    
        puppet.manifests_file = config_values[:puppet][:manifests_file]
      end
    end
    
    0 讨论(0)
  • 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 <<EOF > /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
    
    0 讨论(0)
提交回复
热议问题