From 65366f663de7d044f42ae6dd53368fd4c1f88b35 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Wed, 15 Feb 2023 20:32:48 +0100 Subject: [PATCH] Fix conditionally notified include handlers (#79804) (#79807) Fixes #79776 (cherry picked from commit 10eda5801ad11f66985251b5c3de481e7b917d3c) --- .../79776-fix-force_handlers-cond-include.yml | 2 ++ lib/ansible/plugins/strategy/linear.py | 6 +----- test/integration/targets/handlers/79776-handlers.yml | 2 ++ test/integration/targets/handlers/79776.yml | 10 ++++++++++ test/integration/targets/handlers/runme.sh | 2 ++ 5 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/79776-fix-force_handlers-cond-include.yml create mode 100644 test/integration/targets/handlers/79776-handlers.yml create mode 100644 test/integration/targets/handlers/79776.yml diff --git a/changelogs/fragments/79776-fix-force_handlers-cond-include.yml b/changelogs/fragments/79776-fix-force_handlers-cond-include.yml new file mode 100644 index 00000000000000..8e94a6ed34ac18 --- /dev/null +++ b/changelogs/fragments/79776-fix-force_handlers-cond-include.yml @@ -0,0 +1,2 @@ +bugfixes: + - Fix conditionally notifying ``include_tasks` handlers when ``force_handlers`` is used (https://github.com/ansible/ansible/issues/79776) diff --git a/lib/ansible/plugins/strategy/linear.py b/lib/ansible/plugins/strategy/linear.py index dc34e097bbefcd..a3c91c2942072d 100644 --- a/lib/ansible/plugins/strategy/linear.py +++ b/lib/ansible/plugins/strategy/linear.py @@ -314,11 +314,7 @@ def run(self, iterator, play_context): included_tasks.extend(final_block.get_tasks()) for host in hosts_left: - # handlers are included regardless of _hosts so noop - # tasks do not have to be created for lockstep, - # not notified handlers are then simply skipped - # in the PlayIterator - if host in included_file._hosts or is_handler: + if host in included_file._hosts: all_blocks[host].append(final_block) display.debug("done iterating over new_blocks loaded from include file") diff --git a/test/integration/targets/handlers/79776-handlers.yml b/test/integration/targets/handlers/79776-handlers.yml new file mode 100644 index 00000000000000..639c9cad06ddd3 --- /dev/null +++ b/test/integration/targets/handlers/79776-handlers.yml @@ -0,0 +1,2 @@ +- debug: + msg: "Handler for {{ inventory_hostname }}" diff --git a/test/integration/targets/handlers/79776.yml b/test/integration/targets/handlers/79776.yml new file mode 100644 index 00000000000000..08d222724b1d36 --- /dev/null +++ b/test/integration/targets/handlers/79776.yml @@ -0,0 +1,10 @@ +- hosts: A,B + gather_facts: false + force_handlers: true + tasks: + - command: echo + notify: handler1 + when: inventory_hostname == "A" + handlers: + - name: handler1 + include_tasks: 79776-handlers.yml diff --git a/test/integration/targets/handlers/runme.sh b/test/integration/targets/handlers/runme.sh index e2d521805f8daa..887a82db47e714 100755 --- a/test/integration/targets/handlers/runme.sh +++ b/test/integration/targets/handlers/runme.sh @@ -170,3 +170,5 @@ ansible-playbook test_flush_handlers_rescue_always.yml -i inventory.handlers "$@ ansible-playbook test_fqcn_meta_flush_handlers.yml -i inventory.handlers "$@" 2>&1 | tee out.txt grep out.txt -e "handler ran" grep out.txt -e "after flush" + +ansible-playbook 79776.yml -i inventory.handlers "$@"