mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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:
parent
fdea00880b
commit
1274ce565d
7 changed files with 31 additions and 19 deletions
|
@ -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'])
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)))
|
||||
|
|
Loading…
Reference in a new issue