How to retain Idempotency in Ansible while restarting HTTPD service?

Mihir Patel
3 min readDec 18, 2020

What is Idempotency?

Idempotence is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application. The concept of idempotence arises in a number of places in abstract algebra and functional programming. for example When we multiply any number with 1 but end result always be the same.

In this Blog you will see the idempotency in ansible while httpd service , when we use Restarted keyword in ansible-playbook then the httpd service always restart . but for retain idempotency in ansible we should make ansible-plabook as the our httpd service restart when we change in httpd configure file . if not change in .conf file at second time of run playbook then httpd service not should restart . For this making .. se bellow

Handlers: running operations on change

Sometimes you want a task to run only when a change is made on a machine. For example, you may want to restart a service if a task updates the configuration of that service, but not if the configuration is unchanged. Ansible uses handlers to address this use case. Handlers are tasks that only run when notified. Each handler should have a globally unique name.

Lets, see our .conf file :

Now create the ansible-playbook (http.yml) for httpd :

Playing the Playbook for the first time

ansible-playbook http.yml

When you will run your playbook the first time then you will find your all tasks are run and being shown the brown color that’s mean the task is run the first time or you have changed something in the task. Also, you can see the above clip which I run first all tasks are being shown the orange colors. changed = 2

Playing the Playbook for the second time without change in .conf file

Since there are no changes in the configuration file the handlers are not notified and the Service is not restarted and also green color presents there is no change has happened as you see in the above clip.

change = 0

Playing the Playbook for the third time with change

When we change in file then restart the httpd so we maintain the idempotency in the file

In we change the doc_root = /vae/www/web1

Lets’s see our page is working or not :

So our Page successfully Run

Thank you

--

--