Assorted improvements

- Better switch between *dense* and *default*
- Reimplement C.COLOR* out of necessity (help!)
- Make verbose output more dense (clean up result)
- Implement our own dumper
- Improve delegation support
This commit is contained in:
Dag Wieers 2016-03-16 16:02:37 +01:00 committed by Brian Coca
parent 223c0011e0
commit 589953c79b

View file

@ -19,7 +19,6 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
from ansible import constants as C
from ansible.plugins.callback.default import CallbackModule as CallbackModule_default from ansible.plugins.callback.default import CallbackModule as CallbackModule_default
from ansible.utils.color import colorize, hostcolor from ansible.utils.color import colorize, hostcolor
from collections import OrderedDict from collections import OrderedDict
@ -43,25 +42,41 @@ import sys
# + If verbosity increases, act as default output # + If verbosity increases, act as default output
# So that users can easily switch to default for troubleshooting # So that users can easily switch to default for troubleshooting
# #
# + Leave previous task output on screen # + Rewrite the output during processing
# - If we would clear the line at the start of a task, there would often
# be no information at all
#
# - We use the cursor to indicate where in the task we are. # - We use the cursor to indicate where in the task we are.
# Output after the prompt is the output of the previous task # Output after the prompt is the output of the previous task.
# - If we would clear the line at the start of a task, there would often
# be no information at all, so we leave it until it gets updated
# #
# + Use the same color-conventions of Ansible # + Use the same color-conventions of Ansible
#
# + Ensure the verbose output (-v) is also dense.
# Remove information that is not essential (eg. timestamps, status)
# TODO: # TODO:
# #
# + Ensure all other output is properly displayed
# + Properly test for terminal capabilities, and fall back to default # + Properly test for terminal capabilities, and fall back to default
# + Modify Ansible mechanism so we don't need to use sys.stdout directly # + Modify Ansible mechanism so we don't need to use sys.stdout directly
# + Remove items from result to compact the json output when using -v
# + Make colored output nicer
# + Find an elegant solution for line wrapping # + Find an elegant solution for line wrapping
# + Support notification handler # + Support notification handlers
# + Better support for item-loops (in -v mode)
# When using -vv or higher, simply do the default action
# FIXME: Importing constants as C simply does not work, beats me :-/
#from ansible import constants as C
class C:
COLOR_HIGHLIGHT = 'white'
COLOR_VERBOSE = 'blue'
COLOR_WARN = 'bright purple'
COLOR_ERROR = 'red'
COLOR_DEBUG = 'dark gray'
COLOR_DEPRECATE = 'purple'
COLOR_SKIP = 'cyan'
COLOR_UNREACHABLE = 'bright red'
COLOR_OK = 'green'
COLOR_CHANGED = 'yellow'
# Taken from Dstat # Taken from Dstat
@ -118,13 +133,14 @@ colors = dict(
ok = ansi.darkgreen, ok = ansi.darkgreen,
changed = ansi.darkyellow, changed = ansi.darkyellow,
skipped = ansi.darkcyan, skipped = ansi.darkcyan,
failed=ansi.darkred, ignored = ansi.redbg + ansi.cyan,
unreachable=ansi.redbg+ansi.white failed = ansi.redbg + ansi.darkred,
unreachable = ansi.red,
) )
states = ( 'skipped', 'ok', 'changed', 'failed', 'unreachable' ) states = ( 'skipped', 'ok', 'changed', 'failed', 'unreachable' )
class CallbackModule(CallbackModule_default): class CallbackModule_dense(CallbackModule_default):
''' '''
This is the dense callback interface, where screen estate is still valued. This is the dense callback interface, where screen estate is still valued.
@ -134,24 +150,15 @@ class CallbackModule(CallbackModule_default):
CALLBACK_TYPE = 'stdout' CALLBACK_TYPE = 'stdout'
CALLBACK_NAME = 'dense' CALLBACK_NAME = 'dense'
def __init__(self): def __init__(self):
# From CallbackModule # From CallbackModule
self._display = display self._display = display
if self._display.verbosity >= 4:
name = getattr(self, 'CALLBACK_NAME', 'unnamed')
ctype = getattr(self, 'CALLBACK_TYPE', 'old')
version = getattr(self, 'CALLBACK_VERSION', '1.0')
self._display.vvvv('Loaded callback %s of type %s, v%s' % (name, ctype, version))
self.super_ref = super(CallbackModule, self) self.super_ref = super(CallbackModule, self)
self.super_ref.__init__() self.super_ref.__init__()
# When using -vv or higher, simply do the default action
if self._display.verbosity >= 2:
return
self.hosts = OrderedDict() self.hosts = OrderedDict()
self.keep = False self.keep = False
self.shown_title = False self.shown_title = False
@ -166,11 +173,20 @@ class CallbackModule(CallbackModule_default):
def _add_host(self, result, status): def _add_host(self, result, status):
name = result._host.get_name() name = result._host.get_name()
# Check if we have to update an existing state (when looping) if status == 'failed' and result._task.ignore_errors:
status = 'ignored'
# Check if we have to update an existing state (when looping over items)
if name not in self.hosts: if name not in self.hosts:
self.hosts[name] = status self.hosts[name] = dict(state=status)
elif states.index(self.hosts[name]) < states.index(status): elif states.index(self.hosts[name]['state']) < states.index(status):
self.hosts[name] = status self.hosts[name]['state'] = status
# Store delegated hostname, if needed
delegated_vars = result._result.get('_ansible_delegated_vars', None)
if delegated_vars:
self.hosts[name]['delegate'] = delegated_vars['ansible_host']
self._display_progress(result) self._display_progress(result)
@ -181,14 +197,36 @@ class CallbackModule(CallbackModule_default):
if self._display.verbosity == 1: if self._display.verbosity == 1:
# Print task title, if needed # Print task title, if needed
self._display_task_banner() self._display_task_banner()
self._display_results(result, status)
# TODO: clean up result output, eg. remove changed, delta, end, start, ... def _clean_results(self, result):
if status == 'changed': # Remove non-essential atributes
self.super_ref.v2_runner_on_ok(result) removed_attributes = ('changed', 'delta', 'end', 'failed', 'failed_when_result', 'invocation', 'start', 'stdout_lines')
elif status == 'failed': for attr in removed_attributes:
self.super_ref.v2_runner_on_failed(result) if attr in result:
elif status == 'unreachable': del(result[attr])
self.super_ref.v2_runner_on_unreachable(result)
# Remove empty attributes (list, dict, str)
for attr in result.copy():
if type(result[attr]) in (list, dict, basestring, unicode):
if not result[attr]:
del(result[attr])
if 'cmd' in result:
result['cmd'] = ' '.join(result['cmd'])
def _handle_exceptions(self, result):
if 'exception' in result:
if self._display.verbosity < 3:
# extract just the actual error message from the exception text
error = result['exception'].strip().split('\n')[-1]
msg = "An exception occurred during task execution. To see the full traceback, use -vvv. The error was: %s" % error
else:
msg = "An exception occurred during task execution. The full traceback is:\n" + result['exception']
# finally, remove the exception from the result so it's not shown every time
del result['exception']
return msg
def _display_task_banner(self): def _display_task_banner(self):
if not self.shown_title: if not self.shown_title:
@ -200,21 +238,50 @@ class CallbackModule(CallbackModule_default):
else: else:
sys.stdout.write(ansi.restore + ansi.clearline) sys.stdout.write(ansi.restore + ansi.clearline)
def _display_progress(self, result): def _display_results(self, result, status):
dump = ''
self._handle_warnings(result._result)
self._clean_results(result._result)
if result._task.action == 'include':
return
elif status == 'ignored':
return
elif status == 'changed':
color = C.COLOR_CHANGED
elif status == 'failed':
color = C.COLOR_ERROR
dump = self._handle_exceptions(result._result)
elif status == 'unreachable':
color = C.COLOR_UNREACHABLE
dump = result._result['msg']
if not dump:
dump = self._dump_results(result._result)
delegated_vars = result._result.get('_ansible_delegated_vars', None)
if delegated_vars:
msg = "%s: %s>%s: %s" % (status, result._host.get_name(), delegated_vars['ansible_host'], dump)
else:
msg = "%s: %s: %s" % (status, result._host.get_name(), dump)
self._display.display(msg, color=color)
if result._task.ignore_errors:
self._display.display("...ignoring", color=C.COLOR_SKIP)
def _display_progress(self, result=None):
# Always rewrite the complete line # Always rewrite the complete line
sys.stdout.write(ansi.restore + ansi.clearline + ansi.underline) sys.stdout.write(ansi.restore + ansi.clearline + ansi.underline)
sys.stdout.write('task %d:' % self.tasknr) sys.stdout.write('task %d:' % self.tasknr)
sys.stdout.write(ansi.reset) sys.stdout.write(ansi.reset)
sys.stdout.flush() sys.stdout.flush()
# Print delegated hostname, if needed # Print out each host in its own status-color
delegated_vars = result._result.get('_ansible_delegated_vars', None)
if delegated_vars:
sys.stdout.write(' ' + delegated_vars['ansible_host'] + '>>')
# Print out each host with its own status-color
for name in self.hosts: for name in self.hosts:
sys.stdout.write(' ' + colors[self.hosts[name]] + name + ansi.reset) sys.stdout.write(' ')
if self.hosts[name].get('delegate', None):
sys.stdout.write(self.hosts[name]['delegate'] + '>')
sys.stdout.write(colors[self.hosts[name]['state']] + name + ansi.reset)
sys.stdout.flush() sys.stdout.flush()
# Reset color # Reset color
@ -246,10 +313,6 @@ class CallbackModule(CallbackModule_default):
sys.stdout.flush() sys.stdout.flush()
def v2_playbook_on_task_start(self, task, is_conditional): def v2_playbook_on_task_start(self, task, is_conditional):
if self._display.verbosity > 1:
self.super_ref.v2_playbook_on_task_start(task, is_conditional)
return
# Leave the previous task on screen (as it has changes/errors) # Leave the previous task on screen (as it has changes/errors)
if self._display.verbosity == 0 and self.keep: if self._display.verbosity == 0 and self.keep:
sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.reset + ansi.clearline + ansi.underline) sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.reset + ansi.clearline + ansi.underline)
@ -270,13 +333,8 @@ class CallbackModule(CallbackModule_default):
sys.stdout.write('task %d.' % self.tasknr) sys.stdout.write('task %d.' % self.tasknr)
sys.stdout.write(ansi.reset) sys.stdout.write(ansi.reset)
sys.stdout.flush() sys.stdout.flush()
# self._display_progress()
def v2_playbook_on_handler_task_start(self, task): def v2_playbook_on_handler_task_start(self, task):
if self._display.verbosity >= 2:
self.super_ref.v2_playbook_on_handler_task_start(task)
return
# Leave the previous task on screen (as it has changes/errors) # Leave the previous task on screen (as it has changes/errors)
if self._display.verbosity == 0 and self.keep: if self._display.verbosity == 0 and self.keep:
sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.reset + ansi.clearline + ansi.underline) sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.reset + ansi.clearline + ansi.underline)
@ -289,80 +347,54 @@ class CallbackModule(CallbackModule_default):
self.hosts = OrderedDict() self.hosts = OrderedDict()
self.task = task self.task = task
if task.get_name() != 'setup':
self.handlernr += 1
# Write the next task on screen (behind the prompt is the previous output) # Write the next task on screen (behind the prompt is the previous output)
sys.stdout.write('handler %d.' % self.handlernr) sys.stdout.write('handler %d.' % self.handlernr)
sys.stdout.write(ansi.reset) sys.stdout.write(ansi.reset)
sys.stdout.flush() sys.stdout.flush()
# self._display_progress()
def v2_playbook_on_cleanup_task_start(self, task): def v2_playbook_on_cleanup_task_start(self, task):
if self._display.verbosity >= 2: # TBD
self.super_ref.v2_playbook_on_cleanup_task_start(start) sys.stdout.write('cleanup.')
return sys.stdout.write(ansi.reset)
sys.stdout.flush()
self._display.banner("CLEANUP TASK [%s]" % task.get_name().strip())
def v2_runner_on_failed(self, result, ignore_errors=False): def v2_runner_on_failed(self, result, ignore_errors=False):
if self._display.verbosity >= 2:
self.super_ref.v2_runner_on_failed(result, ignore_errors)
return
self._add_host(result, 'failed') self._add_host(result, 'failed')
def v2_runner_on_ok(self, result): def v2_runner_on_ok(self, result):
if self._display.verbosity >= 2:
self.super_ref.v2_runner_on_ok(result)
return
if result._result.get('changed', False): if result._result.get('changed', False):
self._add_host(result, 'changed') self._add_host(result, 'changed')
else: else:
self._add_host(result, 'ok') self._add_host(result, 'ok')
def v2_runner_on_skipped(self, result): def v2_runner_on_skipped(self, result):
if self._display.verbosity >= 2:
self.super_ref.v2_runner_on_skipped(result)
return
self._add_host(result, 'skipped') self._add_host(result, 'skipped')
def v2_runner_on_unreachable(self, result): def v2_runner_on_unreachable(self, result):
if self._display.verbosity >= 2:
self.super_ref.v2_runner_on_unreachable(result)
return
self._add_host(result, 'unreachable') self._add_host(result, 'unreachable')
def v2_runner_on_include(self, included_file): def v2_runner_on_include(self, included_file):
if self._display.verbosity >= 2: pass
self.super_ref.v2_runner_on_include(included_file)
def v2_playbook_item_on_ok(self, result): def v2_playbook_item_on_ok(self, result):
if self._display.verbosity >= 2: # TBD
self.super_ref.v2_playbook_item_on_ok(result)
if result._result.get('changed', False): if result._result.get('changed', False):
self._add_host(result, 'changed') self._add_host(result, 'changed')
else: else:
self._add_host(result, 'ok') self._add_host(result, 'ok')
def v2_playbook_item_on_failed(self, result): def v2_playbook_item_on_failed(self, result):
if self._display.verbosity >= 2: # TBD
self.super_ref.v2_playbook_item_on_failed(result)
self._add_host(result, 'failed') self._add_host(result, 'failed')
def v2_playbook_item_on_skipped(self, result): def v2_playbook_item_on_skipped(self, result):
if self._display.verbosity >= 2: # TBD
self.super_ref.v2_playbook_item_on_skipped(result)
self._add_host(result, 'skipped') self._add_host(result, 'skipped')
def v2_playbook_on_no_hosts_remaining(self): def v2_playbook_on_no_hosts_remaining(self):
if self._display.verbosity >= 2:
self.super_ref.v2_playbook_on_no_hosts_remaining()
return
# TBD # TBD
if self._display.verbosity == 0 and self.keep: if self._display.verbosity == 0 and self.keep:
sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.clearline) sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.clearline)
@ -376,12 +408,8 @@ class CallbackModule(CallbackModule_default):
sys.stdout.flush() sys.stdout.flush()
def v2_playbook_on_stats(self, stats): def v2_playbook_on_stats(self, stats):
if self._display.verbosity >= 2:
self.super_ref.v2_playbook_on_stats(stats)
return
# In normal mode screen output should be sufficient # In normal mode screen output should be sufficient
elif self._display.verbosity == 0: if self._display.verbosity == 0:
return return
if self.keep: if self.keep:
@ -391,10 +419,6 @@ class CallbackModule(CallbackModule_default):
sys.stdout.write('SUMMARY') sys.stdout.write('SUMMARY')
# FIXME: Reports 'module' object C not having attribute 'COLOR_OK' ?? Doing default instead :-/
self.super_ref.v2_playbook_on_stats(stats)
return
sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.reset + ansi.clearline) sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.reset + ansi.clearline)
sys.stdout.flush() sys.stdout.flush()
@ -409,3 +433,8 @@ class CallbackModule(CallbackModule_default):
colorize(u'failed', t['failures'], C.COLOR_ERROR)), colorize(u'failed', t['failures'], C.COLOR_ERROR)),
screen_only=True screen_only=True
) )
if display.verbosity >= 2:
CallbackModule = CallbackModule_default
else:
CallbackModule = CallbackModule_dense