The docs says:
Since handlers are tasks too, you can also include handler files from the ‘handlers:’ section.
What I do, playbook.yml
:
- hosts: all
handlers:
- include: handlers.yml
# - name: h1
# debug: msg=h1
tasks:
- debug: msg=test
notify: h1
changed_when: true
handlers.yml
:
- name: h1
debug: msg=h1
Then,
$ ansible-playbook playbook.yml -i localhost, -k -e ansible_python_interpreter=python2 -v
...
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "test"
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0
...
But when I uncomment the lines, I see
$ ansible-playbook playbook.yml -i localhost, -k -e ansible_python_interpreter=python2 -v
...
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "test"
}
RUNNING HANDLER [h1] ***********************************************************
ok: [localhost] => {
"msg": "h1"
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0
...
I'm running ansible-2.1.0.0
.
What am I doing wrong? That's the first thing I'd like to know. Workarounds come second.
UPD
Includes can also be used in the ‘handlers’ section, for instance, if you want to define how to restart apache, you only have to do that once for all of your playbooks. You might make a handlers.yml that looks like:
--- # this might be in a file like handlers/handlers.yml - name: restart apache service: name=apache state=restarted
And in your main playbook file, just include it like so, at the bottom of a play:
handlers: - include: handlers/handlers.yml
Handlers are executed in the order they are defined in the handlers section, not in the order listed in the notify statement. Notifying the same handler multiple times will result in executing the handler only once regardless of how many tasks notify it.
Ansible playbook file with a handler This is achieved using the notify module as shown. Note that the 'notify' name should coincide with the handler name as pointed out, otherwise you will encounter errors in your playbook file. Now let's run the playbook file.
Handlers are executed at the end of the play, once all tasks are finished. In Ansible, handlers are typically used to start, reload, restart, and stop services. If your playbook involves changing configuration files, there is a high chance that you'll need to restart a service so that the changes take effect.
Depending on the size of your plays a better solution might be to use roles. Ansible has some discussion why roles are a good idea.
Tasks go in roles/mystuff/tasks/main.yml
and roles/somethingelse/tasks/main.yml
. You can share handlers between the roles, by creating a role containing only handlers roles/myhandlers/handlers/main.yml
and make both roles depend on the myhandlers role:
roles/mystuff/meta/main.yml
and roles/somethingelse/meta/main.yml
:
---
dependencies:
- myhandlers
More on dependencies in https://docs.ansible.com/ansible/2.5/user_guide/playbooks_reuse_roles.html#role-dependencies
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With