Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run several boxes with Vagrant?

Tags:

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.

like image 686
timurb Avatar asked May 27 '14 11:05

timurb


People also ask

How do I run multiple vagrant boxes?

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.

What does vagrant configure 2 mean?

Vagrant.configure("2") do |config| # ... end. The "2" in the first line above represents the version of the configuration object config that will be used for configuration for that block (the section between the do and the end ). This object can be very different from version to version.


2 Answers

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 
like image 101
david Avatar answered Oct 21 '22 15:10

david


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.

like image 22
famousgarkin Avatar answered Oct 21 '22 14:10

famousgarkin