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
pull/82917/head
Sloane Hertel 2 months ago committed by GitHub
parent 8fec1575e6
commit b639bd1fd4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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).'

@ -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)

@ -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

@ -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

@ -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:

Loading…
Cancel
Save