# Run the same handler only once through many playbooks notify

**URL:** <https://forum.ansible.com/t/run-the-same-handler-only-once-through-many-playbooks-notify/34746>\
**Category:** Ansible Project\
**Created:** [March 8, 2021, 8:31pm UTC](https://forum.ansible.com/t/run-the-same-handler-only-once-through-many-playbooks-notify/34746 "2021-03-08T20:31:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quentin\_Aveno](https://avatars.discourse-cdn.com/v4/letter/q/e9c0ed/32.png) [@Quentin\_Aveno](https://forum.ansible.com/u/Quentin_Aveno)\
**Post date:** [March 8, 2021, 8:31pm UTC](https://forum.ansible.com/t/run-the-same-handler-only-once-through-many-playbooks-notify/34746/1 "2021-03-08T20:31:32Z")

</div>

Hi everyone.

Is there a way to run a handler once through multiple playbooks.

Explanation :  
I have a “master” playbook which imports several other playbooks.  
I can run the “master” playbook or one individual playbook depending ot the situation.

Some of those playbooks notify the same handlers during key tasks.

But I have to run this handler only one time for all the playbook after the last notify.

Example :

master playbook:

- playbook 1 :

- playbook 2 :

If you have any idea, I would be grateful 🙂

Have a nice day.

---

<div class="post-metadata">

**Author:** ![klingac](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@klingac](https://forum.ansible.com/u/klingac)\
**Post date:** [March 9, 2021, 4:49pm UTC](https://forum.ansible.com/t/run-the-same-handler-only-once-through-many-playbooks-notify/34746/2 "2021-03-09T16:49:16Z")

</div>

Hi,

try to use “listen” in handlers definitions, e.g.:

```yaml

handlers:
- name: Restart apache

ansible.builtin.service:
name: apache
state: restarted
listen: "restart web services"

tasks:
- name: Restart everything
ansible.builtin.command: echo "this task will restart the web services"
notify: "restart web services"

```

More info in documentation [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_handlers.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_handlers.html)

Dne pondělí 8. března 2021 v 21:31:32 UTC+1 uživatel [aori...@gmail.com](mailto:aori...@gmail.com) napsal:

---

<div class="post-metadata">

**Author:** ![Quentin\_Aveno](https://avatars.discourse-cdn.com/v4/letter/q/e9c0ed/32.png) [@Quentin\_Aveno](https://forum.ansible.com/u/Quentin_Aveno)\
**Post date:** [March 10, 2021, 7:38pm UTC](https://forum.ansible.com/t/run-the-same-handler-only-once-through-many-playbooks-notify/34746/3 "2021-03-10T19:38:14Z")

</div>

Hi,

Thank you very much for the answer.

Unfortunetly, I don’t think that it will answer my question.

I probably was not clear enough, and I’m sorry…  
I thinks that what I’m asking is not possible due to handler scope, but I hope I’m wrong…

An example :

cat \<\< EOF \> playbook1.yml

- hosts: localhost  
gather\_facts: no  
handlers:
- import\_tasks: handler.yml  
tasks:
- debug:  
msg: “Child playbook 1 - task 1”  
notify: “one handler”  
changed\_when: True
- debug:  
msg: “Child playbook 1 - task 2”  
notify: “one handler”  
changed\_when: True  
EOF

cat \<\< EOF \> playbook2.yml

- hosts: localhost  
gather\_facts: no  
handlers:
- import\_tasks: handler.yml  
tasks:
- debug:  
msg: “Child playbook 2 - task 1”  
notify: “one handler”  
changed\_when: True
- debug:  
msg: “Child playbook 2 - task 2”  
notify: “one handler”  
changed\_when: True  
EOF

cat \<\< EOF \> playbook\_parent.yml

- name: child playbook1  
import\_playbook: playbook1.yml
- name: child playbook2  
import\_playbook: playbook2.yml  
EOF

cat \<\< EOF \> handler.yml

- name: “Handler”  
debug:  
msg: “Handler”  
listen: “one handler”  
EOF

Exécution of playbook\_parent.yml :

user-docker@6f808e321c8c:~$ ansible-playbook playbook\_parent.yml  
[WARNING]: No inventory was parsed, only implicit localhost is available

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match ‘all’

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [localhost] =\> {  
“msg”: “Child playbook 1 - task 1”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [localhost] =\> {  
“msg”: “Child playbook 1 - task 2”  
}

RUNNING HANDLER [Handler] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “Handler”  
}

PLAY [localhost] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [localhost] =\> {  
“msg”: “Child playbook 2 - task 1”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [localhost] =\> {  
“msg”: “Child playbook 2 - task 2”  
}

RUNNING HANDLER [Handler] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “Handler”  
}

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

The handler is run two times (one time after last notify in playbook1 and one after last notify in playbook2). I would like to run it only once at the last notify of all playbooks (here just after last notify in playbook2).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 12, 2021, 7:45pm UTC](https://forum.ansible.com/t/run-the-same-handler-only-once-through-many-playbooks-notify/34746/4 "2021-03-12T19:45:19Z")

</div>

Handlers don't work that way, you CAN create some flag that will  
prevent it from running a 2nd time but there is no way to know 'this  
is the last notify'.

---

<div class="post-metadata">

**Author:** ![Quentin\_Aveno](https://avatars.discourse-cdn.com/v4/letter/q/e9c0ed/32.png) [@Quentin\_Aveno](https://forum.ansible.com/u/Quentin_Aveno)\
**Post date:** [March 15, 2021, 4:54pm UTC](https://forum.ansible.com/t/run-the-same-handler-only-once-through-many-playbooks-notify/34746/5 "2021-03-15T16:54:31Z")

</div>

Thanks.  
That’s what I thought.
