New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handlers not executed of roles referenced by dependency in combination with conditional role in playbook #83110
Comments
Files identified in the description: None If these files are incorrect, please update the |
This looks like a duplicate of #81013 (comment) / #80925 (which has a porting guide entry, though it focuses on include_role). If the handler didn't have the |
This is the documented behavior. Handlers are play scoped, last one with the same name wins; in this case the last one inserted into the play inherits |
Ok, maybe i didn't understand the docs right, but i can not see the fact that the handlers played scoped besides that all handlers are in global scope and handler definitions included later in the play with identical names overwrites one defined before. As i also mentioned in the linked bug, this behavior changes over time as we do have an Debian 11 with ansible core 2.12.10 and there it works so this is some kind of regression, maybe with #77955. We can workaround this with defining handlers per role which in fact do the same but with different names. |
I feel like this is covered in https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_handlers.html#naming-handlers. But any suggestions on how to improve the docs are always welcome.
That is indeed not mentioned there but it is a general functionality in Ansible and not specific to handlers. Every task and handler within a role imported statically (either via
Please see the comment from @s-hertel above. It is not a regression but a fix for unintended behavior with an entry in the porting guide. Sorry this has caused your playbook to be incompatible a with newer ansible-core version, we try and list changes like this in a changelog and porting guide at least.
You can target a handler from a specific role by using a special form of the handler name in Hope this helps. |
Ok, i see what you mean, the docs are good on this point, but my problem is, that this was not my point 😄
Yes, that true, i was just convinced that this does not apply to handlers.
I've seen the porting guide but honestly i did not think about it which impact this has.
This doesn't work for our case, because of the single source of handler definition.
The hint for the porting guide was a good one, a reminder to read this more carefully, so yes i think this helps. |
You said you work around this by defining handlers per role which do the same but have different names, but could name them the same and notify them as Another fix is to use a Another option is to use dynamic roles ( - name: Test playbook
hosts: server-01,server-02
tasks:
- include_role:
name: test_role_2
- include_role:
name: test_role_1
when: 'inventory_hostname == "server-01"' If using |
Yes, this also works.
Unfortunately this doesn't work.
I think we should generaly consider this as this could also speed up things. |
@MorphBonehunter Ah, sorry, I didn't look closely at #83112, these options could work for OPs reproducer. It looks like #83112 is due to #78661 (the underlying cause of this one too). One more option could be using a variable in the handler name. The tricky part is not all variable sources are available during task-preprocessing, so you'd need to use a variable type with high enough precedence, like role parameters (#20 on https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#understanding-variable-precedence). # roles/dep/handlers/main.yml
- name: Debug output dep {{ handler_id }}
ansible.builtin.debug:
msg: "Debug output dep"
listen: Debug output dep Role parameters can be set in the playbook or a role's meta/main.yml for dependencies like this: # debug.yml
- name: Testcase
gather_facts: false
hosts: localhost
vars:
r_enable: false
roles:
- role: one
handler_id: 1 # this is a role param
- role: two
when: r_enable # roles/two/meta/main.yml
dependencies:
- role: dep
handler_id: 2 # this is also a role param |
Closing as a duplicate of #80925. If you have further questions please stop by IRC or the mailing list:
See this page for a complete and up to date list of communication channels and their purposes: |
Summary
There seems to be an issue with role dependencies in combination with a conditional role in the playbook.
We have a playbook with multiple roles. Role A had a dependency on role B. But both role A and B are also defined in the playbook. They are defined in the order B and then A. And role A has a when condition so it's not executed on all hosts.
The handler of the role to which the dependency points (role B) is not executed on the hosts for which the when condition in the playbook for the role with the dependency (role A) was false. This only happens if the role with the dependency (role A) is defined later in the playbook
This is probably a strange setup which I've changed now, but nevertheless I felt it would be good to report this bug.
This setup used to work in Ansible 6 and stopped working once we upgraded to Ansible 8. It also doesn't work correctly on Ansible 9.4. In the expected result I've put the output of Ansible 6 in the actual result that of Ansible 9.4.0.
Issue Type
Bug Report
Component Name
core
Ansible Version
Configuration
OS / Environment
Ubuntu 20.04
Steps to Reproduce
test_playbook.yml
roles/test_role_1/tasks/main.yml
roles/test_role_1/meta/main.yml
roles/test_role_2/tasks/main.yml
roles/test_role_2/handlers/main.yml
Expected Results
Actual Results
Code of Conduct
The text was updated successfully, but these errors were encountered: