ansible-playbook doesn't run handlers if play has different tag

I have encountered some unexpected ansible-playbook behavior with respect to play tags and role tags. I’m trying to understand if this is an Ansible bug or this is a limitation of using tags, so I followed the recommendation to ask in the forums first.

I am using tags in order to quickly run a single role without (1) running the other roles in the play and (2) running other plays in the same playbook. But I am finding that handlers are not executed when they should be executed.

Consider this simplified playbook (our playbooks have more plays and use more roles):

# site.yml
- hosts: all
  tags: [ play1 ]
  roles:
    - role: role1
      tags: [ role1 ]

When I run: ansible-playbook site.yml -t role1 then:

  • the tasks for role1 are executed (as expected)
  • the changed tasks are shown as “changed” (as expected)
  • handlers for changed tasks aren’t shown or executed (not expected)

The handlers are not even displayed in the ansible-playbook output as “skipped”, they simply don’t appear at all.

However, if I remove all tags from the play (in this case, just the tag play1), then the handlers for role1 are executed!

Versions:

  • ansible 11.4.0 (core 2.19.2)
  • Python 3.13.2

Here is the remaining code to support this example:

# roles/role1/tasks/main.yml
- name: Role 1 Task
  debug:
  changed_when: true
  notify:
    - Role 1 Handler

---

# roles/role1/handlers/main.yml
- name: Role 1 Handler
  debug:

[Edit: @flowerysong saw the relevant bit. Nice work. I didn’t see your post when I started composing mine.]

I’m unable to reproduce your results, having tried to reproduce your given input playbook, role, and handler files. (Thanks, btw, for including all the files! That really helps us help you.)

Here’s my exact file contents, my playbook invocation, and results, copy-n-pasted from my terminal.

utoddl@tango:~/ansible/alphabetical$ while read f ; do printf "### %s ###\n" "$f" ; cat "$f" ; echo ; done < <(find . -type f)
### ./roles/role1/tasks/main.yml ###
---
# ./roles/role1/tasks/main.yml
- name: Role 1 Task
  ansible.builtin.debug:
  #  msg: "role1/tasks/main.yml notifying 'Role 1 Handler'"
  changed_when: true
  notify:
    - Role 1 Handler

### ./roles/role1/handlers/main.yml ###
---
# ./roles/role1/handlers/main.yml
- name: Role 1 Handler
  ansible.builtin.debug:
    msg: "Role 1 Handler ran."

### ./site.yml ###
---
# ./site.yml
- name: Plays with roles and tags
  hosts: all
  tags: [play1]
  roles:
    - role: role1
      tags: [role1]

utoddl@tango:~/ansible/alphabetical$ ansible-playbook site.yml -i localhost, --tags role1

PLAY [Plays with roles and tags] *********************************************************************

TASK [role1 : Role 1 Task] ***************************************************************************
changed: [localhost] => {
    "msg": "Hello world!"
}

RUNNING HANDLER [role1 : Role 1 Handler] *************************************************************
ok: [localhost] => {
    "msg": "Role 1 Handler ran."
}

PLAY RECAP *******************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Thanks @flowerysong for linking to a bug report that is the exact issue I am seeing! From the issue, I can see that a fix is being implemented so I will look forward to that. Not sure if I should mark this discussion thread as resolved, or wait until the fix is out.

I also appreciate @utoddl trying to reproduce the issue. Could it be that your ansible core is at 2.18.x or earlier ?

Yes, it is; 2.18.6 in fact.

Hope this issue gets resolved soon. Cheers!