Only 1 notification being fire when 2 roles with same handler name assigned to host #4943

Closed
jsmartin opened this Issue Nov 18, 2013 · 4 comments

Comments

Projects
None yet
5 participants
@jsmartin
Contributor

jsmartin commented Nov 18, 2013

2 roles with the same handler named assigned to host, only 1 handler will be executed.

I bisected commits, and found that the problem commit is:

f081c68

Here's my reproducer:

https://github.com/jsmartin/test-ansible.git

ansible-playbook --version
ansible-playbook 1.4 (devel 6dd81f25d9) last updated 2013/11/12 18:50:22 (GMT -400)


PLAY [localhost] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [127.0.0.1]

TASK: [a | test role a echo] ************************************************** 
changed: [127.0.0.1] => {"changed": true, "cmd": ["echo", "role a"], "delta": "0:00:00.004943", "end": "2013-11-12 18:55:26.645837", "item": "", "rc": 0, "start": "2013-11-12 18:55:26.640894", "stderr": "", "stdout": "role a"}

TASK: [b | test role b echo] ************************************************** 
changed: [127.0.0.1] => {"changed": true, "cmd": ["echo", "role b"], "delta": "0:00:00.003273", "end": "2013-11-12 18:55:26.747306", "item": "", "rc": 0, "start": "2013-11-12 18:55:26.744033", "stderr": "", "stdout": "role b"}

NOTIFIED: [a | myhandler] ***************************************************** 
changed: [127.0.0.1] => {"changed": true, "cmd": ["echo", "handler for role A activated"], "delta": "0:00:00.003456", "end": "2013-11-12 18:55:26.849426", "item": "", "rc": 0, "start": "2013-11-12 18:55:26.845970", "stderr": "", "stdout": "handler for role A activated"}

PLAY RECAP ******************************************************************** 
127.0.0.1                  : ok=4    changed=3    unreachable=0    failed=0  

Here you can see only the handler in role "a" was activated

Now let's try with 1.3.4

ansible-playbook --version
ansible-playbook 1.3.4
ansible-playbook -i hosts -v test.yml 

PLAY [localhost] ************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [127.0.0.1]

TASK: [test role a echo] ****************************************************** 
changed: [127.0.0.1] => {"changed": true, "cmd": ["echo", "role a"], "delta": "0:00:00.005104", "end": "2013-11-12 18:57:33.596077", "item": "", "rc": 0, "start": "2013-11-12 18:57:33.590973", "stderr": "", "stdout": "role a"}

TASK: [test role b echo] ****************************************************** 
changed: [127.0.0.1] => {"changed": true, "cmd": ["echo", "role b"], "delta": "0:00:00.004365", "end": "2013-11-12 18:57:33.704722", "item": "", "rc": 0, "start": "2013-11-12 18:57:33.700357", "stderr": "", "stdout": "role b"}

NOTIFIED: [myhandler] ********************************************************* 
changed: [127.0.0.1] => {"changed": true, "cmd": ["echo", "handler for role A activated"], "delta": "0:00:00.004131", "end": "2013-11-12 18:57:33.805843", "item": "", "rc": 0, "start": "2013-11-12 18:57:33.801712", "stderr": "", "stdout": "handler for role A activated"}

NOTIFIED: [myhandler] ********************************************************* 
changed: [127.0.0.1] => {"changed": true, "cmd": ["echo", "handler for role B activated"], "delta": "0:00:00.004174", "end": "2013-11-12 18:57:33.910559", "item": "", "rc": 0, "start": "2013-11-12 18:57:33.906385", "stderr": "", "stdout": "handler for role B activated"}

PLAY RECAP ******************************************************************** 
127.0.0.1                  : ok=5    changed=4    unreachable=0    failed=0   

Here you can see both handlers were activated.

@jctanner

This comment has been minimized.

Show comment
Hide comment
@jctanner

jctanner Nov 18, 2013

Member

workaround ...

index 36788f2..7c1eb68 100644
--- a/lib/ansible/playbook/__init__.py
+++ b/lib/ansible/playbook/__init__.py
@@ -547,6 +547,11 @@ class PlayBook(object):

                                 # Resolve the variables first
                                 handler_name = template(play.basedir, handler.name, handler.module_vars)
+
+                                if hasattr(handler, 'role_name'):
+                                    if handler.role_name is not None:
+                                        handler_name = str(handler.role_name) + '-' + handler_name
+                              
                                 if handler_name not in fired_names:
                                     self._run_task(play, handler, True)
                                 # prevent duplicate handler includes from running more than once
Member

jctanner commented Nov 18, 2013

workaround ...

index 36788f2..7c1eb68 100644
--- a/lib/ansible/playbook/__init__.py
+++ b/lib/ansible/playbook/__init__.py
@@ -547,6 +547,11 @@ class PlayBook(object):

                                 # Resolve the variables first
                                 handler_name = template(play.basedir, handler.name, handler.module_vars)
+
+                                if hasattr(handler, 'role_name'):
+                                    if handler.role_name is not None:
+                                        handler_name = str(handler.role_name) + '-' + handler_name
+                              
                                 if handler_name not in fired_names:
                                     self._run_task(play, handler, True)
                                 # prevent duplicate handler includes from running more than once
@jctanner

This comment has been minimized.

Show comment
Hide comment
@jctanner

jctanner Nov 18, 2013

Member

@mpdehaan could you weigh in on this? I'm not sure if we want to think of handlers as independent with each role or if we want to stick with the current behavior.

Member

jctanner commented Nov 18, 2013

@mpdehaan could you weigh in on this? I'm not sure if we want to think of handlers as independent with each role or if we want to stick with the current behavior.

@mpdehaan

This comment has been minimized.

Show comment
Hide comment
@mpdehaan

mpdehaan Nov 19, 2013

Contributor

It seems that handlers for each name should be unique across roles.

If you want to trigger multiple handlers, handlers are unique by name.

Thus you can parameterize the name of the handler like:

 - name: "I'm a handler for service {{ port }}"
   shell: echo foo

And then:

 notify: "I'm a handler for service {{ port }}"

And things will be good.

This requires Ansible 1.4

Contributor

mpdehaan commented Nov 19, 2013

It seems that handlers for each name should be unique across roles.

If you want to trigger multiple handlers, handlers are unique by name.

Thus you can parameterize the name of the handler like:

 - name: "I'm a handler for service {{ port }}"
   shell: echo foo

And then:

 notify: "I'm a handler for service {{ port }}"

And things will be good.

This requires Ansible 1.4

@mpdehaan mpdehaan closed this Nov 19, 2013

@iat00

This comment has been minimized.

Show comment
Hide comment
@iat00

iat00 Jul 5, 2015

Contributor

I recommend to "clearly" document this requirement in Ansible Documentation :-)

Contributor

iat00 commented Jul 5, 2015

I recommend to "clearly" document this requirement in Ansible Documentation :-)

jimi-c pushed a commit that referenced this issue Dec 6, 2016

Merge pull request #4943 from chouseknecht/devel
Improve error handling in docker_service module

@bochecha bochecha referenced this issue in ideascube/ansiblecube Aug 31, 2017

Closed

WIP WIP WIP #139

@jonathangreen jonathangreen referenced this issue in Islandora-Devops/ansible-role-blazegraph Jan 29, 2018

Closed

Restart tomcat8 failed #2

@ansibot ansibot added bug and removed bug_report labels Mar 6, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment