Easy Ansible setup using VirtualBox

Linu Bajy
4 min readApr 2, 2022

Hi everyone,

In my previous article , we went through the core concepts of Ansible! Now its time to get it in action!

So this article is about setting up 2 servers , I prefer Ubuntu servers and since I’m currently not using one , I decided to write a Vagrantfile , that would help me bring up those servers , so that we can play around with it :)

Vagrantfile is a configuration file, written in Ruby language, that describes about the type and configuration of instances, that will help us to import the virtualbox of those servers.

Prerequisite:

  1. Install Vagrant on you system — https://developer.hashicorp.com/vagrant/downloads?product_intent=vagrant
  2. Install Oracle VirtualBox

Next we write a Vagrantfile .

Here is the sample Vagrantfile that I have used to bring up 2 Ubuntu 18 Virtualboxes — master and worker, as I have described in Vagrantfile.

Note: The Vagrantfile is the file name as should be give as the same , it is case sensitive.

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|

config.vm.define "master" do |master|
master.vm.box = "bento/ubuntu-18.04"
master.vm.hostname = "master"
master.vm.network "private_network", ip: "192.168.33.10"
end
config.vm.define "worker" do |worker|
worker.vm.box = "bento/ubuntu-18.04"
worker.vm.hostname = "worker"
worker.vm.network "private_network", ip: "192.168.33.20"
worker.vm.network "forwarded_port", guest: 80, host: 8093
end


end

In my local command prompt in Windows, to execute the Vagrantfile , we use the command- vagrant up

As you can see the 2 boxes were imported successfully.

Oracle VirtualBox

I intend to keep “master” as the master node where ansible needs to be installed and “worker” as the worker node.

Ansible is only to be installed in the master node and that is one of the pros of ansible as it is based on pull based configuration. The mandatory package for worker node is the python.

Note: Its important to know that the default password for vagrant user is vagrant .

After you have successfully SSHed into the master node, you will notice that the command prompt has changed to vagrant@master . This is very helpful when using multiple terminals to keep track of vagrant boxes .

Next, lets install ansible. But , as soon as I gave the command to install ansible, it was throwing an error as the packages needed update.

So to fix it , run sudo apt-get update and as a good practice , we will be doing it for both boxes.

Now , install ansible into the master node , use the command —

 sudo apt-get install ansible
Successfully installed Ansible

The default configuration file is present in /etc/ansible folder .

Next , we need to check the worker node , if we have the required python package .

After I have run the update command in worker node, I check for the Python version available .

Now , lets get started with a sample ping module to see if the master node can connect with the server nodes.

However, I tried to connect SSH into the server and I could connect to it.

After a bit of google searches, (thanks to askUbuntu!), I came to know that we had to copy the key to the worker server as well to establish connection.

Hence , I copied the key to worker node.

And now, lets check the ping module again. For this , I had created an inventory file that specifies the host IP

Now lets try the ping command again :)

And viola!

Cheers!

Linu

--

--