Making "Restarting a service" idempotent using Handlers in Ansible.
Restarting HTTPD Service is not idempotance in nature and also consume more resources so everytime we execute a playbook which has a service that stated as restart so it is quite expensive in terms of computational resource and time and both these things plays a decent role in a organization development and reputation in market.
Even though nothing is changed, the task of restarting the service executes everytime we execute the playbook.
So to avoid the extra expense of computational resource and save time we need to make the restart service idempotent in nature as well.
In ansible restarting a service is not idempotent but it provides a way to do so.
notify : it triggers a handlers
handler : It contains general tasks but these tasks executes only if someone triggers it. and to do so we use "notify"
In this case of restarting a service we can use the concept of handlers i.e put the restart task inside the handler and put the notify at the task after which there is a requirement to restart the service.
The Image below shows how to apply handlers.
here is the execution of playbook after adding handler.
that's it we make the restarting service Idempotent in nature.