Expose ansible_failed_task in rescue for explicit flush_handlers (#85687)

Fixes #85682
This commit is contained in:
Martin Krizek 2025-08-19 17:29:50 +02:00 committed by GitHub
parent b1fc98c8ad
commit c5ddc93767
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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