Michael Maclean

Vagrantfile inheritance

I’ve recently been using a lot of Vagrant machines at work, and needed to come up with a way to permit people to customise settings such as the developer’s own DNS zone, the amount of RAM and number of vCPUs assigned to each machine, etc.

In the Vagrantfile reference I noticed that Vagrant will merge several Vagrantfiles together on launch.

  • First, the Vagrantfile packaged in the box
  • The Vagrantfile in ~/.vagrant.d/Vagrantfile
  • The Vagrantfile in the project directory

I knew that each Vagrantfile is just a Ruby script, so I decided to try adding a global variable in ~/.vagrant.d/Vagrantfile to see if it was inherited.

$global_vm_memory = 1024
$global_vm_cpus = 4
$global_vm_tld = '.mgdm'

Then, in the project’s Vagrantfile I added:

config.vm.provider "virtualbox" do |v|
	v.memory = $global_vm_memory ||= 512
	v.cpus = $global_vm_cpus ||= 2
end

That worked fine. I’m pretty sure it’s not the cleanest way, but it certainly is simple.