# Problem with role handlers namespacing causing two handlers with same name to both be called

**URL:** <https://forum.ansible.com/t/problem-with-role-handlers-namespacing-causing-two-handlers-with-same-name-to-both-be-called/15916>\
**Category:** Ansible Project\
**Created:** [August 11, 2014, 9:25pm UTC](https://forum.ansible.com/t/problem-with-role-handlers-namespacing-causing-two-handlers-with-same-name-to-both-be-called/15916 "2014-08-11T21:25:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean-Francois\_Chevre](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jean-Francois\_Chevre](https://forum.ansible.com/u/Jean-Francois_Chevre)\
**Post date:** [August 11, 2014, 9:25pm UTC](https://forum.ansible.com/t/problem-with-role-handlers-namespacing-causing-two-handlers-with-same-name-to-both-be-called/15916/1 "2014-08-11T21:25:03Z")

</div>

Hey everyone,

I’m having an issue where two handlers with the same name from two different roles are both being called during a playbook run.

Gist here: [https://gist.github.com/jfchevrette/b8f5edcd94b32284d53f](https://gist.github.com/jfchevrette/b8f5edcd94b32284d53f)

I have two roles: first and second

First role has a handler called ‘restart xyz’  
Second role has a handler also called ‘restart xyz’

Second role has a handler called ‘reconfigure xyz’ which then notifies ‘restart xyz’

(Real-life scenario: suppose I have two roles for two applications and both require a restart of my webserver while one of them requires a reconfiguration before the restart)

Since roles are read alphabetically (AFAIK), I’m expecting the ‘restart xyz’ to be called from the first role once handler ‘reconfigure xyz’ notifies it from the second role.

If I only call the ‘second’ role using it’s tag (defined in my playbook), the “restart xyz” handler will be called twice (once from the ‘second’ role handlers, then from the ‘first’ role handlers). I would expect it to be called only once (from the ‘first’ role) as I’ve been told it’s a global namespace for handlers.

(Real-life scenario: I only want to ensure my second application is configured properly, which then reconfigures my webserver and restart it. Both restart handlers are called and my webserver is restarted twice)

I hope my example is not too confusing.

I poked around in the source code but couldn’t find anything obvious.

Should I open a bug report?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 11, 2014, 10:01pm UTC](https://forum.ansible.com/t/problem-with-role-handlers-namespacing-causing-two-handlers-with-same-name-to-both-be-called/15916/2 "2014-08-11T22:01:51Z")

</div>

Was this your ticket?

[https://github.com/ansible/ansible/issues/8553](https://github.com/ansible/ansible/issues/8553)

If so, see the comment I closed it with for some more info.

If not, let me know.

---

<div class="post-metadata">

**Author:** ![Jean-Francois\_Chevre](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jean-Francois\_Chevre](https://forum.ansible.com/u/Jean-Francois_Chevre)\
**Post date:** [August 11, 2014, 10:22pm UTC](https://forum.ansible.com/t/problem-with-role-handlers-namespacing-causing-two-handlers-with-same-name-to-both-be-called/15916/3 "2014-08-11T22:22:16Z")

</div>

Yes it is and as suggested I posted it to the groups.

I had failed to describe that the issue is that both handlers with the same name are called even though there is only one caller. (one handler notifying ‘restart xyz’ and BOTH ‘restart xyz’ handlers are called)

Thanks Michael

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 12, 2014, 11:25am UTC](https://forum.ansible.com/t/problem-with-role-handlers-namespacing-causing-two-handlers-with-same-name-to-both-be-called/15916/4 "2014-08-12T11:25:01Z")

</div>

Thanks I’ll take a look at this today and see what I can find out.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 12, 2014, 2:57pm UTC](https://forum.ansible.com/t/problem-with-role-handlers-namespacing-causing-two-handlers-with-same-name-to-both-be-called/15916/5 "2014-08-12T14:57:02Z")

</div>

I just tested this on devel and 1.7 and in both I do not see handlers with the same names resulting in duplicate handler evaluation.

I’d like to see a minimal playbook (probably shouldn’t install an app, just demonstrate the issue with things like “shell: echo” and dummy handlers) that can reproduce this if possible.

---

<div class="post-metadata">

**Author:** ![Jean-Francois\_Chevre](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jean-Francois\_Chevre](https://forum.ansible.com/u/Jean-Francois_Chevre)\
**Post date:** [August 12, 2014, 5:02pm UTC](https://forum.ansible.com/t/problem-with-role-handlers-namespacing-causing-two-handlers-with-same-name-to-both-be-called/15916/6 "2014-08-12T17:02:09Z")

</div>

Here you go: [https://www.dropbox.com/s/qhooyvbzogdcjln/ansible-notify-bug.tar.gz](https://www.dropbox.com/s/qhooyvbzogdcjln/ansible-notify-bug.tar.gz)

This uses a local connection and ensures content of two files in /tmp/

ansible-playbook demo.yml -i inventory -l localhost -t first

- Works fine

ansible-playbook demo.yml -i inventory -l localhost -t second

- You will see handler “restart xyz” run twice, once from role ‘second’ and once from role ‘first’

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 12, 2014, 7:07pm UTC](https://forum.ansible.com/t/problem-with-role-handlers-namespacing-causing-two-handlers-with-same-name-to-both-be-called/15916/7 "2014-08-12T19:07:24Z")

</div>

I’m having some trouble with CLI downloads from dropbox, might it be possible to host a github repo or gist?

---

<div class="post-metadata">

**Author:** ![Jean-Francois\_Chevre](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jean-Francois\_Chevre](https://forum.ansible.com/u/Jean-Francois_Chevre)\
**Post date:** [August 13, 2014, 12:38pm UTC](https://forum.ansible.com/t/problem-with-role-handlers-namespacing-causing-two-handlers-with-same-name-to-both-be-called/15916/8 "2014-08-13T12:38:03Z")

</div>

There you go Michael: [https://github.com/jfchevrette/ansible-notify-bug](https://github.com/jfchevrette/ansible-notify-bug)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 13, 2014, 1:16pm UTC](https://forum.ansible.com/t/problem-with-role-handlers-namespacing-causing-two-handlers-with-same-name-to-both-be-called/15916/9 "2014-08-13T13:16:23Z")

</div>

I’m actually wondering if this is bad historical memory on my part.

One possible use case is if every role has a handler called “restart service” and doesn’t name it well.

So what we do here in Ansible (pardon the code is):

if task.role\_name:

name = ‘%s | %s’ % (task.role\_name, [task.name](http://task.name))

else:

name = [task.name](http://task.name)

We prefix internally, each task name with the name of the role.

This is somewhat of a “damned if you, damned if you don’t”, scenario, as both cases are pretty valid.

Thoughts?

I kind of feel the namespacing protection in the system is a good thing, though I seem to have forgotten us acquiring that property. Ideas welcome.
