Automating Apache Web Server Configuration on Docker using Ansible

Automating Apache Web Server Configuration on Docker using Ansible

So we're going to host web pages using Apache's web server (i.e. httpd) on docker running on a remote system. And most important we're going to automate the entire process with the help of Ansible. We'll write a playbook to achieve this. I'm going to write everything inside a single playbook but you can create a role if you want.

As controller node and managed node we are using two RHEL8 provisioned systems.

Note: It's being presumed that the managed node have the RHEL8 DVD and yum local repository has been configured.

We'll need the IP, username and password of the managed node to update the inventory. We're giving the name 'web_server' to the IP group.

No alt text provided for this image

Let's breakdown the task into three small steps:

  1. Configure and start Docker on managed node
  2. Configure httpd on top of Docker.
  3. Copy webpage and start the web server.

So, starting with the first part we have to install docker (community edition) on RHEL8. For that we'll first update the yum repository.

Playbook screenshot

Now moving on, we have to install it and start the services.

No alt text provided for this image
No alt text provided for this image

Note: Since we're using command module to install docker, you must know that it does not follow idempotency like most of the ansible modules. In short, it'll run every time when the playbook runs. You can avoid this by writing some conditions if you like.

We also need to install the python library for docker. For that pip must be installed. Pip comes from a package called python36. This will make sure if pip is installed,

No alt text provided for this image

Now we can install the python library for docker.

No alt text provided for this image

The first step has been completed has been completed. Moving on to the second step and then the third, we're going to fetch the Apache's web server (i.e. httpd) image from docker hub.

No alt text provided for this image

We also need to create a directory to store all our web pages.

No alt text provided for this image

Now the final step i.e. starting the web server,

No alt text provided for this image

We have to expose the web server on some port number so that client can connect and view the web pages. Here, I've used port no 2020 but you may use any other port number.

Finally after saving the playbook we're going to run it.

No alt text provided for this image
No alt text provided for this image

Your web page is live now. You can visit it using the IP and the port number. The contents of the home.html file will be displayed on your web browser.

No alt text provided for this image

Troubleshooting

Error 1:

No alt text provided for this image

If you're seeing this it's because the inventory hasn't been updated properly on the controller node.

Error 2:

No alt text provided for this image

This error occurs due to the wrong username or password fed into the inventory file.

Error 3:

No alt text provided for this image

If you're seeing these red lines, it's probably because the yum local repository hasn't been configured on the managed node.

Error 4:

No alt text provided for this image

This error is because your managed node may not have the RHEL8 DVD attached. Attach the DVD and run the playbook again.


There are many more ways to mess things up and obviously I cannot cover them all. But the important thing is you should never quit. Plus you'll learn more, once you start discovering the reasons behind the error. So, now go on and automate your stuff!

Here's a link to my GitHub repository where you can find the playbook file.


That's all!

Thank you very much....!

See you soon!!!

To view or add a comment, sign in

More articles by Ankit Kumar

Insights from the community

Others also viewed

Explore topics