From b639bd1fd42917fceb1487ec2b5214df4db272c6 Mon Sep 17 00:00:00 2001 From: Sloane Hertel <19572925+s-hertel@users.noreply.github.com> Date: Tue, 26 Mar 2024 14:29:45 -0400 Subject: [PATCH] Allow "role_name : " prefix for notifying handler listen topics (#82854) * Allow role name prefix for handler task listen topics For example, - name: handler name debug: listen: topic1 can be notified using `topic1`, `role : topic1` if the handler is in a standalone or collection role, and `ns.col.role: topic1` if the role is in a collection, the same way handler names work. changelog * fix changelog and tests --- .../fix-role-name-handler-prefix-listen.yml | 2 ++ lib/ansible/plugins/strategy/__init__.py | 11 +++++++++-- .../roles/test_handlers_listen/handlers/main.yml | 4 ++++ .../roles/test_handlers_listen/handlers/main.yml | 5 +++++ .../targets/handlers/test_handlers_listen.yml | 15 +++++++++++++++ 5 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/fix-role-name-handler-prefix-listen.yml create mode 100644 test/integration/targets/handlers/collections/ansible_collections/ns/col/roles/test_handlers_listen/handlers/main.yml 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: