🔰11.3 Restarting HTTPD Service is not idempotence in nature and also consume more resources suggest a way to rectify this challenge in Ansible playbook”
To solve this issue we take the help of Handlers.
What is Handler?
Handlers are just like a normal task. Handlers will perform an action when listens for a notify event. If nothing notifies a handler, it will not run. Regardless of how many tasks notify a handler, it will run only once, after all of the tasks completed in a particular play.
Handlers in ansible to be triggered only when http configuration file changes and then handler will restart httpd service.
If you run the playbook again then it will shows that Your service is started so no need the restart again this become possible because of the handlers and notify keyworks in ansible.
Handlers are just like regular tasks in an Ansible playbook but are only run if the Task contains a notify directive and also indicates that it changed something. For example, if a config file is changed, then the task referencing the config file templating operation may notify a service restart handler.
This means services can be bounced only if they need to be restarted. Handlers can be used for things other than service restarts, but service restarts are the most common usage.
Run the playbook :-
When there is change in the configuration file then the httpd service needs to be restarted.
In the end we have a handler running.
But if we run the playbook without any changes in the configuration file then there’s no need to restart the service and so the handler doesn’t executes.
So by this the problem can be resolved.
So Now our task is completed :)
GitHub Link :