Override Vagrant configuration settings locally (per-dev)

ruby, vagrant

Solution

I would suggest using environment variables to dynamically change the behavior of the `Vagrantfile` without editing the file itself.

To give a real world example, here's how you could use an Ubuntu base box by default but have an environment variable define an alternative Linux distribution:

if ENV['OPERATINGSYSTEM']
  if ENV['OPERATINGSYSTEM'].downcase == 'redhat'
    os_name = 'centos'
    config.vm.box     = 'centos'
    config.vm.box_url = 'https://dl.dropbox.com/u/7225008/Vagrant/CentOS-6.3-x86_64-minimal.box'
  else
    raise(Exception, "undefined operatingsystem: #{ENV['OPERATINGSYSTEM']}")
  end
else
  os_name = 'precise64'
  config.vm.box     = 'precise64'
  config.vm.box_url = 'http://files.vagrantup.com/precise64.box'
end

This example comes from https://github.com/puppetlabs/puppetlabs-openstack_dev_env

Problem

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 provisioning. Now, there might be some developers who sit behind a proxy and they would need some additional configuration to be made to the VM. I have things working on the Puppet side: I can pass the proxy IP (if any) as a fact to puppet in the `Vagrantfile` and Puppet reacts accordingly if it's set. The only issue I have is: how can developers specify/override this setting for their development environment without having to change the `Vagrantfile` (which is under version control and must remain dev-environment-neutral)? If would be awesome if people could override some Vagrant settings in a file called e.g. `Vagrantfile.local`, which I would exclude via `.gitignore`. Since a Vagrantfile is just Ruby, I tried the following: ``` # Also load per-dev custom vagrant config custom_vagrantfile = 'Vagrantfile.local' load custom_vagrantfile if File.exist?(custom_vagrantfile) ``` The file inclusion basically works, but it looks like in the included file, I'm not in the same Vagrant context anymore... ``` Vagrant::Config.run do |config| config.vm.provision :puppet do |puppet| puppet.facter = { "proxy" => "proxy.host:80" } end end ``` ... also "resets" all other puppet config values I made in the main `Vagrantfile`, which makes me think I'm heading in the wrong direction here. I should note that I'm a total noob at Ruby ;) Can anyone give me a hint or even a working solution for how per-dev customization could be done here in general?

Original source