Vagrant – Defining Multiple Virtual Machines


Previous post we checked the details of Vagrantfile. So far all of the settings were for single virtual machine. Now we will change the configuration file so that we can define multiple machines.

Now lets add another box to Vagrant. I am adding Ubuntu 14.04 box. In the link https://vagrantcloud.com/ubuntu/boxes/trusty64 you can see that we can use the command below for a box for Ubuntu 14.04.

$ vagrant box add ubuntu/trusty64

After adding another box lets check the list of boxes. As you can see below we have two boxes. You can use “vagrant box list” to list available boxes.

$ vagrant box list
precise64       (virtualbox, 0)
ubuntu/trusty64 (virtualbox, 14.04)

Below is our old config. I will change it so we can define multiple machines.

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "precise64"
  config.vm.synced_folder = "./test", "/testfolder"
  config.vm.hostname = "ubuntutest"
end

Now the new format again with extra machine.

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
   config.vm.define "box1" do |box1|
      box1.vm.box = "precise64"
      box1.vm.synced_folder "./test", "/testfolder"
      box1.vm.hostname = "ubuntutest"
   end
   config.vm.define "box2" do |box2|
      box2.vm.box = "ubuntu/trusty64"
   end
end

As you can see above we just define new configuration blocks for each machine using “config.vm.define“. After that the definition of each machine is almost the same.

Now run “vagrant up” and see that you will get two machines running. Below checking with “vagrant status” shows the two machines are really running.

$ vagrant status
Current machine states:
box1                      running (virtualbox)
box2                      running (virtualbox)

We have two machines now and “vagrant ssh” will not work; because it will not know which machine to connect. Now you can use “vagrant ssh boxname“. Below is an example:

$ vagrant ssh box2
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-36-generic x86_64)
 * Documentation:  https://help.ubuntu.com/
  System information as of Mon Oct  6 19:52:35 UTC 2014
  System load:  0.7               Processes:           78
  Usage of /:   2.7% of 39.34GB   Users logged in:     0
  Memory usage: 24%               IP address for eth0: 10.0.2.15
  Swap usage:   0%
  Graph this data and manage this system at:
    https://landscape.canonical.com/
  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud
0 packages can be updated.
0 updates are security updates.[email protected]:~$

We are done with “Vagrant – Defining Multiple Virtual Machines”.
Next topic “Vagrant Networking