How to retain Idempotency in Ansible…

Task Description :

Sourabhmiraje
2 min readDec 31, 2020

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

Whenever there is a change in the configuration file of httpd we have to restart the httpd service.

So previously we are using restarted keyword in ansible but this keyword will restart the services no matter whether the change occurred or not. so this not the good practice.

Lets know how we can this problem in this article…

We can use handlers in ansible to solve this challenge :

Handlers are tasks that only run when notified. Each handler should have a globally unique name.

How we will use :

  • We want to restart the httpd service only when there is a change in a configuration file.
  • If there is no change then we have to skip the restart service task of httpd service.

Here is the playbook before :

Output:

At first time

When nothing is changed

After changing directory

GitHub Code:

Please comment below to let me know about the article…

Any suggestions are always appreciated.

Thank You for reading…

Here is My LinkedIn Profile…

--

--