From ce6c9befb88dba43420e5c2867af8c4f9aee3ff9 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Tue, 2 Aug 2022 17:21:21 +0200 Subject: [PATCH] Do not allow handlers from dynamic includes to be notified (#78399) * Do not allow handlers from dynamic includes to be notified --- .../fragments/dont-expose-included-handlers.yml | 2 ++ lib/ansible/plugins/strategy/__init__.py | 1 - test/integration/targets/handlers/runme.sh | 5 +++++ .../handlers/test_notify_included-handlers.yml | 3 +++ .../targets/handlers/test_notify_included.yml | 16 ++++++++++++++++ 5 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/dont-expose-included-handlers.yml create mode 100644 test/integration/targets/handlers/test_notify_included-handlers.yml create mode 100644 test/integration/targets/handlers/test_notify_included.yml diff --git a/changelogs/fragments/dont-expose-included-handlers.yml b/changelogs/fragments/dont-expose-included-handlers.yml new file mode 100644 index 00000000000..dd511541c31 --- /dev/null +++ b/changelogs/fragments/dont-expose-included-handlers.yml @@ -0,0 +1,2 @@ +bugfixes: + - Do not allow handlers from dynamic includes to be notified (https://github.com/ansible/ansible/pull/78399) diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py index 4ff03b0a2b3..f33a61268c4 100644 --- a/lib/ansible/plugins/strategy/__init__.py +++ b/lib/ansible/plugins/strategy/__init__.py @@ -1028,7 +1028,6 @@ class StrategyBase: # for every task in each block brought in by the include, add the list # of hosts which included the file to the notified_handlers dict for block in new_blocks: - iterator._play.handlers.append(block) for task in block.block: task_name = task.get_name() display.debug("adding task '%s' included in handler '%s'" % (task_name, handler_name)) diff --git a/test/integration/targets/handlers/runme.sh b/test/integration/targets/handlers/runme.sh index 0c3120ba3e6..ce6af63a648 100755 --- a/test/integration/targets/handlers/runme.sh +++ b/test/integration/targets/handlers/runme.sh @@ -118,3 +118,8 @@ grep out.txt -e "\[WARNING\]: Handler 'handler name with {{ test_var }}' is unus # Test include_role and import_role cannot be used as handlers ansible-playbook test_role_as_handler.yml "$@" 2>&1 | tee out.txt grep out.txt -e "ERROR! Using 'include_role' as a handler is not supported." + +# Test notifying a handler from within include_tasks does not work anymore +ansible-playbook test_notify_included.yml "$@" 2>&1 | tee out.txt +[ "$(grep out.txt -ce 'I was included')" = "1" ] +grep out.txt -e "ERROR! The requested handler 'handler_from_include' was not found in either the main handlers list nor in the listening handlers list" diff --git a/test/integration/targets/handlers/test_notify_included-handlers.yml b/test/integration/targets/handlers/test_notify_included-handlers.yml new file mode 100644 index 00000000000..61fce70f70f --- /dev/null +++ b/test/integration/targets/handlers/test_notify_included-handlers.yml @@ -0,0 +1,3 @@ +- name: handler_from_include + debug: + msg: I was included diff --git a/test/integration/targets/handlers/test_notify_included.yml b/test/integration/targets/handlers/test_notify_included.yml new file mode 100644 index 00000000000..e6125079b1e --- /dev/null +++ b/test/integration/targets/handlers/test_notify_included.yml @@ -0,0 +1,16 @@ +- name: This worked unintentionally, make sure it does not anymore + hosts: localhost + gather_facts: false + tasks: + - command: echo + notify: + - include_handler + + - meta: flush_handlers + + - command: echo + notify: + - handler_from_include + handlers: + - name: include_handler + include_tasks: test_notify_included-handlers.yml