1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Implement our own detailed task output

Also:
- Streamline ANSI calls
- Implement v2_playbook_on_include
This commit is contained in:
Dag Wieers 2016-03-20 20:25:34 +01:00 committed by Brian Coca
parent 221a4e7014
commit 893dc81b5e

View file

@ -113,10 +113,10 @@ class ansi:
clear = '\033[2J' clear = '\033[2J'
# clearline = '\033[K' # clearline = '\033[K'
clearline = '\033[2K' clearline = '\033[2K'
# save = '\033[s' save = '\033[s'
# restore = '\033[u' restore = '\033[u'
save = '\0337' save_all = '\0337'
restore = '\0338' restore_all = '\0338'
linewrap = '\033[7h' linewrap = '\033[7h'
nolinewrap = '\033[7l' nolinewrap = '\033[7l'
@ -177,7 +177,7 @@ class CallbackModule_dense(CallbackModule_default):
self.type = 'foo' self.type = 'foo'
# Start immediately on the first line # Start immediately on the first line
sys.stdout.write(ansi.save + ansi.reset + ansi.clearline) sys.stdout.write(ansi.reset + ansi.save + ansi.clearline)
sys.stdout.flush() sys.stdout.flush()
def _add_host(self, result, status): def _add_host(self, result, status):
@ -234,7 +234,7 @@ class CallbackModule_dense(CallbackModule_default):
def _display_progress(self, result=None): 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.reset + ansi.clearline + ansi.nolinewrap + ansi.underline)
sys.stdout.write('%s %d:' % (self.type, self.count[self.type])) sys.stdout.write('%s %d:' % (self.type, self.count[self.type]))
sys.stdout.write(ansi.reset) sys.stdout.write(ansi.reset)
sys.stdout.flush() sys.stdout.flush()
@ -249,52 +249,60 @@ class CallbackModule_dense(CallbackModule_default):
# If we are expecting diff output, show it on a new line # If we are expecting diff output, show it on a new line
if result._result.get('diff', False): if result._result.get('diff', False):
sys.stdout.write('\n') sys.stdout.write('\n' + ansi.linewrap)
self.keep = False self.keep = False
def _display_task_banner(self): def _display_task_banner(self):
if not self.shown_title: if not self.shown_title:
self.shown_title = True self.shown_title = True
sys.stdout.write(ansi.restore + ansi.clearline) sys.stdout.write(ansi.restore + ansi.reset + ansi.clearline + ansi.underline)
sys.stdout.write(ansi.underline + '%s %d: %s' % (self.type, self.count[self.type], self.task.get_name().strip())) sys.stdout.write('%s %d: %s' % (self.type, self.count[self.type], self.task.get_name().strip()))
sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.reset + ansi.clearline) sys.stdout.write(ansi.restore + ansi.reset + '\n' + ansi.save + ansi.clearline)
sys.stdout.flush() sys.stdout.flush()
else: else:
sys.stdout.write(ansi.restore + ansi.clearline) sys.stdout.write(ansi.restore + ansi.reset + ansi.clearline)
self.keep = False
def _display_results(self, result, status): def _display_results(self, result, status):
dump = '' # Leave the previous task on screen (as it has changes/errors)
if self.keep:
sys.stdout.write(ansi.restore + ansi.reset + '\n' + ansi.save + ansi.clearline)
else:
sys.stdout.write(ansi.restore + ansi.reset + ansi.clearline)
self.keep = False
self._clean_results(result._result) self._clean_results(result._result)
dump = ''
if result._task.action == 'include': if result._task.action == 'include':
return return
elif status == 'ok': elif status == 'ok':
return return
elif status == 'changed':
color = C.COLOR_CHANGED
elif status == 'ignored': elif status == 'ignored':
color = C.COLOR_SKIPPED
dump = self._handle_exceptions(result._result) dump = self._handle_exceptions(result._result)
elif status == 'failed': elif status == 'failed':
color = C.COLOR_ERROR
dump = self._handle_exceptions(result._result) dump = self._handle_exceptions(result._result)
elif status == 'unreachable': elif status == 'unreachable':
color = C.COLOR_UNREACHABLE
dump = result._result['msg'] dump = result._result['msg']
if not dump: if not dump:
dump = self._dump_results(result._result) 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)
if result._task.loop and 'results' in result._result: if result._task.loop and 'results' in result._result:
self._process_items(result) self._process_items(result)
else: else:
self._display.display(msg, color=color) sys.stdout.write(colors[status] + status + ': ')
delegated_vars = result._result.get('_ansible_delegated_vars', None)
if delegated_vars:
sys.stdout.write(ansi.reset + result._host.get_name() + '>' + colors[status] + delegated_vars['ansible_host'])
else:
sys.stdout.write(result._host.get_name())
sys.stdout.write(': ' + dump + '\n')
sys.stdout.write(ansi.save + ansi.clearline)
sys.stdout.flush()
if status == 'changed': if status == 'changed':
self._handle_warnings(result._result) self._handle_warnings(result._result)
@ -304,31 +312,33 @@ class CallbackModule_dense(CallbackModule_default):
self.super_ref.v2_playbook_on_play_start(play) self.super_ref.v2_playbook_on_play_start(play)
return return
# Leave the previous task on screen (as it has changes/errors)
if self.keep:
sys.stdout.write(ansi.restore + ansi.reset + '\n' + ansi.save + ansi.clearline + ansi.bold)
else:
sys.stdout.write(ansi.restore + ansi.reset + ansi.clearline + ansi.bold)
# Reset at the start of each play # Reset at the start of each play
self.keep = False
self.count.update(dict(handler=0, task=0)) self.count.update(dict(handler=0, task=0))
self.count['play'] += 1 self.count['play'] += 1
self.play = play self.play = play
# Leave the previous task on screen (as it has changes/errors)
if self.keep:
sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.clearline + ansi.bold)
else:
sys.stdout.write(ansi.restore + ansi.clearline + ansi.bold)
# Write the next play on screen IN UPPERCASE, and make it permanent # Write the next play on screen IN UPPERCASE, and make it permanent
name = play.get_name().strip() name = play.get_name().strip()
if not name: if not name:
name = 'unnamed' name = 'unnamed'
sys.stdout.write('PLAY %d: %s' % (self.count['play'], name.upper())) sys.stdout.write('PLAY %d: %s' % (self.count['play'], name.upper()))
sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.reset + ansi.clearline) sys.stdout.write(ansi.restore + ansi.reset + '\n' + ansi.save + ansi.clearline)
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):
# 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 + ansi.reset + '\n' + ansi.save + ansi.clearline + ansi.underline)
else: else:
sys.stdout.write(ansi.restore + ansi.underline) # Do not clear line, since we want to retain the previous output
sys.stdout.write(ansi.restore + ansi.reset + ansi.underline)
# Reset at the start of each task # Reset at the start of each task
self.keep = False self.keep = False
@ -343,15 +353,14 @@ class CallbackModule_dense(CallbackModule_default):
# 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('%s %d.' % (self.type, self.count[self.type])) sys.stdout.write('%s %d.' % (self.type, self.count[self.type]))
sys.stdout.write(ansi.reset)
sys.stdout.flush() sys.stdout.flush()
def v2_playbook_on_handler_task_start(self, task): def v2_playbook_on_handler_task_start(self, task):
# 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 + ansi.reset + '\n' + ansi.save + ansi.clearline + ansi.underline)
else: else:
sys.stdout.write(ansi.restore + ansi.reset + ansi.underline) sys.stdout.write(ansi.restore + ansi.reset + ansi.clearline + ansi.underline)
# Reset at the start of each handler # Reset at the start of each handler
self.keep = False self.keep = False
@ -366,13 +375,11 @@ class CallbackModule_dense(CallbackModule_default):
# 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('%s %d.' % (self.type, self.count[self.type])) sys.stdout.write('%s %d.' % (self.type, self.count[self.type]))
sys.stdout.write(ansi.reset)
sys.stdout.flush() sys.stdout.flush()
def v2_playbook_on_cleanup_task_start(self, task): def v2_playbook_on_cleanup_task_start(self, task):
# TBD # TBD
sys.stdout.write('cleanup.') sys.stdout.write('cleanup.')
sys.stdout.write(ansi.reset)
sys.stdout.flush() sys.stdout.flush()
def v2_runner_on_failed(self, result, ignore_errors=False): def v2_runner_on_failed(self, result, ignore_errors=False):
@ -412,20 +419,23 @@ class CallbackModule_dense(CallbackModule_default):
def v2_playbook_on_no_hosts_remaining(self): def v2_playbook_on_no_hosts_remaining(self):
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 + ansi.reset + '\n' + ansi.save + ansi.clearline)
else: else:
sys.stdout.write(ansi.restore + ansi.clearline) sys.stdout.write(ansi.restore + ansi.reset + ansi.clearline)
# Reset keep # Reset keep
self.keep = False self.keep = False
sys.stdout.write(ansi.white + ansi.redbg + 'NO MORE HOSTS LEFT' + ansi.reset) sys.stdout.write(ansi.white + ansi.redbg + 'NO MORE HOSTS LEFT')
sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.reset + ansi.clearline) sys.stdout.write(ansi.restore + ansi.reset + '\n' + ansi.save + ansi.clearline)
sys.stdout.flush() sys.stdout.flush()
def v2_playbook_on_include(self, included_file):
pass
def v2_playbook_on_stats(self, stats): def v2_playbook_on_stats(self, stats):
if self.keep: if self.keep:
sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.reset + ansi.clearline) sys.stdout.write(ansi.restore + ansi.reset + '\n' + ansi.save + ansi.clearline)
else: else:
sys.stdout.write(ansi.restore + ansi.reset + ansi.clearline) sys.stdout.write(ansi.restore + ansi.reset + ansi.clearline)
@ -436,7 +446,7 @@ class CallbackModule_dense(CallbackModule_default):
sys.stdout.write(ansi.bold + ansi.underline) sys.stdout.write(ansi.bold + ansi.underline)
sys.stdout.write('SUMMARY') sys.stdout.write('SUMMARY')
sys.stdout.write(ansi.restore + '\n' + ansi.save + ansi.reset + ansi.clearline) sys.stdout.write(ansi.restore + ansi.reset + '\n' + ansi.save + ansi.clearline)
sys.stdout.flush() sys.stdout.flush()
hosts = sorted(stats.processed.keys()) hosts = sorted(stats.processed.keys())