From 5c0b2c151ce7d7f2a4df2f1525d3eb6302a2e790 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Thu, 14 Nov 2019 15:50:54 +0100 Subject: [PATCH] Fix ansible_failed_{task,result} undefined in rescue (#64831) This is a fix for a regression introduced by Perfy. Since then we mainly operate on host.name instead of the Host object. In a call to set_nonpersistent_facts where we set ansible_failed_task and ansible_failed_result variables we were still passing the object which led to those vars being undefined. Fixes #64789 --- .../64789-regression-rescue-vars-not-defined.yml | 2 ++ lib/ansible/plugins/strategy/__init__.py | 2 +- .../targets/blocks/block_rescue_vars.yml | 16 ++++++++++++++++ test/integration/targets/blocks/runme.sh | 2 ++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/64789-regression-rescue-vars-not-defined.yml create mode 100644 test/integration/targets/blocks/block_rescue_vars.yml diff --git a/changelogs/fragments/64789-regression-rescue-vars-not-defined.yml b/changelogs/fragments/64789-regression-rescue-vars-not-defined.yml new file mode 100644 index 00000000000..2ae04ae74b4 --- /dev/null +++ b/changelogs/fragments/64789-regression-rescue-vars-not-defined.yml @@ -0,0 +1,2 @@ +bugfixes: + - Fix regression when ``ansible_failed_task`` and ``ansible_failed_result`` are not defined in the rescue block (https://github.com/ansible/ansible/issues/64789) diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py index 5f6974cea09..2275e5590d1 100644 --- a/lib/ansible/plugins/strategy/__init__.py +++ b/lib/ansible/plugins/strategy/__init__.py @@ -510,7 +510,7 @@ class StrategyBase: if state and iterator.get_active_state(state).run_state == iterator.ITERATING_RESCUE: self._tqm._stats.increment('rescued', original_host.name) self._variable_manager.set_nonpersistent_facts( - original_host, + original_host.name, dict( ansible_failed_task=original_task.serialize(), ansible_failed_result=task_result._result, diff --git a/test/integration/targets/blocks/block_rescue_vars.yml b/test/integration/targets/blocks/block_rescue_vars.yml new file mode 100644 index 00000000000..404f7a37923 --- /dev/null +++ b/test/integration/targets/blocks/block_rescue_vars.yml @@ -0,0 +1,16 @@ +- hosts: localhost + gather_facts: no + tasks: + - block: + - name: EXPECTED FAILURE + fail: + rescue: + - name: Assert that ansible_failed_task is defined + assert: + that: + - ansible_failed_task is defined + + - name: Assert that ansible_failed_result is defined + assert: + that: + - ansible_failed_result is defined diff --git a/test/integration/targets/blocks/runme.sh b/test/integration/targets/blocks/runme.sh index 3384bb9d9bb..f158735611f 100755 --- a/test/integration/targets/blocks/runme.sh +++ b/test/integration/targets/blocks/runme.sh @@ -37,3 +37,5 @@ env python -c \ 'import sys, re; sys.stdout.write(re.sub("\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]", "", sys.stdin.read()))' \ block_test_wo_colors.out [ "$(grep -c 'TEST COMPLETE' block_test.out)" = "$(grep -E '^[0-9]+ plays in' block_test_wo_colors.out | cut -f1 -d' ')" ] + +ansible-playbook -vv block_rescue_vars.yml