Getting Started With Ansible
By: Raktim Midya
Tags: DevOps, Linux, Basics, Automation, Ansible

Created by Raktim

Getting Started With Ansible

Let's learn basics of Ansible along with one practical — docker environment provisioning using Ansible.

This article will help you to understand the basics of Most demanding Automation Tool — “Ansible”, along with setting up the environment to work on Ansible. Also I will show one practical where we gonna setup Docker environment and also we will configure webserver on Docker Container only by using Ansible. So, Let's jump in…

What is Ansible :

Ansible logo

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015. It includes its own declarative language to describe system configuration. Ansible is agentless , temporarily connecting remotely via SSH or Windows Remote Management to do its tasks.

It includes its own declarative language to describe system configuration. , temporarily connecting remotely via SSH or Windows Remote Management to do its tasks. To know more about Ansible : https://www.ansible.com/overview/it-automation

Installation of Ansible on RHEL8 :

Frankly speaking I don't want to waste your time by telling more about what is Ansible. Rather than learning theory, let's jump into some practical and you will automatically see the power of Ansible. Now we gonna Install and Setup Ansible on our local machine…

Pre-requisite :

Here I am using RedHat Enterprise Linux (version 8) or RHEL8 . My base OS is Windows 10 and on top of Windows I installed Oracle VirtualBox where I am running my RHEL8 VM . To do this practical we need two VMs.

. My base OS is Windows 10 and where I am running my . Next you definitely need basic knowledge of Linux and how to work on Linux Terminal. Lastly your both VMs should connected via bridge adaptor because I want to do networking between both VMs and also I want both VM to reach to Internet.

Basic Architecture of Distributed Ansible Cluster :

Before installing software we need to understand why and how we use Ansible.

Normally in industry we have thousands of servers and manually configuring those servers is a very haptic and time consuming task.

Here the play of Ansible come. We can configure any kind of server using Ansible in just a few minutes and also We can configure thousands of servers in parallel using Ansible.

Let's understand the below shown architecture of Ansible…

Ansible distributed architecture

As you can noticed that We have “Ansible Management Node” and we have some “Host” in the diagram. You can think this Ansible Management Node is our Master Node and Host node is worker node. That means in worker node, we are configuring our servers.

and we have some in the diagram. That means in worker node, we are configuring our servers. I already told that Ansible can configure any kind of servers and also capable of doing it to thousands of servers in parallel . So, in the diagram we can see Ansible is using SSH protocol to reach every server at a same time.

. So, in the diagram we can see In Ansible rather than using Master, we use the term “Controller Node” and for worker we use the term “Managed Node”. Also after installing Ansible, I will talk about “inventory” and “playbook”, so don't worry about those things.

Installing Ansible software on RHEL8 :

Created by Raktim

Installation of Ansible on RHEL8 is a very easy task. We just need to run only one single command. But before running that command one simple note…

Ansible is written using Python language and by default in RHEL8, python 3 is installed, but in your system if it's not present then install python at first. Next run the below mentioned command and Ansible will be installed on your RHEL8 system.

pip3 install ansible

Setting up Hosts/Inventory :

Next we going to setup the hosts or managed nodes details in our Master Node. In Ansible rather than using hosts, we use the term “Inventory”.

Remember one thing that in Ansible, whatever we do, we always use Controller Node. We never go to any Managed Node. But Ansible will configure our managed nodes to works as servers. So, Ansible need the IP of those managed nodes.

That's why we need to create one file inside our controller system where we gonna mention the IP address of those managed nodes. To make things simple here I am using one managed node. But once you learn more, you will be able to work with multiple managed nodes and will be able to group them.

Run these below mentioned commands and it will create your hosts file.

mkdir /etc/ansible cat > /etc/ansible/hosts 192.168.99.104 ansible_user=root ansible_password=redhat

Here at first we created one folder called “/etc/ansible” because by default Ansible use this folder for storing configuration files, which I will show soon.

which I will show soon. Next as you might have previously noticed that Ansible use SSH protocol to execute any thing on Managed Node so in “hosts” file I mentioned the IP of the managed node and then the username (root) and password (redhat) of my managed node. To make things simple here I directly used “ansible_user” and “ansible_password” keywords.

To make things simple here I directly used “ansible_user” and “ansible_password” keywords. Here I used “cat” command to create the file. You can use VI or gedit editor also.

To know more about Ansible Inventory : https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html

Setting up Configuration file :

We have successfully setup our Hosts file, but how ansible come to know where is our inventory details. That's why we need to setup the configuration file for Ansible and need to mention which is our inventory file.

For that we need to create one file called “ansible.cfg” inside “/etc/ansible” folder. Here also I am using “cat” command but you can use vi or gedit editors also. Run this below command to create the “ansible.cfg” file…

cat > /etc/ansible/ansible.cfg [defaults]

inventory = /etc/ansible/hosts

host_key_checking = False

As you can notice that I write the inventory location the same file that we created just few minutes ago.

Next “host_key_checking” is false because if you know SSH then when we do SSH to any system we need to write “yes” right after we do ssh to connect to the remote system. In simple terms you can thing this is host key checking.

Now as Ansible is an automation tool and while doing SSH, there is no physical person to write “yes” so we use “host_key_checking” false.

To know more about Ansible Configuration : https://docs.ansible.com/ansible/latest/installation_guide/intro_configuration.html

Final Task :

Finally we are done with our setup of Ansible. Now it's time to check few things. Follow the below mentioned commands and by observing the output you will come to know if you environment is ready or not…

Using this command we can see that Ansible is able to locate my hosts IPs. Here “all” means I am requesting Ansible to show all the hosts that Ansible can track from inventory file. Once you learn Inventory grouping then instead of “all” you can write the group names too.

Next to check the connectivity we can use…

Here using this command I want Ansible to ping to all the hosts, but this command failed. Why ?

Because, If you see the output message you can get the idea that Ansible is saying that it tried to login to the managed node using SSH, but unable because it needs some other program to be installed. As you can noticed that Ansible is saying to install “sshpass” program so let's install it on our controller node. For that run the below mentioned command…

yum install sshpass -y

Now let's again run the same command again and see if Ansible is able to ping to the managed nodes or not.

Congratulations… We have successfully setup our Ansible Environment on our Local Machine.

Little bit Basics of Ansible :

Created by Raktim

Now let's learn few basics of Ansible means how to run Ansible commands. There are lots of ways to configure server using Ansible like Ad hoc Commands, Ansible Playbook, Ansible Roles, Ansible Galaxy etc.

Let's learn the basics about Ad hoc Commands & Ansible Playbook.

Ad hoc Commands :

These are simple ansible commands we run on terminal to configure anything on the managed nodes. I don't want to talk in details about Ansible modules but you can think whatever we do in Ansible we need to use Modules. Like if you noticed the last command there I use one option called “-m” means after this I need to mention which module I want to use. In previous command I used “ping” module.

To know more about Ansible Modules : https://docs.ansible.com/ansible/latest/user_guide/modules_intro.html

Similarly to install any package on managed node we can use “package” module. Let's see the ansible command to install “firefox” on managed node.

ansible all -m package -a “name=firefox state=present”

Here this “-a” option signify the argument we want to pass to the package module. Now using argument I told my package module that I want one package named Firefox to be present on my managed node.

Let's see the output of this command…

As we can notice that Ansible has done some changes on my managed node and it install the Firefox package successfully. Now if you again run the same command you will notice the intelligence of Ansible.

Now this time Ansible detected that Firefox package is already present on managed node, so Ansible don't do any changes. This is called idempotence.

To know more about Ansible Ad hoc Commands : https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html

Ansible Playbook :

Source : Google

Now from here the actual Ansible Scripting starts.

Frankly speaking writing any Ansible Script is so much easier because of YAML language. Let's convert the same Firefox installation command in Ansible Playbook script.

As you can noticed here at first I write the hosts group name to all means I want to run this playbook on my all hosts.

Next in playbook we use “tasks” keyboard to tell that the list which is under “tasks” are the tasks I want to perform, like one task is installation of Firefox.

are the tasks I want to perform, like one task is installation of Firefox. Next you already know “package” module and also know the arguments of this module.

One small note : In Ansible we don't use the term task, here we use the term “play” and as this file contain lots of plays so it's called playbook.

Now let's run this playbook. Go to the folder where you wrote this YML file and run the below mentioned command shown on the screenshot.

Now as we have already installed “Firefox” using Ad hoc command so it's showing everything “ok”. But if you want to uninstall “Firefox” then in state simply mention “absent” instead of “present”.

But if you want to uninstall “Firefox” then in state simply mention “absent” instead of “present”. To know more about Ansible Playbook : https://docs.ansible.com/ansible/latest/user_guide/playbooks.html

Docker Practical :

Created by Raktim

I hope now you know how to work on Ansible. Also till now we have seen that writing Ansible Playbook is so easy.

Now we gonna do one real practical where I will show how to setup yum for installation of docker on managed node. Next we will also see how to start docker, pull image and start webserver and finally we gonna host our own website using Docker. These each and every procedure we gonna perform using Ansible Playbook.

Problem Statement :

Write an Ansible Playbook that does the following operations in the managed nodes:

1. Configure Docker

2. Start and enable Docker service

3. Pull the httpd server image from the Docker Hub

4. Run the httpd container and expose it to the public

5. Copy the html code in /var/www/html directory and start the web server

Configuration of yum for Docker and Installing Docker :

On the above mentioned code you can notice that we at first use one module called “yum_repository”. This module helps to setup yum repo. Arguments of this module is the same we use in any yum repo file.

Note : There is no proper support of “docker-ce” on RHEL8. That's why we are using centos online repository to download the software.

Next as RHEL8 don't have proper support for “docker-ce” so directly using package module we can't install “docker-ce”. In Ansible we also have dedicated yum module for installation of packages but using that also we can't install “docker-ce”.

That's why we are using “command” module to run the yum command on managed node to install docker-ce. Only for the option “- -nobest” we can't use package or yum module.

Start and enable Docker service :

Here we at first use service module to start the docker service. Also to make service enable we used the option “enable” to yes.

Also to make service enable we used the option “enable” to yes. Next we are installing python 3 because to work with docker service using Ansible we need one dedicated python library called “docker-py” installed on our managed node. So, to install any python library we need to install python at first.

because to work with docker service using Ansible we need one dedicated python library called installed on our managed node. So, Next using “pip” module we install “docker-py” python library.

Pulling the custom httpd server image from the Docker Hub :

As you can noticed here I used one module called “docker_image” to pull my custom created httpd server image — “raktim00/httpd-server”.

to pull my custom created httpd server image — Next before creating container I created one folder in my managed node using file module. While creating container I will attach this folder inside the container.

Running the httpd container :

Here we are using one module called “docker_container” and we are creating one docker container from the image “raktim00/httpd-server”.

and we are creating I had given the name of the container “webserver” and also I exposed the port 80 of the container to external port 8085.

and also Lastly I attached one volume from “/etc/http_files” folder of the base os of managed node to “/var/www/html” folder of the container.

Copy the html code in /var/www/html directory :

This code is creating one html file inside “/etc/http_files” folder and as this folder is connected to the container so we can easily access this html file when we start the httpd.

In my image it's already set that whenever you create container it will automatically start the httpd services.

and as this folder is connected to the container so we can easily access this html file when we start the httpd. Lastly we can easily write firewall rules but to make things simple I disabled the firewalld using service module.

Let's Deploy :

You already know how to deploy the ansible playbook. For reference I am providing the GitHub repository link where you can find the complete code.

Let's see the output :