Move color coding bits to module, use over rest of playbook

pull/675/merge
Michael DeHaan 13 years ago
parent f4a4649086
commit 12ff9b5b9a

@ -27,47 +27,10 @@ import ansible.constants as C
from ansible import errors from ansible import errors
from ansible import callbacks from ansible import callbacks
from ansible import utils from ansible import utils
from ansible.color import ANSIBLE_COLOR, stringc
ANSIBLE_COLOR=True
if os.getenv("ANSIBLE_NOCOLOR") is not None:
ANSIBLE_COLOR=False
if not sys.stdout.isatty():
ANSIBLE_COLOR=False
# --- begin "pretty"
#
# pretty - A miniature library that provides a Python print and stdout
# wrapper that makes colored terminal text easier to use (eg. without
# having to mess around with ANSI escape sequences). This code is public
# domain - there is no license except that you must leave this header.
#
# Copyright (C) 2008 Brian Nez <thedude at bri1 dot com>
#
# http://nezzen.net/2008/06/23/colored-text-in-python-using-ansi-escape-sequences/
codeCodes = {
'black': '0;30', 'bright gray': '0;37',
'blue': '0;34', 'white': '1;37',
'green': '0;32', 'bright blue': '1;34',
'cyan': '0;36', 'bright green': '1;32',
'red': '0;31', 'bright cyan': '1;36',
'purple': '0;35', 'bright red': '1;31',
'yellow': '0;33', 'bright purple': '1;35',
'dark gray': '1;30', 'bright yellow': '1;33',
'normal': '0'
}
def stringc(text, color):
"""String in color."""
if ANSIBLE_COLOR:
return "\033["+codeCodes[color]+"m"+text+"\033[0m"
else:
return text
# --- end "pretty"
def colorize(lead, num, color): def colorize(lead, num, color):
"""Print `lead' = `num' in `color'""" """ Print 'lead' = 'num' in 'color' """
if num == 0: if num == 0:
color='black'; color='black';
if ANSIBLE_COLOR: if ANSIBLE_COLOR:
@ -75,10 +38,14 @@ def colorize(lead, num, color):
else: else:
return "%s=%-4s" % (lead, str(num)) return "%s=%-4s" % (lead, str(num))
def hostcolor(host, t): def hostcolor(host, stats):
if ANSIBLE_COLOR: if ANSIBLE_COLOR:
if t['failures'] != 0 or t['unreachable'] != 0: if stats['failures'] != 0 or stats['unreachable'] != 0:
return "%-41s" % stringc(host, 'red') return "%-41s" % stringc(host, 'red')
elif stats['changed'] != 0:
return "%-41s" % stringc(host, 'yellow')
else:
return "%-41s" % stringc(host, 'green')
return "%-30s" % host return "%-30s" % host

@ -20,6 +20,7 @@ import sys
import getpass import getpass
import os import os
import subprocess import subprocess
from ansible.color import stringc
cowsay = None cowsay = None
if os.path.exists("/usr/bin/cowsay"): if os.path.exists("/usr/bin/cowsay"):
@ -125,17 +126,18 @@ def host_report_msg(hostname, module_name, result, oneline):
''' summarize the JSON results for a particular host ''' ''' summarize the JSON results for a particular host '''
failed = utils.is_failed(result) failed = utils.is_failed(result)
msg = ''
if module_name in [ 'command', 'shell', 'raw' ] and 'ansible_job_id' not in result: if module_name in [ 'command', 'shell', 'raw' ] and 'ansible_job_id' not in result:
if not failed: if not failed:
return command_generic_msg(hostname, result, oneline, 'success') msg = command_generic_msg(hostname, result, oneline, 'success')
else: else:
return command_generic_msg(hostname, result, oneline, 'FAILED') msg = command_generic_msg(hostname, result, oneline, 'FAILED')
else: else:
if not failed: if not failed:
return regular_generic_msg(hostname, result, oneline, 'success') msg = regular_generic_msg(hostname, result, oneline, 'success')
else: else:
return regular_generic_msg(hostname, result, oneline, 'FAILED') msg = regular_generic_msg(hostname, result, oneline, 'FAILED')
return msg
############################################### ###############################################
@ -262,45 +264,62 @@ class PlaybookRunnerCallbacks(DefaultRunnerCallbacks):
item = results.get('item', None) item = results.get('item', None)
if item: if item:
print "failed: [%s] => (item=%s) => %s" % (host, item, utils.jsonify(results)) msg = "failed: [%s] => (item=%s) => %s" % (host, item, utils.jsonify(results))
else: else:
print "failed: [%s] => %s" % (host, utils.jsonify(results)) msg = "failed: [%s] => %s" % (host, utils.jsonify(results))
return color.string(msg, 'red')
def on_ok(self, host, host_result): def on_ok(self, host, host_result):
item = host_result.get('item', None) item = host_result.get('item', None)
# show verbose output for non-setup module results if --verbose is used # show verbose output for non-setup module results if --verbose is used
msg = ''
if not self.verbose or host_result.get("verbose_override",None) is not None: if not self.verbose or host_result.get("verbose_override",None) is not None:
if item: if item:
print "ok: [%s] => (item=%s)" % (host,item) msg = "ok: [%s] => (item=%s)" % (host,item)
else: else:
print "ok: [%s]" % (host) if 'ansible_job_id' not in host_result or 'finished' in host_result:
msg = "ok: [%s]" % (host)
else: else:
# verbose ...
if item: if item:
print "ok: [%s] => (item=%s) => %s" % (host, item, utils.jsonify(host_result)) msg = "ok: [%s] => (item=%s) => %s" % (host, item, utils.jsonify(host_result))
else: else:
print "ok: [%s] => %s" % (host, utils.jsonify(host_result)) if 'ansible_job_id' not in host_result or 'finished' in host_result:
msg = "ok: [%s] => %s" % (host, utils.jsonify(host_result))
if msg != '':
if not 'changed' in host_result or not host_result['changed']:
print stringc(msg, 'green')
else:
print stringc(msg, 'yellow')
def on_error(self, host, err): def on_error(self, host, err):
item = err.get('item', None) item = err.get('item', None)
msg = ''
if item: if item:
print >>sys.stderr, "err: [%s] => (item=%s) => %s" % (host, item, err) msg = "err: [%s] => (item=%s) => %s" % (host, item, err)
else: else:
print >>sys.stderr, "err: [%s] => %s" % (host, err) msg = "err: [%s] => %s" % (host, err)
msg = stringc(msg, 'red')
print >>sys.stderr, msg
def on_skipped(self, host, item=None): def on_skipped(self, host, item=None):
if item: msg = ''
print "skipping: [%s] => (item=%s)" % (host, item) if item:
msg = "skipping: [%s] => (item=%s)" % (host, item)
else: else:
print "skipping: [%s]" % host msg = "skipping: [%s]" % host
print stringc(msg, 'yellow')
def on_no_hosts(self): def on_no_hosts(self):
print "no hosts matched or remaining\n" print stringc("no hosts matched or remaining\n", 'orange')
def on_async_poll(self, host, res, jid, clock): def on_async_poll(self, host, res, jid, clock):
@ -308,15 +327,19 @@ class PlaybookRunnerCallbacks(DefaultRunnerCallbacks):
self._async_notified[jid] = clock + 1 self._async_notified[jid] = clock + 1
if self._async_notified[jid] > clock: if self._async_notified[jid] > clock:
self._async_notified[jid] = clock self._async_notified[jid] = clock
print "<job %s> polling, %ss remaining"%(jid, clock) msg = "<job %s> polling, %ss remaining"%(jid, clock)
print stringc(msg, 'blue')
def on_async_ok(self, host, res, jid): def on_async_ok(self, host, res, jid):
print "<job %s> finished on %s"%(jid, host) msg = "<job %s> finished on %s"%(jid, host)
print stringc(msg, 'blue')
def on_async_failed(self, host, res, jid): def on_async_failed(self, host, res, jid):
print "<job %s> FAILED on %s"%(jid, host) msg = "<job %s> FAILED on %s"%(jid, host)
print stringc(msg, 'red')
######################################################################## ########################################################################

@ -0,0 +1,59 @@
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
ANSIBLE_COLOR=True
if os.getenv("ANSIBLE_NOCOLOR") is not None:
ANSIBLE_COLOR=False
if not sys.stdout.isatty():
ANSIBLE_COLOR=False
# --- begin "pretty"
#
# pretty - A miniature library that provides a Python print and stdout
# wrapper that makes colored terminal text easier to use (eg. without
# having to mess around with ANSI escape sequences). This code is public
# domain - there is no license except that you must leave this header.
#
# Copyright (C) 2008 Brian Nez <thedude at bri1 dot com>
#
# http://nezzen.net/2008/06/23/colored-text-in-python-using-ansi-escape-sequences/
codeCodes = {
'black': '0;30', 'bright gray': '0;37',
'blue': '0;34', 'white': '1;37',
'green': '0;32', 'bright blue': '1;34',
'cyan': '0;36', 'bright green': '1;32',
'red': '0;31', 'bright cyan': '1;36',
'purple': '0;35', 'bright red': '1;31',
'yellow': '0;33', 'bright purple': '1;35',
'dark gray': '1;30', 'bright yellow': '1;33',
'normal': '0'
}
def stringc(text, color):
"""String in color."""
if ANSIBLE_COLOR:
return "\033["+codeCodes[color]+"m"+text+"\033[0m"
else:
return text
# --- end "pretty"

@ -25,6 +25,7 @@ import yaml
import optparse import optparse
import operator import operator
from ansible import errors from ansible import errors
from ansible import color
import ansible.constants as C import ansible.constants as C
try: try:
@ -329,3 +330,5 @@ def base_parser(constants=C, usage="", output_opts=False, runas_opts=False, asyn
return parser return parser

Loading…
Cancel
Save