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 00000000000..8e94a6ed34a --- /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 b2be0a1b108..6a76fe57f2b 100644 --- a/lib/ansible/plugins/strategy/linear.py +++ b/lib/ansible/plugins/strategy/linear.py @@ -313,11 +313,7 @@ class StrategyModule(StrategyBase): 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 00000000000..639c9cad06d --- /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 00000000000..08d222724b1 --- /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 e2d521805f8..887a82db47e 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 "$@"