Handlers are lists of tasks, not really any different from regular
tasks, that are referenced by a globally unique name, and are notified
by notifiers. 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 complete in a particular play. ansible doc
1) Handlers that do the same thing should be named the same.
restart nginx
ALWAYS restarts nginx, not handler1
and handler2
2) Handlers are run at the END of the entire "Play" a play scoped to your sections.
3) I would use the register
and when
functions for tasks that should be restarted, note this var should carry with you.
Code Source
PLAY [localhost] ***************************************************************
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "Play 1"
}
TASK [role1 : Always true in role1] ********************************************
changed: [localhost]
TASK [role1 : Always true in role1] ********************************************
changed: [localhost]
TASK [role1 : Always true in role1] ********************************************
changed: [localhost]
TASK [role1 : Always true in role1] ********************************************
changed: [localhost]
TASK [role1 : Always true in role1] ********************************************
changed: [localhost]
TASK [role2 : Run if change in task c of role 1] *******************************
changed: [localhost]
TASK [role2 : Always true in role2] ********************************************
changed: [localhost]
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "This is a task in a play"
}
RUNNING HANDLER [role1 : handler] **********************************************
ok: [localhost] => {
"msg": "This is a handler in role1"
}
PLAY [localhost] ***************************************************************
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "Play 2"
}
TASK [role1 : Always true in role1] ********************************************
changed: [localhost]
TASK [role1 : Always true in role1] ********************************************
changed: [localhost]
TASK [role1 : Always true in role1] ********************************************
changed: [localhost]
TASK [role1 : Always true in role1] ********************************************
changed: [localhost]
TASK [role1 : Always true in role1] ********************************************
changed: [localhost]
TASK [role2 : Run if change in task c of role 1] *******************************
changed: [localhost]
TASK [role2 : Always true in role2] ********************************************
changed: [localhost]
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "This is a task in a play"
}
RUNNING HANDLER [role1 : handler] **********************************************
ok: [localhost] => {
"msg": "This is a handler in role1"
}
PLAY RECAP *********************************************************************
localhost : ok=20 changed=14 unreachable=0 failed=0
Lots of ways to do the same task. Handlers were designed to prevent restarting the same process multiple times, such as multiple changes to a nginx server that has websites, ssl certs, and other tasks that need service restarts.