From 764070de695eaffff92149852f57cc5acab7d8ea Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Wed, 3 Sep 2025 01:20:30 +0200 Subject: [PATCH] [stable-2.19] Expose ansible_failed_task in rescue for explicit flush_handlers (#85687) (#85707) Fixes #85682 (cherry picked from commit c5ddc9376765f99f0f02ebe6111d1ad99374087c) --- .../fragments/85682-rescue-flush_handlers.yml | 2 ++ lib/ansible/executor/play_iterator.py | 2 +- .../targets/handlers/rescue_flush_handlers.yml | 16 ++++++++++++++++ test/integration/targets/handlers/runme.sh | 2 ++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/85682-rescue-flush_handlers.yml create mode 100644 test/integration/targets/handlers/rescue_flush_handlers.yml diff --git a/changelogs/fragments/85682-rescue-flush_handlers.yml b/changelogs/fragments/85682-rescue-flush_handlers.yml new file mode 100644 index 00000000000..115dd4b5faf --- /dev/null +++ b/changelogs/fragments/85682-rescue-flush_handlers.yml @@ -0,0 +1,2 @@ +bugfixes: + - The ``ansible_failed_task`` variable is now correctly exposed in a rescue section, even when a failing handler is triggered by the ``flush_handlers`` task in the corresponding ``block`` (https://github.com/ansible/ansible/issues/85682) diff --git a/lib/ansible/executor/play_iterator.py b/lib/ansible/executor/play_iterator.py index 69d0b00b0e7..de0c5f78d1b 100644 --- a/lib/ansible/executor/play_iterator.py +++ b/lib/ansible/executor/play_iterator.py @@ -574,7 +574,7 @@ class PlayIterator: Given the current HostState state, determines if the current block, or any child blocks, are in rescue mode. """ - if state.run_state == IteratingStates.TASKS and state.get_current_block().rescue: + if state.run_state in (IteratingStates.TASKS, IteratingStates.HANDLERS) and state.get_current_block().rescue: return True if state.tasks_child_state is not None: return self.is_any_block_rescuing(state.tasks_child_state) diff --git a/test/integration/targets/handlers/rescue_flush_handlers.yml b/test/integration/targets/handlers/rescue_flush_handlers.yml new file mode 100644 index 00000000000..065743654a8 --- /dev/null +++ b/test/integration/targets/handlers/rescue_flush_handlers.yml @@ -0,0 +1,16 @@ +- hosts: localhost + gather_facts: false + tasks: + - block: + - debug: + changed_when: true + notify: h1 + + - meta: flush_handlers + rescue: + - assert: + that: + - ansible_failed_task is defined + handlers: + - name: h1 + fail: diff --git a/test/integration/targets/handlers/runme.sh b/test/integration/targets/handlers/runme.sh index 0cc7b3c36ca..648eb87bb91 100755 --- a/test/integration/targets/handlers/runme.sh +++ b/test/integration/targets/handlers/runme.sh @@ -230,3 +230,5 @@ ansible-playbook handler_notify_earlier_handler.yml "$@" 2>&1 | tee out.txt ANSIBLE_DEBUG=1 ansible-playbook tagged_play.yml --skip-tags the_whole_play "$@" 2>&1 | tee out.txt [ "$(grep out.txt -ce 'META: triggered running handlers')" = "0" ] [ "$(grep out.txt -ce 'handler_ran')" = "0" ] + +ansible-playbook rescue_flush_handlers.yml "$@"