summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Cammarata <jimi@sngx.net>2016-03-28 09:02:23 -0400
committerJames Cammarata <jimi@sngx.net>2016-03-28 09:02:23 -0400
commit409bf922179abb407514f2a4f20783efde2624ca (patch)
tree65858eeb127148cfbd3fe3fec4a07de616b2754b
parent0fffb6c60c454f28aa10af2e5c283eca276bc5b3 (diff)
downloadansible-add_block_rescue_task_variables.tar.gz
Adds two variables to facts when a task fails and a rescue block startsadd_block_rescue_task_variables
`ansible_failed_task`: Contains the task data, essentially a serialized view of the Task() object. `ansible_failed_result`: Contains the result of the task failure, which is identical in function to registering the result. Doing so automatically like this saves the user from having to register every result in a block and then trying to figure out which result had the failure Similar to the way try/except/finally work, these variables will not be available in the `always` portion of a block unless there is a corresponding `rescue` first. Fixes #12341
-rw-r--r--lib/ansible/plugins/strategy/__init__.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/ansible/plugins/strategy/__init__.py b/lib/ansible/plugins/strategy/__init__.py
index f8349ea744..925bb9c982 100644
--- a/lib/ansible/plugins/strategy/__init__.py
+++ b/lib/ansible/plugins/strategy/__init__.py
@@ -216,6 +216,20 @@ class StrategyBase:
if iterator.is_failed(host):
self._tqm._failed_hosts[host.name] = True
self._tqm._stats.increment('failures', host.name)
+ else:
+ # otherwise, we grab the current state and if we're iterating on
+ # the rescue portion of a block then we save the failed task in a
+ # special var for use within the rescue/always
+ state, _ = iterator.get_next_task_for_host(host, peek=True)
+ if state.run_state == iterator.ITERATING_RESCUE:
+ original_task = iterator.get_original_task(host, task)
+ self._variable_manager.set_nonpersistent_facts(
+ host,
+ dict(
+ ansible_failed_task=original_task.serialize(),
+ ansible_failed_result=task_result._result,
+ ),
+ )
else:
self._tqm._stats.increment('ok', host.name)
self._tqm.send_callback('v2_runner_on_failed', task_result, ignore_errors=task.ignore_errors)