Making the HTTPD Service Idempotence in Nature using Ansible Playbook

Writing a playbook to make HTTPD service Idempotence dor utilizing Computer Resource properly.

MishanRG
4 min readMar 20, 2021

Greetings Everyone!!! I hope you all are fine and doing well.

In this blog, we will be creating an Ansible Playbook that will help us install HTTPD in a system and run the service idempotence when the playbook is running again. If you need some insight related to Ansible, you can check this blog.

So let’s get started with the task…

STATIC PLAYBOOK

First, let’s write a simple and static playbook that will install and start the “httpd” service. We will observe why we cannot get Idempotence Nature with HTTPD with a simple and regular setup.

Below is the code from my GitHub, which I wrote for a regular installation of httpd.

So What is the issue?

Above, you can see that I am installing HTTPD and creating an index file, and restarting httpd. So what happens is every time I change my code or configuration file, the httpd service will restart, but when I am not changing my configuration file of HTTPD and just changing the code, HTTPD shouldn’t be restarted. And restarting the HTTPD every time playbook run is not good for the servers, and it takes a lot of computing resources.

As in the above image, you can see that we have installed and started the httpd service, and it is running in our managed node.

When we ran the same playbook, we can see that the task “Started HTTPD Service” is changed, which means it ran again, which will mean it restarted the HTTPD service.

DYNAMIC PLAYBOOK as SOLUTION

So we have a solution to this problem. We will use the concept of “handler and notify” in Ansible Playbook.

Handlers: Handlers in Ansible is a concept in which we will write the task inside it when we want to run the task when some other tasks notify it to run. We will use it on the task where the configuration file will be changed, and if that task runs, it will notify the handler task of restarting HTTPS. If it didn’t run HTTPD task also won’t run.

Notify: Notify is a concept we add to the task which needs to be run to run the handlers task. Here changing the configuration file task will notify handlers to run.

Here is the copy of the code which I am using to make the HTTPD service. Idempotence nature, and here are the modifications I made in our old playbook.

As you can see in this code, at last, there is a feature called handlers, and inside the task of “Copying Configuration File,” I have used notify for the handler called “RestartService.”

If we have many handlers then we denote then with their name on the notify.

So this way, when I copy a configuration file in the httpd configuration, it will notify the handler and run the handler task.

In the above image, you can see the task of “Copying Configuration File” is changed, and at last, the handler task is also running.

But when I reran the playbook like I did last time, this time it didn’t change my Copying task, so my Handler task of restarting HTTPD also didn’t run. Now, whenever I change my configuration file, only HTTPD restarts, and that is what we wanted.

CONCLUSION

So we have completed our task. We have made our HTTPD service Idempotence as we wanted it using an Automation Playbook.

You can check my GitHub link down for the code of the same task.

I hope I have cleared all the doubts and presented my point clear. If you have any doubt and confusion, always feel free to contact me on my LinkedIn

Thank You for your time. Have a Good Day.

--

--

MishanRG

I blog about ML, Big Data, Cloud Computing. And improving to be the best.