diff --git a/changelogs/fragments/fix-role-name-handler-prefix-listen.yml b/changelogs/fragments/fix-role-name-handler-prefix-listen.yml new file mode 100644 index 00000000000..c065585cc15 --- /dev/null +++ b/changelogs/fragments/fix-role-name-handler-prefix-listen.yml @@ -0,0 +1,2 @@ +bugfixes: + - 'Fix notifying role handlers by listen keyword topics with the "role_name : " prefix (https://github.com/ansible/ansible/issues/82849).' diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py index 2fe9313089e..efd69efe9b4 100644 --- a/lib/ansible/plugins/strategy/__init__.py +++ b/lib/ansible/plugins/strategy/__init__.py @@ -553,12 +553,19 @@ class StrategyBase: seen = [] for handler in handlers: if listeners := handler.listen: - if notification in handler.get_validated_value( + listeners = handler.get_validated_value( 'listen', handler.fattributes.get('listen'), listeners, templar, - ): + ) + if handler._role is not None: + for listener in listeners.copy(): + listeners.extend([ + handler._role.get_name(include_role_fqcn=True) + ' : ' + listener, + handler._role.get_name(include_role_fqcn=False) + ' : ' + listener + ]) + if notification in listeners: if handler.name and handler.name in seen: continue seen.append(handler.name) diff --git a/test/integration/targets/handlers/collections/ansible_collections/ns/col/roles/test_handlers_listen/handlers/main.yml b/test/integration/targets/handlers/collections/ansible_collections/ns/col/roles/test_handlers_listen/handlers/main.yml new file mode 100644 index 00000000000..0658f7e117d --- /dev/null +++ b/test/integration/targets/handlers/collections/ansible_collections/ns/col/roles/test_handlers_listen/handlers/main.yml @@ -0,0 +1,4 @@ +- name: test notifying listen namespaced by collection + role + set_fact: + notify_listen_in_specific_collection_role: True + listen: notify_specific_collection_role_listen diff --git a/test/integration/targets/handlers/roles/test_handlers_listen/handlers/main.yml b/test/integration/targets/handlers/roles/test_handlers_listen/handlers/main.yml index 3bfd82a2546..d5cdbac2825 100644 --- a/test/integration/targets/handlers/roles/test_handlers_listen/handlers/main.yml +++ b/test/integration/targets/handlers/roles/test_handlers_listen/handlers/main.yml @@ -8,3 +8,8 @@ set_fact: notify_listen_in_role_4: True listen: notify_listen_in_role + +- name: test notifying listen namespaced by the role + set_fact: + notify_listen_in_specific_role: True + listen: notify_specific_role_listen diff --git a/test/integration/targets/handlers/test_handlers_listen.yml b/test/integration/targets/handlers/test_handlers_listen.yml index dd2cd87dd9a..16167a3e908 100644 --- a/test/integration/targets/handlers/test_handlers_listen.yml +++ b/test/integration/targets/handlers/test_handlers_listen.yml @@ -99,6 +99,7 @@ gather_facts: false roles: - role: test_handlers_listen + - role: ns.col.test_handlers_listen tasks: - name: test notify handlers listen in roles command: uptime @@ -113,6 +114,20 @@ - "notify_listen_ran_4_3 is defined" - "notify_listen_in_role_4 is defined" - "notify_listen_from_role_4 is defined" + - name: test notifying handlers using the role name prefix + command: uptime + notify: + - 'test_handlers_listen : notify_specific_role_listen' + - 'test_handlers_listen : notify_specific_collection_role_listen' + - meta: flush_handlers + - assert: + that: + - notify_listen_in_specific_collection_role is defined + - notify_listen_in_specific_role is defined + - name: test notifying the collection listener by the role's FQCN also works + command: uptime + notify: + - 'ns.col.test_handlers_listen : notify_specific_collection_role_listen' handlers: - name: notify_listen_ran_4_1 set_fact: