timurb
timurb

Reputation: 5544

How to run several boxes with Vagrant?

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.

Upvotes: 58

Views: 61579

Answers (7)

Zach Folwick
Zach Folwick

Reputation: 921

I was able to have a single vagrantfile:

Vagrant.configure("2") do |winconfig|
 # stuff
end

Vagrant.configure("2") do |nixconfig|
 # stuff
end

Vagrant.configure("2") do |macconfig|
 # stuff
end

and then I can bring them up with vagrant up --parallel. As others have mentioned, different vagrant files may be better for maintainability.

Upvotes: 5

mehmet
mehmet

Reputation: 8144

Copying the directory holding Vagrantfile to a new place and spinning up the new machine there is the most straight forward way, if machines are not co-operating.

However, you may not want to copy/paste provisioning scripts for VCS tracking/backtracking purposes. Keep all your scripts in a folder, ex. dev, and put your Vagrantfile under numbered folders under dev, ex. dev/10. When you create a newer version, ex. dev/11 and not need the older one, you may just delete it. And refer to the common provisioning scripts using relative path:

config.vm.provision "shell", path: "../provisioner.sh"

Upvotes: 0

030
030

Reputation: 11669

https://www.vagrantup.com/docs/vagrantfile/tips.html#loop-over-vm-definitions

(1..3).each do |i|
  config.vm.define "node-#{i}" do |node|
    node.vm.provision "shell",
      inline: "echo hello from node #{i}"
  end
end

Upvotes: 4

david
david

Reputation: 711

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

Upvotes: 62

deepdive
deepdive

Reputation: 10962

You can even use different Vagrantfile(s) in same directory for different machine configuration or boxes

VAGRANT_VAGRANTFILE=Vagrantfile.ubntu_1404_64 VAGRANT_DOTFILE_PATH=.vagrant_ub140464 vagrant up

OR

VAGRANT_VAGRANTFILE=Vagrantfile.ubntu_1404_32 VAGRANT_DOTFILE_PATH=.vagrant_ub140432 vagrant up

Both the Vagrantfile can reside in same directory

Upvotes: 17

timurb
timurb

Reputation: 5544

You need just to copy the directory holding Vagrantfile to the new place and run vagrant up from it.

Make sure you copy the dir prior to starting up the box for the first time or Vagrant will think that these two locations refer to the same box. Or, if you already did vagrant up before copying the directory, then delete copied_directory/.vagrant after you make the copy.

Upvotes: 23

famousgarkin
famousgarkin

Reputation: 14116

You can definitely run multiple Vagrant boxes concurrently, as long as their configuration does not clash with one another in some breaking way, e.g. mapping the same network ports on the host, or using same box names/IDs inside the same provider. There's no difference from having multiple boxes running on a provider manually, say multiple boxes on VirtualBox, or having them registered and started up by Vagrant. The result is the same, Vagrant just streamlines the process.

You can either use so called multi-machine environment to manage these boxes together in one project/Vagrantfile. They don't necessarily have to be somehow connected, ease of management may be the reason alone, e.g. if you need to start them up at the same time.

Or you can use separate projects/Vagrantfiles and manage the machines from their respective directories, completely separated.

In case of running multiple instances of the same project, you need multiple copies of the project directory, as Vagrant stores the box state in the .vagrant directory under the project.

Upvotes: 43

Related Questions