Implement display_ok_hosts and display_skipped_hosts for unixy (#53179)

* Implement display_ok_hosts and display_skipped_hosts like default callback

* Implement show_custom_stats and display_failed_stderr like default callback

* Fix pep8

* Clarify conditional

* Changelog fragment

* Import from defaults.py per feedback
This commit is contained in:
akatch 2019-06-21 10:22:16 -05:00 committed by Brian Coca
parent 7b9d991f78
commit 200eed6177
2 changed files with 37 additions and 14 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- Implement config options for ``display_ok_hosts`` and ``display_skipped_hosts`` in unixy callback plugin

View file

@ -21,15 +21,14 @@ DOCUMENTATION = '''
'''
from os.path import basename
from ansible import constants as C
from ansible import context
from ansible.module_utils._text import to_text
from ansible.plugins.callback import CallbackBase
from ansible.utils.color import colorize, hostcolor
from ansible.plugins.callback.default import CallbackModule as CallbackModule_default
class CallbackModule(CallbackBase):
class CallbackModule(CallbackModule_default):
'''
Design goals:
@ -48,6 +47,9 @@ class CallbackModule(CallbackBase):
CALLBACK_TYPE = 'stdout'
CALLBACK_NAME = 'unixy'
def _run_is_verbose(self, result):
return ((self._display.verbosity > 0 or '_ansible_verbose_always' in result._result) and '_ansible_verbose_override' not in result._result)
def _get_task_display_name(self, task):
self.task_display_name = None
display_name = task.get_name().strip().split(" : ")
@ -60,7 +62,7 @@ class CallbackModule(CallbackBase):
def _preprocess_result(self, result):
self.delegated_vars = result._result.get('_ansible_delegated_vars', None)
self._handle_exception(result._result)
self._handle_exception(result._result, use_stderr=self.display_failed_stderr)
self._handle_warnings(result._result)
def _process_result_output(self, result, msg):
@ -106,12 +108,15 @@ class CallbackModule(CallbackBase):
self._display.display(msg)
def v2_runner_on_skipped(self, result, ignore_errors=False):
self._preprocess_result(result)
display_color = C.COLOR_SKIP
msg = "skipped"
if self.display_skipped_hosts:
self._preprocess_result(result)
display_color = C.COLOR_SKIP
msg = "skipped"
task_result = self._process_result_output(result, msg)
self._display.display(" " + task_result, display_color)
task_result = self._process_result_output(result, msg)
self._display.display(" " + task_result, display_color)
else:
return
def v2_runner_on_failed(self, result, ignore_errors=False):
self._preprocess_result(result)
@ -119,7 +124,7 @@ class CallbackModule(CallbackBase):
msg = "failed"
task_result = self._process_result_output(result, msg)
self._display.display(" " + task_result, display_color)
self._display.display(" " + task_result, display_color, stderr=self.display_failed_stderr)
def v2_runner_on_ok(self, result, msg="ok", display_color=C.COLOR_OK):
self._preprocess_result(result)
@ -128,9 +133,11 @@ class CallbackModule(CallbackBase):
if result_was_changed:
msg = "done"
display_color = C.COLOR_CHANGED
task_result = self._process_result_output(result, msg)
self._display.display(" " + task_result, display_color)
task_result = self._process_result_output(result, msg)
self._display.display(" " + task_result, display_color)
elif self.display_ok_hosts:
task_result = self._process_result_output(result, msg)
self._display.display(" " + task_result, display_color)
def v2_runner_item_on_skipped(self, result):
self.v2_runner_on_skipped(result)
@ -146,7 +153,7 @@ class CallbackModule(CallbackBase):
display_color = C.COLOR_UNREACHABLE
task_result = self._process_result_output(result, msg)
self._display.display(" " + task_result, display_color)
self._display.display(" " + task_result, display_color, stderr=self.display_failed_stderr)
def v2_on_file_diff(self, result):
if result._task.loop and 'results' in result._result:
@ -189,6 +196,20 @@ class CallbackModule(CallbackBase):
colorize(u'ignored', t['ignored'], None)),
log_only=True
)
if stats.custom and self.show_custom_stats:
self._display.banner("CUSTOM STATS: ")
# per host
# TODO: come up with 'pretty format'
for k in sorted(stats.custom.keys()):
if k == '_run':
continue
self._display.display('\t%s: %s' % (k, self._dump_results(stats.custom[k], indent=1).replace('\n', '')))
# print per run custom stats
if '_run' in stats.custom:
self._display.display("", screen_only=True)
self._display.display('\tRUN: %s' % self._dump_results(stats.custom['_run'], indent=1).replace('\n', ''))
self._display.display("", screen_only=True)
def v2_playbook_on_no_hosts_matched(self):
self._display.display(" No hosts found!", color=C.COLOR_DEBUG)