How to run several boxes with Vagrant?

timurb picture timurb · May 27, 2014 · Viewed 48.1k times · Source

I need to run several boxes with Vagrant.

Is there a way to do that?

These don't relate one to another in any way, they can be thought as different environments using for test so it seems that multi-machine setup has nothing to do with this.

Answer

david picture david · Nov 18, 2015

The best way is to use an array of hashes. You can define the array like:

servers=[
  {
    :hostname => "web",
    :ip => "192.168.100.10",
    :box => "saucy",
    :ram => 1024,
    :cpu => 2
  },
  {
    :hostname => "db",
    :ip => "192.168.100.11",
    :box => "saucy",
    :ram => 2048,
    :cpu => 4
  }
]

Then you just iterate each item in server array and define the configs:

Vagrant.configure(2) do |config|
    servers.each do |machine|
        config.vm.define machine[:hostname] do |node|
            node.vm.box = machine[:box]
            node.vm.hostname = machine[:hostname]
            node.vm.network "private_network", ip: machine[:ip]
            node.vm.provider "virtualbox" do |vb|
                vb.customize ["modifyvm", :id, "--memory", machine[:ram]]
            end
        end
    end
end