Multiple handler notifications with same handler name, only one is executed

3,421 views
Skip to first unread message

Jose Chavez

unread,
Feb 11, 2014, 3:26:31 PM2/11/14
to ansible...@googlegroups.com
Hello Ansible devs,

I seem to have hit a regression in Ansible 1.4.3. I have a very simple ansible playbook with one role and a handler file that contains two notification. The role calls notify against the name of the handlers (same name for both) but only the first handler is called. In Ansible 1.4.1 all handlers with the same name would be called. Could this be a bug?

Simple ansible playbook contents below:

playbook file: test_handler_problem.yml
---
- hosts: test-servers
  roles:
    - echo_msg
  tags:
    - test

role: echo_msg
     with tasks main.yml file contents:
       ---

       - name: echo_msg | Display message
         debug: msg="running test to show handler problem"
         changed_when: true
         notify: show handler problem

    with handlers main.yml contents:
      ---
      # Test handlers with same name 1
      - name: show handler problem
      debug: msg="Handler 1 called"

      # Test handlers with same name 2
      - name: show handler problem
      debug: msg="Handler 2 called"

When I execute the playbook I get the following output:

PLAY [test-servers] ***********************************************************

GATHERING FACTS ***************************************************************
ok: [xxx.xx.xxx.xxx]

TASK: [echo_msg | echo_msg | Display message] *********************************
changed: [xxx.xx.xxx.xxx] => {
    "changed": true,
    "item": "",
    "msg": "running test to show handler problem"
}

NOTIFIED: [echo_msg | show handler problem] ***********************************
ok: [xxx.xx.xxx.xxx] => {
    "item": "",
    "msg": "Handler 1 called"
}

PLAY RECAP ********************************************************************
xxx.xx.xxx.xxx             : ok=3    changed=1    unreachable=0    failed=0

Michael DeHaan

unread,
Feb 11, 2014, 9:54:34 PM2/11/14
to ansible...@googlegroups.com
So my original response was:

So this is completely and totally a real thing, because the handlers are stored in a giant dictionary, by name.

We don't make that a failure because if two roles define "restart apache" that shouldn't be a conflict someone has to resolve, nor should Apache restart twice.

Then I read what you said about it being a regression.  I'm not so sure, was it a regression, or was the behavior you suggested happened in 1.4.1 actually a bug?  I'd argue it was, and it got fixed.

(That being said, if you do want multiple handlers and they are slightly different, you can in fact parameterize the name of the handler, and that will work -- but it can't use an inventory-scoped variable to do it.  That's also rarely done.)




--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jose Chavez

unread,
Feb 12, 2014, 12:43:25 PM2/12/14
to ansible...@googlegroups.com
Hi Michael,

Could you give me an example of how I could accomplish this using parameterization? I basically just need to be able to have a role task call multiple notifications which in 1.4.1 was just one notify call to the same named handlers :) I tried specifying multiple notify's in the role task as well but it looks like only one is called there too.

Michael DeHaan

unread,
Feb 12, 2014, 12:46:50 PM2/12/14
to ansible...@googlegroups.com
This is what is available:

tasks:
     - shell: foo {{ my_port }}
       notify: 
           - "some handler {{ my_port }}"

handlers:
      - name: "some handler 22"
        shell: bar
      - name: "some handler 50"
        shell: baz







Jesse Keating

unread,
Feb 12, 2014, 1:04:55 PM2/12/14
to ansible...@googlegroups.com
On 2/12/14, 9:43 AM, Jose Chavez wrote:
> I tried specifying multiple notify's in the role task as well but it
> looks like only one is called there too.

You can notify more than one handler

notify:
- handler 1
- handler 2


-jlk

Jose Chavez

unread,
Feb 12, 2014, 1:24:08 PM2/12/14
to ansible...@googlegroups.com
thanks Jesse. I see where I went wrong.
Reply all
Reply to author
Forward
0 new messages