Launching multiple Vagrant boxes

Before we start to look at the changes we need to make to our Ansible playbook, we should look at how we are going to launch two Vagrant boxes running different operating systems side by side. It is possible to launch two Vagrant boxes from a single Vagrantfile; we will be using the following one:

# -*- mode: ruby -*-
# vi: set ft=ruby :

API_VERSION = "2"
DOMAIN = "nip.io"
PRIVATE_KEY = "~/.ssh/id_rsa"
PUBLIC_KEY = '~/.ssh/id_rsa.pub'
CENTOS_IP = '192.168.50.6'
CENTOS_BOX = 'centos/7'
UBUNTU_IP = '192.168.50.7'
UBUNTU_BOX = 'generic/ubuntu1704'

Vagrant.configure(API_VERSION) do |config|

config.vm.define "centos" do |centos|
centos.vm.box = CENTOS_BOX
centos.vm.network "private_network", ip: CENTOS_IP
centos.vm.host_name = CENTOS_IP + '.' + DOMAIN
centos.ssh.insert_key = false
centos.ssh.private_key_path = [PRIVATE_KEY,
"~/.vagrant.d/insecure_private_key"]
centos.vm.provision "file", source: PUBLIC_KEY, destination:
"~/.ssh/authorized_keys"

centos.vm.provider "virtualbox" do |v|
v.memory = "2024"
v.cpus = "2"
end

centos.vm.provider "vmware_fusion" do |v|
v.vmx["memsize"] = "2024"
v.vmx["numvcpus"] = "2"
end
end

config.vm.define "ubuntu" do |ubuntu|
ubuntu.vm.box = UBUNTU_BOX
ubuntu.vm.network "private_network", ip: UBUNTU_IP
ubuntu.vm.host_name = UBUNTU_IP + '.' + DOMAIN
ubuntu.ssh.insert_key = false
ubuntu.ssh.private_key_path = [PRIVATE_KEY,
"~/.vagrant.d/insecure_private_key"]
ubuntu.vm.provision "file", source: PUBLIC_KEY, destination:
"~/.ssh/authorized_keys"

ubuntu.vm.provider "virtualbox" do |v|
v.memory = "2024"
v.cpus = "2"
end

ubuntu.vm.provider "vmware_fusion" do |v|
v.vmx["memsize"] = "2024"
v.vmx["numvcpus"] = "2"
end
end

end

As you can see, we are defining two different boxes, one called centos and the other ubuntu, you should replace the Vagrantfile in the lemp folder you copied earlier.

We can launch the two machines using just one command; to use VirtualBox, we should run:

$ vagrant up 

Or to use VMware, we can run:

$ vagrant up --provider=vmware_fusion

As you can see from the Terminal output here, this launched the two boxes:

Bringing machine 'centos' up with 'vmware_fusion' provider...
Bringing machine 'ubuntu' up with 'vmware_fusion' provider...
==> centos: Cloning VMware VM: 'centos/7'. This can take some time...
==> centos: Checking if box 'centos/7' is up to date...
==> centos: Verifying vmnet devices are healthy...
==> centos: Preparing network adapters...
==> centos: Starting the VMware VM...
==> centos: Waiting for the VM to receive an address...
==> centos: Forwarding ports...
centos: -- 22 => 2222
==> centos: Waiting for machine to boot. This may take a few minutes...
centos: SSH address: 127.0.0.1:2222
centos: SSH username: vagrant
centos: SSH auth method: private key
==> centos: Machine booted and ready!
==> centos: Setting hostname...
==> centos: Configuring network adapters within the VM...
centos: SSH address: 127.0.0.1:2222
centos: SSH username: vagrant
centos: SSH auth method: private key
==> centos: Rsyncing folder: /Users/russ/lemp/ => /vagrant
==> centos: Running provisioner: file...
==> ubuntu: Cloning VMware VM: 'generic/ubuntu1704'. This can take some time...
==> ubuntu: Checking if box 'generic/ubuntu1704' is up to date...
==> ubuntu: Verifying vmnet devices are healthy...
==> ubuntu: Preparing network adapters...
==> ubuntu: Starting the VMware VM...
==> ubuntu: Waiting for the VM to receive an address...
==> ubuntu: Forwarding ports...
ubuntu: -- 22 => 2222
==> ubuntu: Waiting for machine to boot. This may take a few minutes...
ubuntu: SSH address: 127.0.0.1:2222
ubuntu: SSH username: vagrant
ubuntu: SSH auth method: private key
==> ubuntu: Machine booted and ready!
==> ubuntu: Setting hostname...
==> ubuntu: Configuring network adapters within the VM...
==> ubuntu: Running provisioner: file...

Once the boxes are up and running, you can SSH to them using the machine name:

$ vagrant ssh centos
$ vagrant ssh ubuntu

Now that we have our two boxes running on two different operating systems, we can discuss the changes we need to make to our playbook. First of all, let's look at how the changes to the Vagrantfile will affect our host inventory file, as you can see from this file:

centos ansible_host=192.168.50.6.nip.io 
ubuntu ansible_host=192.168.50.7.nip.io

[wordpress]
centos
ubuntu

[wordpress:vars]
ansible_connection=ssh
ansible_user=vagrant
ansible_private_key_file=~/.ssh/id_rsa
host_key_checking=False

We now have two hosts, one called centos and the other ubuntu, and we are putting them in a group called wordpress where we are setting some common variables. You should update your production file, as we will be using it in the next section.