
Ansible Handlers
The concept of idempotency.
Ansible is a laudable tool in the it-industry. With the germination in the concept of automation, Ansible has no doubt left its mark. Renowned for the playbooks which are not only easy and handy but also help to manage the system with one go.
In short, Ansible is a configuration, provision tool built on top of python. It has in-built modules that help to achieve an abundance of tasks with a click. It avoids the manual visit to the system and of course saves you a worthwhile time.

As of now, you got an abstract of ansible, we move a step ahead discussing what how why we require handlers in ansible.
“ Restarting HTTPD Service is not idempotence in nature. “
The above line seems obscure. Well, Apache httpd is a service offered by the software which is a foremost necessity when you deploy a webserver in your system. The next eye-catcher is IDEMPOTENCE

What is idempotency?
The principle that enables Ansible to be declarative and yet reliable is idempotence, a concept borrowed from mathematics. An idempotent operation is one that can be applied multiple times without changing the result beyond the initial application, such as multiplication by zero.
In lay-man words, idempotence is the concept of re-doing an operation without bringing a change in it. Like you multiply a number with 1 will always give you the same result no matter how many times the action has been performed.
So,
Whenever we create a webserver playbook it is observed that the service gets restarted the number of times we play it . We want the concept of idempotence to use here as well . Therefore , to achieve it we use ansible handlers .
Ansible Handlers

What are ansible handlers?
Handlers are just like normal tasks in an Ansible playbook but they run only when if the Task contains a “notify” directive. It also indicates that it changed something.
It suggests that the task is re-executed only when certain changes have taken place otherwise it remains as it is.
We will use this concept to restart an httpd service in ansible-playbook :

Create the following playbook to deploy a webserver. This playbook does not use handlers hence when we run it twice the service is restarted despite any changes.

Explanation :
This playbook is to deploy a webserver on the managed node. In addition, we have used the concept of authentication to make it secure. We have copied the configuration file of httpd by making the required changes using the replace module.
Run the playbook :
< ansible-playbook playbook_name>

Re-running the same playbook

Now we create a playbook using ansible-handlers. Likewise, this playbook is run twice. It is observed that re-running the playbook does not restart the httpd service.
Playbook with handlers :

Here we put the service of restarting the server under handlers. Also, we have used the keyword notify. The notify keyword is used to handle the handlers i.e it notifies the handlers of any new changes made. It's also noticeable that we write the handler name in the notify keyword so that it notifies that particular handler.
In this we made no changes in the configuration file hence, it notifies the handler not to restart the service since no changes have been made.
Running the playbook:

Conclusion :
This draws us to the conclusion that ansible-handlers are a way to achieve the idempotency in restarting the httpd service. Handlers check whether any new changes have made. If not, then no changes are made. If yes, then the service is restarted.
