Introduction with handlers in Ansible

Reading Time: 2 minutes

Hi readers, In this blog, we will be discussing the use case of Ansible handlers also we will be looking at how multiple handlers work get notifr Ansible.

What is Handlers in Ansible?

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.

Let take a example, it is useful for secondary actions that might be required after running a Task. Such as installing a new nginx after installation then run the nginx in our remote machine.

Nginx.yml

- name: mukesh
  hosts: WORKSPACE
  tasks:
         - name: Install nginx
           package:
             name: nginx
             state: present
           notify:
                Start nginx
  handlers:
       - name: Start nginx
         service:
           name: nginx
           state: started

We can run it with the ansible-playbook command:

$  ansible-playbook Nginx.yml	

PLAY [mukesh] ********************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************
ok: [18.191.18.209]

TASK [Install nginx] *************************************************************************************************************************
changed: [18.191.18.209]

RUNNING HANDLER [Start nginx] ****************************************************************************************************************
changed: [18.191.18.209]

PLAY RECAP ***********************************************************************************************************************************
18.191.18.209              : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

We can also flag multiple handlers in a single task. I have showcased below multiple Flagged handlers in a single task.

Multiple handler.yml

- name: mukesh
  hosts: WORKSPACE
  become: yes
  tasks:
         - name: update everything
           package:
               name: '*'
               state: latestFor example, it is useful for secondary actions that might be required after running a Task, such as installing a new service after installation then run the service or update configuration of service.
               exclude: kernel*
         - name: Copy using inline content
           copy:
               content: "update the file"
               dest: /home/mukesh.txt
               mode: "0777"
           notify:
                - update
                - data
  handlers:
       - name: update
         find:
                paths: /home/
                patterns: '*.txt'
                recurse: yes
         register: new_file
       - name: data
         debug:
            var: new_file.matched

We can run it with the ansible-playbook command:

$ ansible-playbook Multiple handler.yml

PLAY [mukesh] ********************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************
ok: [18.191.18.209]

TASK [update everything] *********************************************************************************************************************
ok: [18.191.18.209]

TASK [Copy using inline content] *************************************************************************************************************
changed: [18.191.18.209]

RUNNING HANDLER [update] *********************************************************************************************************************
ok: [18.191.18.209]

RUNNING HANDLER [data] ***********************************************************************************************************************
ok: [18.191.18.209] => {
    "new_file.matched": "14"
}Hi readers, In this blog, we will be discussing the use case of Ansible handlers also we will be looking at how multiple handlers work get notified by single task Ansible.



PLAY RECAP ***********************************************************************************************************************************
18.191.18.209              : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

you can check for your remote machine that Nginx is running :

$ service nginx status
nginx (pid  22488) is running...

Conclusion

A Handlers are not any different from regular tasks that are referenced as a globally unique name. handlers will perform an action when listens for a notify event. If nothing notifies a handler, it will not run. Regardless of how many tasks notify a handler, it will run only once, after all of the tasks completed in a particular play.

Written by 

I always love to learn and explore new technologies. Having working skills in Linux, AWS, DevOps tools Jenkins, Git, Maven, CI-CD, Ansible, Scripting language (shell/bash), Docker as well as ELK stack and Grafana for implementing logging and visualization technology.