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

Prince Raj
2 min readDec 14, 2020

What is httpd?

The Apache HTTP Server, colloquially called Apache, is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation.

Here we are going to use of handlers in ansible playbook with the use of Handlers

Here is the ansible playbook without handlers keyword:-

and the output of this code is always started “httpd” but we want only when there is change in conf file so handlers are used to rectify this error:-

By default, handlers run after all the tasks in a particular play have been completed. This approach is efficient, because the handler only runs once, regardless of how many tasks notify it. For example, if multiple tasks update a configuration file and notify a handler to restart Apache, Ansible only bounces Apache once to avoid unnecessary restarts.

If you need handlers to run before the end of the play, add a task to flush them using the meta module, which executes Ansible actions:

and using this playbook with the help of ansible playbook httpd service is not started:-

--

--