Provisioning Ansible on Ubuntu with Vagrant and Virtual box

RMAG news

Introduction

Automation tools play a crucial role in day-to-day software development jobs. And it is on that note that Ansible has proven to be one of the most used technologies in today’s software development world.

Ansible is an open-source software development tool that enables infrastructure as code (IaC). Ansible helps in:

Software provisioning – Ansible can prepare your servers with the necessary software and settings.
Configuration management – Ansible can manage system configurations to ensure your systems and servers are in the desired state
Application deployment – Ansible can automate the process of deploying applications to servers.

Ansible is agentless. It relies on temporary remote connections via SSH. Ansible uses a human-readable script called Playbook to automate tasks and manage and maintain system configurations.

Vagrant on the other hand is an open-source software that provides a convenient way to manage virtual environments making it easier to replicate virtual environments across different hosts.

Meanwhile, VirtualBox is a free open-source virtualization platform. Together with Vagrant, they provide a seamless solution for creating and maintaining virtual machines.
Objective

This article aims to guide us through the process of setting up a local environment with Ansible on Ubuntu with Vagrant and VirtualBox where we will run a simple Ansible playbook on the local machine to print “Hello World”.

Prerequisites

Software requirements

Ubuntu
Vagrant
Virtual Box
Basic Knowledge of Linux commands

Setting up Ubuntu virtual machine with Vagrant and VirtualBox

To begin we will need to download and install Vagrant and VirtualBox

Installing VirtualBox
Virtual Box is a free and open-source technology available for download for different operating systems.
Download VirtualBox: visit the VirtualBox download page to download the appropriate version for your operating system

Install virtual box: Install the virtual box application from the downloaded binary.

Installing Vagrant

Download Vagrant: Download Vagrant from the Vagrant download page

Install Vagrant: Follow the installation guide to install Vagrant for your specific operating system. Vagrant will be automatically added to your system path, so you can start using vagrant commands from your terminal.

Verify Installation: to verify that Vagrant was installed correctly execute the command below to see if it prints out the installed version.
vagrant -version

Creating an Ubuntu Virtual Machine

For the sake of this article, we will be using PowerShell. Run it as an administrator.

We will need to create a folder to store all our vagrant-related files, we will call the file. To do that run the following command.
mkdir ansibleProject

Cd into the directory with
cd ansibleProject

Initialise a vagrant file with an Ubuntu image. This will contain all the necessary configurations needed for our virtual machine. We are going to use the Ubuntu image available in the Vagrant cloud.
vagrant init

Start the virtual machine with the following command
Vagrant up

The above command will download the ubuntu/trusty64 virtual machine image from the vagrant cloud and start the VM.

It also creates a key pair so we can SSH into the machine once it is done setting up.

You can check the status of the virtual machine with the following command vagrant status. It should print out “running (virtualbox)”. we can also check the status by opening VirtualBox to see if our virtual machine is up and running.
SSH into the virtual machine by running the following command vagrant ssh

Installing Ansible

To install Ansible on our virtual machine running on Ubuntu we need to run the following commands.

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository –yes –update ppa:ansible/ansible
sudo apt install ansible

As soon as Ansible is done installing, we will verify if it was installed correctly.

To verify that Anisble was installed correctly run the following command
ansible –version.

Creating a playbook

To create a playbook, we will first create a new directory. This will contain our ansible playbook.
mkdir ansible-project

Create a new file called playbook.yml. This file will contain the task you want to perform on your machine.
Touch playbook.yml
Run ls to see if it was created successfully.

Write your playbook in the playbook.yml file

To do this:

Open the playbook.yml by running vi playbook.yml

Press “I” on your keyboard to enter Insert mode. Copy and paste the following:


– name: Basic playbook
hosts: localhost
tasks:
– name: Print a message
debug:
msg: “Hello, World!”

Hit the “esc” key when done, to exit insert mode.
Type “:wq!” to exit and save the playbook.yml

This is a simple playbook script that tells Ansible to connect to a machine, in this case, localhost which is the machine Ansible is installed on.

Then tells the machine to print “Hello world”

run the playbook by running ansible-playbook playbook.yml

Conclusion

In this simple guide, we’ve seen how to set up a development environment on Ubuntu using Vagrant and VirtualBox, followed by installing Ansible for automation and configuration management.

With Ansible installed we have a powerful tool that can help reduce repetitive tasks and increase productivity.

We can take this further by automating configurations on the cloud with Ansible rather than on our local machines.

As we continue our journey, whether we are managing a small infrastructure or orchestrating a complex deployment Ansible simplicity makes it a great addition in our DevOps toolkit.
Don’t stop learning!

Leave a Reply

Your email address will not be published. Required fields are marked *