diff --git a/changelogs/fragments/71306-fix-exit-code-no-failure.yml b/changelogs/fragments/71306-fix-exit-code-no-failure.yml new file mode 100644 index 00000000000..78d59b1900b --- /dev/null +++ b/changelogs/fragments/71306-fix-exit-code-no-failure.yml @@ -0,0 +1,2 @@ +bugfixes: + - Fix an exit code for a non-failing playbook (https://github.com/ansible/ansible/issues/71306) diff --git a/lib/ansible/executor/play_iterator.py b/lib/ansible/executor/play_iterator.py index 781dbee4255..f67f709245c 100644 --- a/lib/ansible/executor/play_iterator.py +++ b/lib/ansible/executor/play_iterator.py @@ -491,7 +491,7 @@ class PlayIterator: else: return not (state.did_rescue and state.fail_state & self.FAILED_ALWAYS == 0) elif state.run_state == self.ITERATING_TASKS and self._check_failed_state(state.tasks_child_state): - cur_block = self._blocks[state.cur_block] + cur_block = state._blocks[state.cur_block] if len(cur_block.rescue) > 0 and state.fail_state & self.FAILED_RESCUE == 0: return False else: diff --git a/test/integration/targets/blocks/issue71306.yml b/test/integration/targets/blocks/issue71306.yml new file mode 100644 index 00000000000..9762f6ee838 --- /dev/null +++ b/test/integration/targets/blocks/issue71306.yml @@ -0,0 +1,16 @@ +- hosts: all + gather_facts: no + tasks: + - block: + - block: + - block: + - name: EXPECTED FAILURE + fail: + when: ansible_host == "host1" + + - debug: + msg: "I am successful!" + run_once: true + rescue: + - debug: + msg: "Attemp 1 failed!" diff --git a/test/integration/targets/blocks/runme.sh b/test/integration/targets/blocks/runme.sh index 69f8d04aa1a..edba0c5275f 100755 --- a/test/integration/targets/blocks/runme.sh +++ b/test/integration/targets/blocks/runme.sh @@ -74,3 +74,12 @@ cat rc_test.out [ "$(grep -c 'Failure in always' rc_test.out )" -eq 1 ] [ "$(grep -c 'DID NOT RUN' rc_test.out )" -eq 0 ] rm -f rc_test.out + +# https://github.com/ansible/ansible/issues/71306 +set +e +exit_code=0 +ansible-playbook -i host1,host2 -vv issue71306.yml > rc_test.out || exit_code=$? +set -e +cat rc_test.out +[ $exit_code -eq 0 ] +rm -f rc_test_out