Multi-machine Vagrantfile for VirtualBox or VMware Fusion

Thursday, September 19, 2013

The following Vagrantfile shows the syntax for setting up multiple virtual machines using VirtualBox or VMware Fusion.

In addition, the example shows the syntax for globally turning off shared folders, setting each virtual machine’s hostname, CPU, memory, and an adding a private NIC on the 192.168.205.0/24 network.

I have also put together a multi-machine Vagrantfile with shorter, cleaner syntax using JSON and loops.

Example Vagrantfile

# -*- mode: ruby -*-

# vi: set ft=ruby :
 
VAGRANTFILE_API_VERSION = "2"
 
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
 
  config.vm.box = "base"

  config.vm.provider "vmware_fusion" do |v, override|
    override.vm.box = "base"
  end
 
  # Turn off shared folders
  config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true
 
  # Begin server1
  config.vm.define "server1" do |server1|
    server1.vm.hostname = "server1"

    server1.vm.provider "vmware_fusion" do |v|
        v.vmx["numvcpus"] = "1"
        v.vmx["memsize"] = "1024"
    end
 
    server1.vm.provider "virtualbox" do |v|
        v.customize [ "modifyvm", :id, "--cpus", "1" ]
        v.customize [ "modifyvm", :id, "--memory", "1024" ]
    end

    server1.vm.network "private_network", ip: "192.168.205.10"
  end
  # End server1
 
  # Begin server2
  config.vm.define "server2" do |server2|
    server2.vm.hostname = "server2"
 
    server2.vm.provider "vmware_fusion" do |v|
        v.vmx["numvcpus"] = "2"
        v.vmx["memsize"] = "1024"
    end
 
    server2.vm.provider "virtualbox" do |v|
        v.customize [ "modifyvm", :id, "--cpus", "2" ]
        v.customize [ "modifyvm", :id, "--memory", "1024" ]
    end

    server2.vm.network "private_network", ip: "192.168.205.11"
  end
  # End server2
 
  # Begin server3
  config.vm.define "server3" do |server3|
    server3.vm.hostname = "server3"
 
    server3.vm.provider "vmware_fusion" do |v|
        v.vmx["numvcpus"] = "2"
        v.vmx["memsize"] = "2048"
    end
 
    server3.vm.provider "virtualbox" do |v|
        v.customize [ "modifyvm", :id, "--cpus", "2" ]
        v.customize [ "modifyvm", :id, "--memory", "2048" ]
    end

    server3.vm.network "private_network", ip: "192.168.205.12"
  end
  # End server3
end


comments powered by Disqus