Provisioning Vagrant multi-machines with Ansible

Gika Megawan Pramudita
4 min readJan 10, 2018

--

Hello all. In this tutorial, we will learn how to create Vagrant box and connect it with ansible.

What is Vagrant? Vagrant is an open source tool for configuring and managing virtualized development environments via command-line. Vagrant has Vagrantfile who can define multiple machines in just one file.

before we get started, we need some requirements:
Virtualbox (click here)
Vagrant (click here)

First, create a directory that will be used in the next article.

$ mkdir testinglab$ cd testinglab$ vagrant init centos/7

This will be initialize the testinglab directory to be a Vagrant environment by creating an initial Vagrantfile and download the box named “centos/7” from box catalog. Click here to discover and find more information about the catalog.

We will create multi-machines by configure Vagrantfile.

$ vi VagrantfileVagrant.configure("2") do |config|config.vm.define "master" do |subconfig|subconfig.vm.box = "centos/7"subconfig.vm.hostname = "master"subconfig.vm.network :private_network, ip: "10.0.0.10"endconfig.vm.define "slave" do |subconfig|subconfig.vm.box = "centos/7"subconfig.vm.hostname = "slave"subconfig.vm.network :private_network, ip: "10.0.0.11"endend

start the machines

$ vagrant up

If all went well, the output should look something like this:

Bringing machine 'master' up with 'virtualbox' provider...Bringing machine 'slave' up with 'virtualbox' provider...==> master: Importing base box 'centos/7'...==> master: Matching MAC address for NAT networking...==> master: Checking if box 'centos/7' is up to date...==> master: Setting the name of the VM: centos_master_1491893684616_35913==> master: Clearing any previously set network interfaces...==> master: Preparing network interfaces based on configuration...master: Adapter 1: natmaster: Adapter 2: hostonly==> master: Forwarding ports...master: 22 (guest) => 2222 (host) (adapter 1)==> master: Booting VM...==> master: Waiting for machine to boot. This may take a few minutes...master: SSH address: 127.0.0.1:2222master: SSH username: vagrantmaster: SSH auth method: private keymaster: Vagrant insecure key detected. Vagrant will automatically replacemaster: this with a newly generated keypair for better security.master:master: Inserting generated public key within guest...master: Removing insecure key from the guest if it's present...master: Key inserted! Disconnecting and reconnecting using new SSH key...==> master: Machine booted and ready!==> master: Checking for guest additions in VM...master:master: This is not an error message; everything may continue to work properly,master: in which case you may ignore this message.==> master: Setting hostname...==> master: Configuring and enabling network interfaces...==> master: Rsyncing folder: /Users/geek/Documents/testinglab/centos/ => /vagrant==> slave: Importing base box 'centos/7'...==> slave: Matching MAC address for NAT networking...==> slave: Checking if box 'centos/7' is up to date...==> slave: Setting the name of the VM: centos_slave_1491893801196_64191==> slave: Fixed port collision for 22 => 2222. Now on port 2200.==> slave: Clearing any previously set network interfaces...==> slave: Preparing network interfaces based on configuration...slave: Adapter 1: natslave: Adapter 2: hostonly==> slave: Forwarding ports...slave: 22 (guest) => 2200 (host) (adapter 1)==> slave: Booting VM...==> slave: Waiting for machine to boot. This may take a few minutes...slave: SSH address: 127.0.0.1:2200slave: SSH username: vagrantslave: SSH auth method: private keyslave: Vagrant insecure key detected. Vagrant will automatically replaceslave: this with a newly generated keypair for better security.slave:slave: Inserting generated public key within guest...slave: Removing insecure key from the guest if it's present...slave: Key inserted! Disconnecting and reconnecting using new SSH key...==> slave: Machine booted and ready!==> slave: Checking for guest additions in VM...slave:slave: This is not an error message; everything may continue to work properly,slave: in which case you may ignore this message.==> slave: Setting hostname...==> slave: Configuring and enabling network interfaces...==> slave: Rsyncing folder: /Users/geek/Documents/testinglab/centos/ => /vagrant

We already have two virtual machines named master and slave.

check all the machines status.

$ vagrant statusCurrent machine states:master                    running (virtualbox)slave                     running (virtualbox)

get information of ports, users, and identityfile by running:

$ vagrant ssh-config
Host masterHostName 127.0.0.1User vagrantPort 2222UserKnownHostsFile /dev/nullStrictHostKeyChecking noPasswordAuthentication noIdentityFile /Users/geek/Documents/testinglab/centos/.vagrant/machines/master/virtualbox/private_keyIdentitiesOnly yesLogLevel FATALHost slaveHostName 127.0.0.1User vagrantPort 2200UserKnownHostsFile /dev/nullStrictHostKeyChecking noPasswordAuthentication noIdentityFile /Users/geek/Documents/testinglab/centos/.vagrant/machines/slave/virtualbox/private_keyIdentitiesOnly yesLogLevel FATAL

As you can see master has port 2222, and slave has port 2200. Next step is modify ansible hosts file so it looks like this:

$ vi /etc/ansible/hosts

[testinglab]master ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_user=vagrant ansible_ssh_private_key_file=/Users/geek/Documents/testinglab/centos/.vagrant/machines/master/virtualbox/private_keyslave ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200 ansible_ssh_user=vagrant ansible_ssh_private_key_file=/Users/geek/Documents/testinglab/centos/.vagrant/machines/slave/virtualbox/private_key

Make sure that you can access these machines by using specify group

$ ansible -m ping testinglab

master | SUCCESS => {"changed": false,"ping": "pong"}slave | SUCCESS => {"changed": false,"ping": "pong"}

--

--

Gika Megawan Pramudita

SRE and DevOps practitioner who enjoys writing about tech and occasionally sharing bits of my life.