added result sanitation to registered var and to callbacks

removed time display as it only is provided by command module
This commit is contained in:
Brian Coca 2015-07-11 00:47:59 -04:00
parent fdea00880b
commit 1274ce565d
7 changed files with 31 additions and 19 deletions

View file

@ -235,3 +235,4 @@ DEFAULT_SUBSET = None
DEFAULT_SU_PASS = None DEFAULT_SU_PASS = None
VAULT_VERSION_MIN = 1.0 VAULT_VERSION_MIN = 1.0
VAULT_VERSION_MAX = 1.0 VAULT_VERSION_MAX = 1.0
RESULT_SANITIZE = frozenset(['invocation','warnings'])

View file

@ -33,6 +33,7 @@ try:
except ImportError: except ImportError:
HAS_ATFORK=False HAS_ATFORK=False
from ansible import constants as C
from ansible.playbook.handler import Handler from ansible.playbook.handler import Handler
from ansible.playbook.task import Task 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 this task is registering a result, do it now
if result._task.register: 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 # send callbacks, execute other options based on the result status
# FIXME: this should all be cleaned up and probably moved to a sub-function. # FIXME: this should all be cleaned up and probably moved to a sub-function.

View file

@ -19,8 +19,13 @@
from __future__ import (absolute_import, division) from __future__ import (absolute_import, division)
__metaclass__ = type __metaclass__ = type
import json
from ansible import constants as C
__all__ = ["CallbackBase"] __all__ = ["CallbackBase"]
class CallbackBase: class CallbackBase:
''' '''
@ -40,6 +45,16 @@ class CallbackBase:
version = getattr(self, 'CALLBACK_VERSION', 'unknwon') version = getattr(self, 'CALLBACK_VERSION', 'unknwon')
self._display.vvvv('Loaded callback %s of type %s, v%s' % (name, ctype, version)) 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): def set_connection_info(self, conn_info):
pass pass

View file

@ -19,8 +19,6 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import json
from ansible.plugins.callback import CallbackBase from ansible.plugins.callback import CallbackBase
class CallbackModule(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 # finally, remove the exception from the result so it's not shown every time
del result._result['exception'] 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: if result._task.ignore_errors:
self._display.display("...ignoring") self._display.display("...ignoring")
@ -70,9 +68,7 @@ class CallbackModule(CallbackBase):
if 'verbose_always' in result._result: if 'verbose_always' in result._result:
indent = 4 indent = 4
del result._result['verbose_always'] del result._result['verbose_always']
if self._display.verbosity >= 2 and 'delta' in result._result: msg += " => %s" % self._dump_results(result._result, indent=indent)
msg += " [time: %s]" % (result._result['delta'])
msg += " => %s" % json.dumps(result._result, indent=indent, ensure_ascii=False)
self._display.display(msg, color=color) self._display.display(msg, color=color)
def v2_runner_on_skipped(self, result): def v2_runner_on_skipped(self, result):
@ -82,11 +78,11 @@ class CallbackModule(CallbackBase):
if 'verbose_always' in result._result: if 'verbose_always' in result._result:
indent = 4 indent = 4
del result._result['verbose_always'] 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') self._display.display(msg, color='cyan')
def v2_runner_on_unreachable(self, result): 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): def v2_playbook_on_no_hosts_matched(self):
self._display.display("skipping: no hosts matched", color='cyan') self._display.display("skipping: no hosts matched", color='cyan')

View file

@ -82,7 +82,7 @@ class CallbackModule(CallbackBase):
if 'msg' in res._result.keys() and res._result['msg']: if 'msg' in res._result.keys() and res._result['msg']:
subject = res._result['msg'].strip('\r\n').split('\n')[0] subject = res._result['msg'].strip('\r\n').split('\n')[0]
body += 'with the following message:\n\n' + res._result['msg'] + '\n\n' 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) mail(sender=sender, subject=subject, body=body)
def v2_runner_on_unreachable(self, result): def v2_runner_on_unreachable(self, result):

View file

@ -19,8 +19,6 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import json
from ansible.plugins.callback import CallbackBase 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') self._display.display("%s | FAILED! => %s" % (result._host.get_name(), result._result), color='red')
def v2_runner_on_ok(self, result): 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): def v2_runner_on_skipped(self, result):
pass pass

View file

@ -40,22 +40,22 @@ class CallbackModule(CallbackBase):
def runner_on_failed(self, host, res, ignore_errors=False): 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): 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): 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')) self.logger.info('%s ansible-command: task execution SKIPPED; host: %s; message: %s' % (self.hostname,host, 'skipped'))
def runner_on_unreachable(self, host, res): 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): 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): 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): 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)))