From 1274ce565dbbd302aef3cbc8de84055b6d549558 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Sat, 11 Jul 2015 00:47:59 -0400 Subject: [PATCH] added result sanitation to registered var and to callbacks removed time display as it only is provided by command module --- lib/ansible/constants.py | 1 + lib/ansible/executor/process/result.py | 4 +++- lib/ansible/plugins/callback/__init__.py | 15 +++++++++++++++ lib/ansible/plugins/callback/default.py | 12 ++++-------- lib/ansible/plugins/callback/mail.py | 2 +- lib/ansible/plugins/callback/minimal.py | 4 +--- lib/ansible/plugins/callback/syslog_json.py | 12 ++++++------ 7 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/ansible/constants.py b/lib/ansible/constants.py index 43ae782e19..5b7c901415 100644 --- a/lib/ansible/constants.py +++ b/lib/ansible/constants.py @@ -235,3 +235,4 @@ DEFAULT_SUBSET = None DEFAULT_SU_PASS = None VAULT_VERSION_MIN = 1.0 VAULT_VERSION_MAX = 1.0 +RESULT_SANITIZE = frozenset(['invocation','warnings']) diff --git a/lib/ansible/executor/process/result.py b/lib/ansible/executor/process/result.py index 505457f7d2..71d6746be0 100644 --- a/lib/ansible/executor/process/result.py +++ b/lib/ansible/executor/process/result.py @@ -33,6 +33,7 @@ try: except ImportError: HAS_ATFORK=False +from ansible import constants as C from ansible.playbook.handler import Handler from ansible.playbook.task import Task @@ -107,7 +108,8 @@ class ResultProcess(multiprocessing.Process): # if this task is registering a result, do it now if result._task.register: - self._send_result(('register_host_var', result._host, result._task.register, result._result)) + res = {k: result._result[k] for k in set(result._result.keys()).difference(C.RESULT_SANITIZE)} + self._send_result(('register_host_var', result._host, result._task.register, res)) # send callbacks, execute other options based on the result status # FIXME: this should all be cleaned up and probably moved to a sub-function. diff --git a/lib/ansible/plugins/callback/__init__.py b/lib/ansible/plugins/callback/__init__.py index 17a6606fb8..a5a13c1cff 100644 --- a/lib/ansible/plugins/callback/__init__.py +++ b/lib/ansible/plugins/callback/__init__.py @@ -19,8 +19,13 @@ from __future__ import (absolute_import, division) __metaclass__ = type +import json + +from ansible import constants as C + __all__ = ["CallbackBase"] + class CallbackBase: ''' @@ -40,6 +45,16 @@ class CallbackBase: version = getattr(self, 'CALLBACK_VERSION', 'unknwon') self._display.vvvv('Loaded callback %s of type %s, v%s' % (name, ctype, version)) + def _dump_results(self, result, sanitize=True, indent=4, sort_keys=True): + if sanitize: + res = self._sanitize_result(result) + else: + res = results + return json.dumps(res, indent=indent, ensure_ascii=False, sort_keys=sort_keys) + + def _sanitize_result(self, result): + return {k: result[k] for k in set(result.keys()).difference(C.RESULT_SANITIZE)} + def set_connection_info(self, conn_info): pass diff --git a/lib/ansible/plugins/callback/default.py b/lib/ansible/plugins/callback/default.py index 5292b74c00..2bbc697f53 100644 --- a/lib/ansible/plugins/callback/default.py +++ b/lib/ansible/plugins/callback/default.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.plugins.callback import CallbackBase class CallbackModule(CallbackBase): @@ -48,7 +46,7 @@ class CallbackModule(CallbackBase): # finally, remove the exception from the result so it's not shown every time del result._result['exception'] - self._display.display("fatal: [%s]: FAILED! => %s" % (result._host.get_name(), json.dumps(result._result, ensure_ascii=False)), color='red') + self._display.display("fatal: [%s]: FAILED! => %s" % (result._host.get_name(), self._dump_results(result._result)), color='red') if result._task.ignore_errors: self._display.display("...ignoring") @@ -70,9 +68,7 @@ class CallbackModule(CallbackBase): if 'verbose_always' in result._result: indent = 4 del result._result['verbose_always'] - if self._display.verbosity >= 2 and 'delta' in result._result: - msg += " [time: %s]" % (result._result['delta']) - msg += " => %s" % json.dumps(result._result, indent=indent, ensure_ascii=False) + msg += " => %s" % self._dump_results(result._result, indent=indent) self._display.display(msg, color=color) def v2_runner_on_skipped(self, result): @@ -82,11 +78,11 @@ class CallbackModule(CallbackBase): if 'verbose_always' in result._result: indent = 4 del result._result['verbose_always'] - msg += " => %s" % json.dumps(result._result, indent=indent, ensure_ascii=False) + msg += " => %s" % self._dump_results(result._result, indent=indent) self._display.display(msg, color='cyan') def v2_runner_on_unreachable(self, result): - self._display.display("fatal: [%s]: UNREACHABLE! => %s" % (result._host.get_name(), result._result), color='red') + self._display.display("fatal: [%s]: UNREACHABLE! => %s" % (result._host.get_name(), self._dump_results(result._result)), color='red') def v2_playbook_on_no_hosts_matched(self): self._display.display("skipping: no hosts matched", color='cyan') diff --git a/lib/ansible/plugins/callback/mail.py b/lib/ansible/plugins/callback/mail.py index af86e61df9..4828062df9 100644 --- a/lib/ansible/plugins/callback/mail.py +++ b/lib/ansible/plugins/callback/mail.py @@ -82,7 +82,7 @@ class CallbackModule(CallbackBase): if 'msg' in res._result.keys() and res._result['msg']: subject = res._result['msg'].strip('\r\n').split('\n')[0] body += 'with the following message:\n\n' + res._result['msg'] + '\n\n' - body += 'A complete dump of the error:\n\n' + json.dumps(res._result, indent=4) + body += 'A complete dump of the error:\n\n' + self._dump_results(res._result) mail(sender=sender, subject=subject, body=body) def v2_runner_on_unreachable(self, result): diff --git a/lib/ansible/plugins/callback/minimal.py b/lib/ansible/plugins/callback/minimal.py index d5950fae01..86e5694a15 100644 --- a/lib/ansible/plugins/callback/minimal.py +++ b/lib/ansible/plugins/callback/minimal.py @@ -19,8 +19,6 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -import json - from ansible.plugins.callback import CallbackBase @@ -55,7 +53,7 @@ class CallbackModule(CallbackBase): self._display.display("%s | FAILED! => %s" % (result._host.get_name(), result._result), color='red') def v2_runner_on_ok(self, result): - self._display.display("%s | SUCCESS => %s" % (result._host.get_name(), json.dumps(result._result, indent=4)), color='green') + self._display.display("%s | SUCCESS => %s" % (result._host.get_name(), self._dump_results(result._result)), color='green') def v2_runner_on_skipped(self, result): pass diff --git a/lib/ansible/plugins/callback/syslog_json.py b/lib/ansible/plugins/callback/syslog_json.py index fe0281b780..991a94dd31 100644 --- a/lib/ansible/plugins/callback/syslog_json.py +++ b/lib/ansible/plugins/callback/syslog_json.py @@ -40,22 +40,22 @@ class CallbackModule(CallbackBase): def runner_on_failed(self, host, res, ignore_errors=False): - self.logger.error('%s ansible-command: task execution FAILED; host: %s; message: %s' % (self.hostname,host,json.dumps(res, sort_keys=True))) + self.logger.error('%s ansible-command: task execution FAILED; host: %s; message: %s' % (self.hostname,host,self._dump_results(res))) def runner_on_ok(self, host, res): - self.logger.info('%s ansible-command: task execution OK; host: %s; message: %s' % (self.hostname,host,json.dumps(res, sort_keys=True))) + self.logger.info('%s ansible-command: task execution OK; host: %s; message: %s' % (self.hostname,host,self._dump_results(res))) def runner_on_skipped(self, host, item=None): self.logger.info('%s ansible-command: task execution SKIPPED; host: %s; message: %s' % (self.hostname,host, 'skipped')) def runner_on_unreachable(self, host, res): - self.logger.error('%s ansible-command: task execution UNREACHABLE; host: %s; message: %s' % (self.hostname,host,json.dumps(res, sort_keys=True))) + self.logger.error('%s ansible-command: task execution UNREACHABLE; host: %s; message: %s' % (self.hostname,host,self._dump_results(res))) def runner_on_async_failed(self, host, res): - self.logger.error('%s ansible-command: task execution FAILED; host: %s; message: %s' % (self.hostname,host,json.dumps(res, sort_keys=True))) + self.logger.error('%s ansible-command: task execution FAILED; host: %s; message: %s' % (self.hostname,host,self._dump_results(res))) def playbook_on_import_for_host(self, host, imported_file): - self.logger.info('%s ansible-command: playbook IMPORTED; host: %s; message: %s' % (self.hostname,host,json.dumps(res, sort_keys=True))) + self.logger.info('%s ansible-command: playbook IMPORTED; host: %s; message: %s' % (self.hostname,host,self._dump_results(res))) def playbook_on_not_import_for_host(self, host, missing_file): - self.logger.info('%s ansible-command: playbook NOT IMPORTED; host: %s; message: %s' % (self.hostname,host,json.dumps(res, sort_keys=True))) + self.logger.info('%s ansible-command: playbook NOT IMPORTED; host: %s; message: %s' % (self.hostname,host,self._dump_results(res)))