included handlers in playbook with roles - not firing

I probably have something structured wrong or it’s something simple - but I’m stuck trying to get handlers added to a playbook via include to fire. The handlers from roles that are listed in the playbook fire correctly, but the include ones do not. And the task with the notify has a changed: true.

Running ansible-playbook 2.0.0 (last pull on 7/18) on SLES

Here’s the playbook

`

After playing around some more I’ll see if I can answer my own question.

  • notify is contained within the scope of a play
  • a role within a playbook constitutes a single play
  • notify does not propagate outside the play and will not be picked up by handlers outside of the play (i.e., role)
  • role dependencies will not allow you to propagate the notify across roles - it behaves as two separate, autonomous plays
  • handler names are global but the notify to invoke them is local to the role

I can move the handler to the role and it will execute. I would need to copy the handler to each role that could invoke it or include the other role handlers file within each role that needs it. I tried the include directive in the handlers file within the role so I didn’t have to copy code across multiple roles – didn’t work for me, probably a relative path issue on my part.

I think my best bet is to use post_tasks: within the playbook and execute the refresh regardless of the change state in each respective role.

Please feel free to correct me if I’ve stated things incorrectly.

so to clarify:

* notify is contained within the scope of a play

correct

* a role within a playbook constitutes a single play

incorrect

* notify does not propagate outside the play and will not be picked up by
handlers outside of the play (i.e., role)

correct up to the i.e, which is wrong

* role dependencies will not allow you to propagate the notify across roles
- it behaves as two separate, autonomous plays

incorrect

* handler names are global but the notify to invoke them is local to the
role

first 1/2 is correct (up to 'but'), the 2nd half is incorrect

So now im just going to mostly repeat data you can find here
(expanding a bit):
http://docs.ansible.com/ansible/playbooks_intro.html#handlers-running-operations-on-change

* roles can only exist INSIDE a play so they are always part of a play
and do not define a play themselves.
* a play can have multiple roles and/or tasks, either directly or
indirectly included, through roles, include directive or role
dependencies.
* in a file (playbook) you can have several plays, handlers are local
to each play and not notifiable across plays.
* dependencies should include a role in the same play as the dependent
role, which includes it's handlers.
* handler names must be unique in the play, or they will overwrite
each other, meaning you can only execute one of them.
* any task in a play can notify any handler in the play, except a
latter handler at the end of a play.
* notified handlers get processed between ‘pre_tasks’, ‘roles’,
‘tasks’, and ‘post_tasks’ sections or when you invoke them through
"meta: flush_hanlders".
* running a handler clears it from the notification list, but it can
be notified again by any task, except a latter handler at the end of a
play.

Thanks Brian. I’ve spent most of the day on that link and similar ones. I’ll have to keep tinkering around and learn some more. Everything else has been working well, just got wrapped around the axle with handlers.

The fact that a (more particular) role that includes another (more generic) role can not call the handlers of the more generic role really sucks in ansible. Example of a roles structure

roles/ ansible_galaxy_apache_role/ handlers/main.yml <--- has restart apache handler my_apache_role/ tasks/main.yml <-- includes ansible_galaxy_apache_role, and calls its restart apache handler

A playbook.yml now has

`
roles:

  • my_apache_role
    `

The play fails since it won’t be able to find the restart apache handler. What gives with this?

Hi ZillaYT,

This below works for me:

XXX/roles/foo/handlers/main.yml

  • name: comm_nginx handler
    include: …/…/…/…/comm_nginx/handlers/main.yml

comm_nginx/handlers/main.yml

#Your handlers definition business as usual.

Some ansible-playbook log

RUNNING HANDLER [comm_nginx : print_report_handler] ********************************************************************************************************************************************************

ok: [demo_nginx_1_host] => {

“msg”: “The test is successful.”

}

META: ran handlers

META: ran handlers

Hope this help!

Tim