included handlers in playbook with roles - not firing

2,308 views
Skip to first unread message

rmullinnix

unread,
Jul 24, 2015, 3:08:01 PM7/24/15
to Ansible Project
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
---
# This Playbook deploys the components for the webserver
#   Apache, consului and kibana

# setup and deploy apache
- hosts: webservers
  become: yes

  roles:
    - role: common
    - role: apache
    - role: consului
    - role: kibana

  handlers:
    - include: roles/consul/handlers/main.yml
    - include: roles/consul-template/handlers/main.yml

I'm using consul and consul-template to hold the config variables for the components.  I push a change to a consul-template file and expect the handler 'refresh consultemplate' to run, but it does not.

Here is the roles/consul-template/handlers/main.yml file
---
# file: roles/consul-template/handlers/main.yml
- name: refresh consultemplate
  shell: 'kill -1 $(cat /var/run/consul-template.pid)'

- name: install consultemplate
  command: /sbin/insserv -f consul-template

- name: restart consultemplate
  service: name=consul-template state=restarted

And the task in the roles/kibana/tasks/main.yml with the notify
  - name: Add consul-template ctmpl file
    copy: src=kibana.ctmpl dest=/etc/consul-template/ctmpl/kibana.ctmpl
    notify: refresh consultemplate


And the run
TASK [kibana : kibana : Add consul-template ctmpl file] *************************
changed: [XXXXXX]

TASK [kibana : kibana : Add apache mod_proxy to connect to consul] **************
changed: [XXXXXX]

RUNNING HANDLER [apache : restart apache] ***************************************
changed: [XXXXXX]

PLAY RECAP **********************************************************************
XXXXXX     : ok=29   changed=8    unreachable=0    failed=0

So the handler I have in roles/apache/handlers/main.yml works correctly.  It's just the include handlers where the associated role is not in the playbook

Any help would be appreciated.


rmullinnix

unread,
Jul 24, 2015, 5:51:24 PM7/24/15
to Ansible Project, rmull...@yahoo.com
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.

Brian Coca

unread,
Jul 24, 2015, 6:32:09 PM7/24/15
to Ansible Project, rmull...@yahoo.com
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.

------
Brian Coca

rmullinnix

unread,
Jul 24, 2015, 6:45:21 PM7/24/15
to Ansible Project, rmull...@yahoo.com
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.

ZillaYT

unread,
Oct 11, 2016, 10:01:21 PM10/11/16
to Ansible Project
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?
Message has been deleted

tim.che...@gmail.com

unread,
Sep 15, 2018, 3:56:45 AM9/15/18
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages