Rectifying challenge in Ansible playbook for Restarting HTTPD Service

Aakash Bhardwaj
2 min readJul 8, 2021

Ansible playbook uses a declarative language approach and is idempotent in nature i.e., if some tasks are already done then it does not do it again! But this is not idempotent in the case of restarting the httpd service.

If it keeps on restarting the services again and again even though the services are started, it will consume more resources and time, which is not a good for the users.

So, in order to make this optimized, use “when” keyword and “register”:

for the first time it restarts the services, then if run the playbook again this time it will not restart the services making it non-idempotent.

As we can see that the “starting services” task is skipped. Using register to register a task in a variable and using that registered value and doing another task based on when variable changed.

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

Ansible playbook uses a declarative language approach and is idempotent in nature i.e., if some tasks are already done then it does not do it again! But this is not idempotent in the case of restarting the httpd service.

If it keeps on restarting the services again and again even though the services are started, it will consume more resources and time, which is not a good for the users.

So, in order to make this optimized, use “when” keyword and “register”:

for the first time it restarts the services, then if run the playbook again this time it will not restart the services making it non-idempotent.

As we can see that the “starting services” task is skipped. Using register to register a task in a variable and using that registered value and doing another task based on when variable changed.

--

--