Automating Apache webserver Configuration on Docker Container using Ansible

 

Ansible is an open-source software used for configuration management, provisioning, of any OS. We can use YAML as a language to write the code for ansible, main thing about the ansible is that it is agentless which means it doesn't need any software installed in the target system to run the ansible commands.

  • Controller node: The node on which ansible is installed and the from where we run the playbook to manage the target node are known as a controller node
  • Managed node: The node on which the playbook implements or which is going to be configured is known as managed or target node.
  • Playbook: Playbook is a file that contains all the list of instructions or commands which we want to perform in the managed node, the playbook is written in the YAML language.
  • Inventory: This file contains the list of all managed node, it contains the detail like pass, username and IP address of the target node.

In this article we will automate launch of a docker container using “ httpd “ docker image and configuring webserver in docker container of Target node with the help of Ansible.

we can create Inventory file anywhere we want but we need to update the location of inventory in the ansible configuration file also. I have created my inventory on the “ /root/ip.txt “ location of the Controller Node, and it will mainly consist of few details about Target Node.

vim /root/ip.txt

Here we have to give a username, password and the protocol by which ansible can log in to that system for configuration management.

Updating Configuration file

It is a one time process as after this we only add the IP of the nodes in the inventory file

vim /etc/ansible/ansible.cfg

Here we have provided the path of the inventory and also used host_key_checking=false as when we do ssh from the controller node to managed node. we have to accept the public key from managed node we are doing this with the program there is no one to permit so we are disabling the host_key_checking.

Now we have to check if the IP is connecting or not and for that, we will use the ping command of the ansible

As we can see our Target node is getting connected now we can perform the task on this node.

For Installing any software we have to use the yum command and for using the yum command first need to configure the yum repository and while configuring the yum repository we have to provide the location where the packages are present and for this first, we have to mount the DVD with a folder.

vim docker.yml

file module is used to create a folder in the Target node and we have also mentioned the path and name of the folder we want to create and after this, we have also mounted a DVD with that folder.

Now we will write the task for configuring the yum repository from the DVD

These tasks will create 2 yum repository one is for AppStream and another is for BaseOS packages.

We also have to configure the yum repository for docker as well as the docker software is not available in the DVD so we need some internet link where docker software is present.

Hence our all the configuration is done till now so let's run the playbook and see if its working or not using the command
ansible-playbook docker.yml

As we can see all our task are working fine and if check Target node the yum repository is also created.

we will use the docker community edition as it is open source and free to use for this we have to write a task in the ansible-playbook for installing docker software as well as for starting the docker services

For installing we have to use package module of ansible in which we have to provide software name and the state as a present this means while performing this task we want our software to be present in the system and for starting the service we have used service module of ansible in which we have to pass the name of the software and state as started

Now let's check if this task is working or not by running our playbook

We can see change is written it means that the software is not previously present in the system and ansible installed as Ansible is idempotent it will only change the state if the final state is different then the previous one by this it saves CPU time and resources.

To check if the services are actually running in the target node we can use the following command. This command is exclusive to centos/ RHEL8 and depends upon the OS on the target node.

systemctl status docker

From the above command, we can clearly see the services is active.

For pulling an image from the docker hub we will use the docker_image module of the ansible

But while running this we encountered an error

By analyzing this error we come to know that we need to install the Python SDK for docker. So let us add this to the playbook too. For this, we have to use the pip module of python to install this dependency but we know the pip module comes from python software so first, we need to install python software as well before this task.

We have added task for installing python and for installing docker dependency as well now let's run our playbook.

Now everything looks fine we can even check it by running the docker images command on the target node which shows the httpd image that has been installed on the system.

I have created a webpage with the name of web.html in my Controller node

Now we have to transfer this webpage to the target node using the copy module of ansible.

We have used the Ansible file module to create a directory where we will keep all of our web pages and then we used the ansible copy module to copy that webpage from the Controller node to the Target node’s /doc-webpage/index.html file.

Now let's run our playbook to check if it is working

we now start our container and here we are using port number 8080 to host our webpages and also we are now using the same folder in which we have our website as a volume and to docker we are sharing the same folder as a volume to docker’s httpd container default document root.

Here we used the docker_container ansible module to launch a new container we use the port forwarding concept and exposed port 80 of the docker container to port 8080 of the target node and also we have also mapped the “/doc-webpage” folder with the docker container web server default root folder.

We have also use the firewall module of ansible as when we access the webpage firewall always block us to connect so we have to allow the firewall to connect on a port of the docker host.

Now let's run our playbook to check if it is working or now

The IP address of a Managed node in my case is: 192.168.0.111 Expose the port 80 of the container to 8080 of the host virtual machine(managed node) because the Controller node can interact directly with a managed node only and not the docker container.

So to access the webserver we use link 192.168.0.111:8080/index.html

we used the port forwarding concept and exposed port 80 of the docker container to port 8080 of the target node. Ip of target node:8080-> Redirects to -> Ip of docker container:80 (on which web server is running)

Here is the Github link of the code used above now you only have to add details of the system in the inventory file of ansible and the rest of ansible will do itself.

Automation is necessary for digital transformation. I hope now you have an idea about Ansible to get it started. Ansible is a game-changer and an automation tool that helps to manage the infrastructure.

I would be writing more about Ansible so stay tuned !!

Thanks for reading this article! Leave a comment below if you have any questions.

Comments

Popular posts from this blog

How to configure Hadoop cluster using Ansible playbook

Automation Using Python Menu-Driven Program

Configuring HAProxy using Ansible playbook