Configuring Apache Web Server on Docker using Ansible.

Harsh Agrawal
4 min readJan 9, 2021

--

Hello Guys, back with another article. In this we will find how can we automate Apache Web Server on top of docker using the Linux automation tool (ansible) .

To learn more about ansible, can refer my previous blog : Use case of Ansible in automating today’s industries…

This blog contains the following tasks:

👉 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
Webserver

To perform the above task we need to follow steps mentioned below:-

We have 192.168.29.202 ip system as Control Node and 192.168.29.225 ip system as Managed Node.

First, we need to install Ansible on Control Node using command

pip3 install anisble

After installing Ansible on Control Node we need to provide the path of inventory file in configuration file of ansible(ansible.cfg). Inventory file is a file where we write ip address of those system’s which we want configure automatically via ansible (ip of managed nodes).

Configuring Controller Node for Ansible

Now we write ip address , user name of managed node, password of the managed node(in red portion) and connection type, here ssh.

Now we are all set to start the automation via ansible. For checking the connectivity between managed node and control node run the command

ansible all -m ping

If the text come in green color or “ping : pong” message appear’s then the connection is established successfully.

Checking connectivity

Since we are ready with our setup, now we will perform our task to Configuring Apache Web Server from Controller node on Managed node using Ansible.

So lets move to our workspace and write yml script.

Using vim editor for writing yml file

Lets write script to accomplish our task.

Write the script in the same format and make sure of indentation to avoid any error.

If we are unable to rectify any syntactical error then refer the website yamllint.com

Name key is given in every block of script to describe the working of that block (eg:-first block of script is for “name : setting up docker yum configuration”)

Writing ansible script
Writing ansible script

Press esc button and then colon(:) + w+q [:w q] to escape from vim editor.

Now run the command

ansible-playbook docker25.yml
running yml file
running yml file

Now go to managed node(192.168.29.225 ip) .We can clearly see that docker-community edition(docker-ce) is successfully configured on managed node.

Run the following command to find ip address on which Apache server is running.

docker inspect httpd | grep IP

Note — IP should be in capital in above command.

Finding IP address of docker container

Go to Fire Fox browser on the system and type the ip address that is generated from the above command and see if your web page is running successfully

Congratulations! Our server is up and running !!!

Apache server is up and running

Thank you guys for reading my blog .Follow me for more such content and hit clap button if you liked it.

Harsh Agrawal

--

--