Run the same handler only once through many playbooks notify

329 views
Skip to first unread message

aori...@gmail.com

unread,
Mar 8, 2021, 3:31:32 PM3/8/21
to Ansible Project

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 :)
Have a nice day.

klingac

unread,
Mar 9, 2021, 11:49:16 AM3/9/21
to Ansible Project
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"
```

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

aori...@gmail.com

unread,
Mar 10, 2021, 2:38:15 PM3/10/21
to Ansible Project
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).

Brian Coca

unread,
Mar 12, 2021, 2:45:49 PM3/12/21
to Ansible Project
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'.


--
----------
Brian Coca

aori...@gmail.com

unread,
Mar 15, 2021, 12:54:31 PM3/15/21
to Ansible Project
Thanks.
That's what I thought.
Reply all
Reply to author
Forward
0 new messages