Discussion:
[ansible-project] included handlers in playbook with roles - not firing
'rmullinnix' via Ansible Project
2015-07-24 19:07:29 UTC
Permalink
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.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/623d598c-98c3-468f-9ac6-9b7faef9496e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'rmullinnix' via Ansible Project
2015-07-24 21:51:24 UTC
Permalink
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.
Post by 'rmullinnix' via 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,
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
- role: common
- role: apache
- role: consului
- role: kibana
- 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.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c3921721-896d-431a-9e70-3b0d5d44d286%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-07-24 22:32:05 UTC
Permalink
Post by 'rmullinnix' via Ansible Project
* notify is contained within the scope of a play
correct
Post by 'rmullinnix' via Ansible Project
* a role within a playbook constitutes a single play
incorrect
Post by 'rmullinnix' via Ansible Project
* 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
Post by 'rmullinnix' via Ansible Project
* role dependencies will not allow you to propagate the notify across roles
- it behaves as two separate, autonomous plays
incorrect
Post by 'rmullinnix' via Ansible Project
* 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
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8kfAeTUK6B5gQ1558zfrRJzT3NqHoBed_g05bKiF3c-_A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
'rmullinnix' via Ansible Project
2015-07-24 22:45:21 UTC
Permalink
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.
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9153709a-79be-4175-b120-4eb5b6a574b0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...