From a99f2fd52fa9a45bade4da7230aeba8a96516d23 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Tue, 10 Nov 2015 10:14:30 -0500 Subject: [PATCH] Save delegated vars in result for use in callback methods Fixes #13014 --- lib/ansible/executor/task_executor.py | 11 ++++++++ lib/ansible/plugins/callback/default.py | 37 +++++++++++++++++-------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py index a80ee00373d..36c15381418 100644 --- a/lib/ansible/executor/task_executor.py +++ b/lib/ansible/executor/task_executor.py @@ -461,6 +461,17 @@ class TaskExecutor: if self._task.notify is not None: result['_ansible_notify'] = self._task.notify + # add the delegated vars to the result, so we can reference them + # on the results side without having to do any further templating + # FIXME: we only want a limited set of variables here, so this is currently + # hardcoded but should be possibly fixed if we want more or if + # there is another source of truth we can use + delegated_vars = variables.get('ansible_delegated_vars', dict()).get(self._task.delegate_to, dict()).copy() + if len(delegated_vars) > 0: + result["_ansible_delegated_vars"] = dict() + for k in ('ansible_host', ): + result["_ansible_delegated_vars"][k] = delegated_vars.get(k) + # preserve no_log setting result["_ansible_no_log"] = self._play_context.no_log diff --git a/lib/ansible/plugins/callback/default.py b/lib/ansible/plugins/callback/default.py index 9fd185ccf24..611244760d1 100644 --- a/lib/ansible/plugins/callback/default.py +++ b/lib/ansible/plugins/callback/default.py @@ -34,6 +34,7 @@ class CallbackModule(CallbackBase): CALLBACK_NAME = 'default' def v2_runner_on_failed(self, result, ignore_errors=False): + delegated_vars = result._result.get('_ansible_delegated_vars', None) if 'exception' in result._result: if self._display.verbosity < 3: # extract just the actual error message from the exception text @@ -50,8 +51,8 @@ class CallbackModule(CallbackBase): if result._task.loop and 'results' in result._result: self._process_items(result) else: - if result._task.delegate_to: - self._display.display("fatal: [%s -> %s]: FAILED! => %s" % (result._host.get_name(), result._task.delegate_to, self._dump_results(result._result)), color='red') + if delegated_vars: + self._display.display("fatal: [%s -> %s]: FAILED! => %s" % (result._host.get_name(), delegated_vars['ansible_host'], self._dump_results(result._result)), color='red') else: self._display.display("fatal: [%s]: FAILED! => %s" % (result._host.get_name(), self._dump_results(result._result)), color='red') @@ -60,17 +61,18 @@ class CallbackModule(CallbackBase): def v2_runner_on_ok(self, result): + delegated_vars = result._result.get('_ansible_delegated_vars', None) if result._task.action == 'include': return elif result._result.get('changed', False): - if result._task.delegate_to is not None: - msg = "changed: [%s -> %s]" % (result._host.get_name(), result._task.delegate_to) + if delegated_vars: + msg = "changed: [%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host']) else: msg = "changed: [%s]" % result._host.get_name() color = 'yellow' else: - if result._task.delegate_to is not None: - msg = "ok: [%s -> %s]" % (result._host.get_name(), result._task.delegate_to) + if delegated_vars: + msg = "ok: [%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host']) else: msg = "ok: [%s]" % result._host.get_name() color = 'green' @@ -96,8 +98,9 @@ class CallbackModule(CallbackBase): self._display.display(msg, color='cyan') def v2_runner_on_unreachable(self, result): - if result._task.delegate_to: - self._display.display("fatal: [%s -> %s]: UNREACHABLE! => %s" % (result._host.get_name(), result._task.delegate_to, self._dump_results(result._result)), color='red') + delegated_vars = result._result.get('_ansible_delegated_vars', None) + if delegated_vars: + self._display.display("fatal: [%s -> %s]: UNREACHABLE! => %s" % (result._host.get_name(), delegated_vars['ansible_host'], self._dump_results(result._result)), color='red') else: self._display.display("fatal: [%s]: UNREACHABLE! => %s" % (result._host.get_name(), self._dump_results(result._result)), color='red') @@ -135,13 +138,20 @@ class CallbackModule(CallbackBase): def v2_playbook_item_on_ok(self, result): + delegated_vars = result._result.get('_ansible_delegated_vars', None) if result._task.action == 'include': return elif result._result.get('changed', False): - msg = "changed: [%s]" % result._host.get_name() + if delegated_vars: + msg = "changed: [%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host']) + else: + msg = "changed: [%s]" % result._host.get_name() color = 'yellow' else: - msg = "ok: [%s]" % result._host.get_name() + if delegated_vars: + msg = "ok: [%s -> %s]" % (result._host.get_name(), delegated_vars['ansible_host']) + else: + msg = "ok: [%s]" % result._host.get_name() color = 'green' msg += " => (item=%s)" % (result._result['item'],) @@ -151,6 +161,7 @@ class CallbackModule(CallbackBase): self._display.display(msg, color=color) def v2_playbook_item_on_failed(self, result): + delegated_vars = result._result.get('_ansible_delegated_vars', None) if 'exception' in result._result: if self._display.verbosity < 3: # extract just the actual error message from the exception text @@ -164,7 +175,11 @@ class CallbackModule(CallbackBase): # finally, remove the exception from the result so it's not shown every time del result._result['exception'] - self._display.display("failed: [%s] => (item=%s) => %s" % (result._host.get_name(), result._result['item'], self._dump_results(result._result)), color='red') + if delegated_vars: + self._display.display("failed: [%s -> %s] => (item=%s) => %s" % (result._host.get_name(), delegated_vars['ansible_host'], result._result['item'], self._dump_results(result._result)), color='red') + else: + self._display.display("failed: [%s] => (item=%s) => %s" % (result._host.get_name(), result._result['item'], self._dump_results(result._result)), color='red') + self._handle_warnings(result._result) def v2_playbook_item_on_skipped(self, result):