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?
If you are prepared to define settings that are applied to all your vagrant boxes it's worth noting that, "Vagrant actually loads a series of Vagrantfiles, merging the settings as it goes." (ref https://docs.vagrantup.com/v2/vagrantfile/)
So I have the following defined in
~/.vagrant.d/Vagrantfile
to increase the amount of RAM for my Vagrant boxes:The
Vagrantfile
is just Ruby, so YAML is another option.For example, in the
Vagrantfile
I do this:Then I have a
vagrant.yml
file (I just made up the name; you can use whatever name you like) for the developer-specific configuration:I believe that's the exact use case that Nugrant plugin was created to solve. It allows each of your devs to have a
.vagrantuser
(which is a .gitignore-ed file) in YAML specifying custom configuration values then reference these values with ease inVagrantfile
.In your case, a proxied developer would have their
.vagrantuser
file looking like this:And your
Vagrantfile
would look like this (pseudo code, I don't really know ruby):You should bundle a sample/reference vagrantuser (i.e.
vagrantuser.example
) file for your devs to copy and adjust to their environment.Here's an idea. It may be "ugly" and "wrong", but, at least, it works :)
Let's run that
Adapting to your example, file2.rb would contain only usage of
config
without defining it (config
will be provided from outer context)And your Vagrant file may look like this:
Update (another, "data-driven" approach)
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 ofVagrantfile
to do it:Then just put a
Vagrantfile-settings.sh
file next toVagrantfile
, add it to.gitignore
(or whatever) and put any script inside, for example to set proxy for interactive terminal, all daemons and docker containers: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:
This example comes from https://github.com/puppetlabs/puppetlabs-openstack_dev_env