Automation Is Fun With Ansible

Manish Verma
8 min readAug 7, 2020

Automation of HTTP Apache server on Docker using Ansible

Hello Everyone!!

I am back again with my new article on Automation. This time I am going to introduce you to one of the most demanding and powerful technology used in Automation world for configuring multiple same or different environment and servers just by writing a few lines of codes.

Now its time to introduce with Ansible and some key components.

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 includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015. Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks.

Anisble Modules (also referred to as “task plugins” or “library plugins”) are discrete units of code that can be used from the command line or in a playbook task. Ansible executes each module, usually on the remote target node, and collects return values.

Ansible Playbooks are Ansible’s configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process.

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.

Apache HTTP Server, colloquially called Apache, is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation.

Till here we have understand about Ansible and its component, Docker and Apache HTTP Server.

Here, I am using Ansible to complete one task and the Objective of the task is to create a setup which has HTTP Apache server running on Docker container platform and I have to setup this environment using the Ansible.

Description of the task:

  • Configure Docker
  • Start and enable Docker services
  • Pull the httpd server image from the Docker Hub
  • Run the httpd container and expose it to the public
  • Copy the html code in /var/www/html directory and start the web server

To do this task first we need to understand requirements which I have mentioned below.

Now its time to setup our system to leverage the power so such a powerful automation. So from here we are going to understand how to install these tools in our system.

Note: Here I am using the RedHat version 8 Linux Distribution for setting up the Ansible environment which is acts as Controller Node and another RedHat environment which acts an Agent Node or a Managed Node.

Requirements:

  • RedHat Enterprise Linux version 8
  • epel-release-latest-8.noarch.rpm : yum repository to install Ansible software. To download this rpm package visit to my GitHub profile link given below click on the underline name.
  • Python SDK for Docker

Now its time to start hands on!

Note: I am assuming that you have installed the RedHat with GUI and configured local yum repository.

Here I have assigned some name to the node as follow:

  • Ansible environment named as “Controller Node”
  • Agent node named as “Managed Node”.

First of we need to install the epel-release-latest-8.noarch.rpm package in our Controller Node because this package or yum repository provides the Ansible software. To install this package use this command:

rpm ivh epel-release-latest-8.noarch.rpm

epel package installed in Controller

After installing the epel-release package we need to install Ansible. For installing Ansible use this command:

yum install ansible -y

or

dnf install ansible -y

Ansible installed in Controller Node

To check whether Ansible is installed or not run this command:

ansible --version

Ansible is installed succesffully

We need to provide some information of our Agent nodes to the Ansible . So for this we need to create one file which is known as “Inventory” where we put the IP Address’s with the credentials of our Agent Node with some Group name. Inventory is a kind of database that stores the IP Address’s and SSH credentials, so that Controller Node can easily login to the Managed Nodes.

Inventory created

Here “myhosts.txt” is in Inventory file, where [web] denotes group name then IP Address, username and password of Managed Node.

We also need to update Ansible with this Inventory file. So for this we need to edit or create one file called “ansible.cfg” inside the /etc/ansible/ directory.

Inventory updated

Now to check whether our Inventory is update and Managed Node is recognized by Ansible or not run this command:

ansible --version

then

ansible all --list-hosts

Inventory updated and Node added

To test the connectivity between the Controller Node and Managed Node, we need to ping from Controller Node to the Managed Node.

Connectivity is Good

Till here the environment for Controller Node and Managed Node is setup successfully.

Now its time to perform our task and play with Ansible Playbooks.

Note: I have done this task in different parts so that we can connect with task easily and understand as much as possible.

We need to install one SDK which is “Python SDK for Docker” in our Managed Node which is used run Docker commands by Python. To install this SDK we need to run this command:

pip3 install docker

So as per the description of the task first of all we need to setup the yum repository for Docker and then we need to install Docker. This thing can be done in this way. I have create one file called “docker.yml” which consist of all the modules that are required to setup yum, install Docker, started and enabled its services. Here you find the more information about modules and options yum_configuration, docker_install and service.

Ansible code:

Creating yum repository and installing Docker software

After writing this entire code, we need to run these code as an Ansible Playbook. So we need to run this command:

ansible-playbook docker.yml

Playbook successfully executed
Docker installed successfully

Till now our docker repository is created and docker software is installed in Managed Node.

Now we are moving to our next step of our task where I have create a file called “docker_image.yml” which is used to pull the image from Docker Repository which is required to create containers that runs on Docker engine. To do this we need to run these code as Ansible Playbook. Here you find the more information about modules and options docker_image .

Code to pull the HTTP image

After writing this code, we need to run these code as an Ansible Playbook. So we need to run this command:

ansible-playbook docker_image.yml

Playbook successfully executed
HTTPD image successfully pulled(downloaded)

Now we have reached to the last step of our task where we have to launch one container using the HTTPD image that we have downloaded from Docker repository and copy the html code created by developer in “ /var/www/html/” directory then start the web server. This container is exposed to the public or client. For this part I have create one file called “docker_container.yml”. All things can be done by these coding file. Here you find the more information about modules and options docker_container and copy.

Code to create Docker container

This is the code for HTML page that I have created to deploy on HTTPD server.

HTML code to be deployed on HTTPD server

After writing this code, we need to run these code as an Ansible Playbook. So we need to run this command:

ansible-playbook docker_conatiner.yml

Playbook executed successfully
Docker container run and exposed

Note: Maybe in this last step sometime we face some issue due to some “Internal Server Error” or “iptables failing”. So to fix this issue we need to run some commands in the sequence on Managed Node:

docker rm -f container_name (only if container is launch in managed but failed)

iptables -F

iptables -P FORWARD ACCEPT

systemctl restart docker

Finally we have reached to our target that is the final output of the HTML page which we have copied and deployed on the HTTPD server using container on Docker engine.

Final Output

— — — -— — — — — — Task completed — — — — — — — — —

Some words for my mentor:

I would like to thank you to my mentor “Mr. Vimal Daga” Sir for sharing his superb knowledge and great understanding over these topic with me and lots of other people like. I just want to say that the name “Mr. Vimal Daga” means “Excellence”.

For coding files and epel rpm package visit to my GitHub profile:

Queries, Suggestions and Feedbacks are always welcome!

Find me on LinkedIn as:

Thank you very much for reading this article. Share with other so that lots and lots of people should get benefited from this article.

--

--