Run the same handler only once through many playbooks notify

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 :

    • prerequisite
    • install app1[notify handler] (<-do not run now but notify)
    • configure app1 [notify handler] (<-do not run now but notify)
  • playbook 2 :

    • prerequisite
    • install app2 [notify handler] (<-do not run now but notify)
    • configure app2 [notify handler] (<- run now cause it’s the last notify)

If you have any idea, I would be grateful :slight_smile:

Have a nice day.

Hi,

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


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

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

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).

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'.

Thanks.
That’s what I thought.