'How to share handlers?

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


Solution 1:[1]

To moderators. Read my question carefully please. That's the answer to my question. And I'm totally aware that SO is not a forum.

That's a bug in ansible-2.1. The credit goes to udondan who found the issue.

Solution 2:[2]

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/latest/user_guide/playbooks_reuse_roles.html#using-role-dependencies

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 x-yuri
Solution 2 4wk_