Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share handlers?

Tags:

ansible

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
like image 690
x-yuri Avatar asked Jul 04 '16 16:07

x-yuri


People also ask

Can a handler notify another handler?

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.

How do you call a handler inside a playbook?

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.

Where do you put handlers?

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.


1 Answers

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

like image 166
Robin Roth Avatar answered Oct 14 '22 18:10

Robin Roth