Ansible Pilot

Two Ways to Run Multiple Ansible Handlers - Ansible Playbook

How to execute two Ansible handlers on a changed status of Ansible Playbook.

March 13, 2023
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

Two ways to run multiple Ansible handlers

How to execute two Ansible handlers on a changed status of Ansible Playbook.

What is an Ansible handler?

Handler runs tasks on change.

Handlers execute some tasks only when the previous task returns a changed status. If not necessary, they don’t execute.

Demo

Let’s jump into two real-life examples of how to run multiple Ansible handlers. First of all, we need a task changed status. The simplest Ansible module returning a “changed” status is the command module with a Linux command, like “uptime”. Let’s suppose we would like to execute two handlers on the screen, for example, two messages on the screen.

Solution 1

code

---
- name: handler demo
  hosts: all
  tasks:
    - name: Test connection
      ansible.builtin.command: "uptime"
      notify: message

  handlers:
    - name: message 1
      ansible.builtin.debug:
        msg: message 1
      listen: message

    - name: message 2
      ansible.builtin.debug:
        msg: message 2
      listen: message
localhost ansible_connection=local

execution

$ ansible-playbook -i inventory two-1.yml

PLAY [handler demo] *********************************************************************

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

TASK [Test connection] ******************************************************************
changed: [localhost]

RUNNING HANDLER [message 1] *************************************************************
ok: [localhost] => {
    "msg": "message 1"
}

RUNNING HANDLER [message 2] *************************************************************
ok: [localhost] => {
    "msg": "message 2"
}

PLAY RECAP ******************************************************************************
localhost                  : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Solution 2

code

---
- name: handler demo
  hosts: all
  tasks:
    - name: Test connection
      ansible.builtin.command: "uptime"
      notify:
        - message 1
        - message 2

  handlers:
    - name: message 1
      ansible.builtin.debug:
        msg: message 1

    - name: message 2
      ansible.builtin.debug:
        msg: message 2

execution

$ ansible-playbook -i inventory two-1.yml

PLAY [handler demo] *********************************************************************

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

TASK [Test connection] ******************************************************************
changed: [localhost]

RUNNING HANDLER [message 1] *************************************************************
ok: [localhost] => {
    "msg": "message 1"
}

RUNNING HANDLER [message 2] *************************************************************
ok: [localhost] => {
    "msg": "message 2"
}

PLAY RECAP ******************************************************************************
localhost                  : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
PLAY [handler demo] *********************************************************************

Recap

Now you know Two ways to run multiple Ansible handlers.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases