How to make HTTPD service idempotence in Ansible Playbook

Introduction :
As we know that Ansible is a great tool for configuration management, we can configure httpd server easily using Ansible. Everything is good until you have some changes. After you successfully configured httpd server using Ansible, it will run greatly but if you change anything in the configuration file of httpd you need to restart the services. Restarting the service everytime you run the playbook is not good as it creates downtime to the httpd server.
But how to do it….
Let’s see ….
Idempotence :
Idempotent means the result will be the same as if you do it once or many times. In Ansible, after you run the playbook, you will get the desired state. But if you run the playbook again, it does not do any operation as it already met desired state. If you change anything in the playbook, the result will be changed. Then it will do the operation as the result changes. This is a feature of Ansible modules called Idempotence. Ansible modules are idempotent.
Idempotence by default only works as per task but it doesen’t perform task to task. What does it mean ? Idempotency only works per task and that belongs to that task itself but it doesn’t work as if the one task changes other task too change. If anything changes in one task, that task only performs again but it doesen’t perform any other task again.
Problem :
After you successfully configured the httpd server, all things will be ok and running great. When you want to change anything in the server configuration like port number or Root directory, you need to change in the configuration file. As soon as the configuration changes, you need to restart the httpd server until then it doesn’t work. Due to idempotence, as the server is already started, how can they start the service again. They need to restart the service but the restarting the service is a seperate task and if you use it in the playbook without any condition, it will perform the task again and again whenever you run the playbook without any need. That creates huge downtime. So restarting the service is not idempotent.
Solution :
We have to make restarting the service idempotent, so that it only performs the task of restart when the configuration file changes. Here the role of Notify and Handler comes into play. We use notify keyword to the particular configuration file task. This will notifies to the handler that something changes. When it notifies that something changes, handler block will run and perform the task.
Here you can see the pictures how it works

Here my port number is 8080 and root direcory is web.

You can see there is nothing changed as they already in the desired state. So no point in restarting the service.

Here, I have changed root directory of httpd server and so configuration file will be changed.

You can see here the configuration file changed and notify notifies the handler and handler performs their task i.e., restarting the service.
You can get my entire code of playbook from my Github link https://github.com/rangamani54/Ansible_httpd.git
Thankyou…
Happy Learning…
Keep Sharing…