Making HTTPD Service Idempotent

Pjore
5 min readMar 20, 2021

Hello, Guys,🙋‍♀️ We are going to perform the following Task

Task Description:

Restarting HTTPD Service is not Idempotence in nature and also consume more resources suggest a way to rectify this challenge in Ansible playbook.

First, let's discuss What is Idempotence?

What is Idempotence???

Before moving to the meaning of idempotence let's discuss what is Non-Idempotence?

If we create any file using the command our file will be created. If we again try to create the same file then again the same file will be created by overwriting the previous file. So it is doing the same task again and again even if there is no need to do that. So Idempotence is exactly the opposite of this. If you try to execute the same thing again and again then it will just skip that thing.

Idempotence is one of the important features of Ansible. Ansible runs invoked multiple times and every time, it compares the desired state Vs current state and after that decides whether to take action or skip.

When you run any playbook for the first time then it will execute all the tasks. If you want to run the same playbook for the second time after changing a particular task in the playbook, at this time whatever task you changed only those tasks are going to run. If you run the same playbook for the third time without doing any changes then it will be going to skip all. So this helps in managing the state efficiency.

Let's consider this with the help of one example:

If you want to create a playbook for adding users and creating a password for users. In the first run, it will create a user and password for you. during the second time if a user is created and if you want to change the only the password, in this condition it's not going to create a user again it will only run task related to the password. For the third time if all conditions are satisfied and you don't perform any changes then it's not going to execute the same task again.

Let's practically see what is idempotence.

vim ip.txt

add IP of target node

vim /etc/ansible/ansible.cfg

This is our configuration file

First, check connectivity with the target node

Connectivity is successful.

Now I am just going to write the simple playbook for configuring a web server and starting their services.

vim webidem.yml

let's run the playbook now

ansible-playbook webidem.yml

This is our first run. You can see now It executed all the tasks in the first run.

Let's run it for the second time

Now you can see changes in the Start Services of Webserver and there is no change in other tasks. This happened because it is restarting the httpd service again and again.

Let's run for the third time

You can see it is again running a task related to restarting the httpd service.

We need to find a solution for this now. We can overcome this problem with the help of Handlers.

Handlers are like a tasks but it will run when only called by another task. It is like regular task in ansible playbook, but it only runs if task contains notify keyword and also indicate that it changed something.

Whatever tag you used for notify keyword (I used Start Services of Webserver here)should be the same as whatever name you define in handlers.

Let's do some changes to the code.

We created this configuration file. Our service is going to restart only when there will be some changes in this file.

vim webidem.yml

Let's run this

ansible-playbook webidem.yml

You can see it is running successfully. I am going to run this again

Now You can see there is no change and our service didn't start again.

Now I am going to do some changes to the configuration file. so let's see whether our handler will work or not.

I did these changes in the configuration file. now run the playbook again

ansible-playbook webidem.yml

You can see changes in the configuration file and our handler is running means it is restarting the services. So the conclusion is that our service restarts only when there is some change in the configuration file.

So this is how we solved the problem by using Handlers.

Our Task Completed Successfully!!!!!!!

Thanks For Reading😊😊😊

--

--