def colorize(lead, num, color): """ Print 'lead' = 'num' in 'color' """ if num != 0 and ANSIBLE_COLOR and color is not None: return "%s%s%-15s" % (stringc(lead, color), stringc("=", color), stringc(str(num), color)) else: return "%s=%-4s" % (lead, str(num))
def print_stats(host, smry): ok = smry['ok'] changed = smry['changed'] unreachable = smry['unreachable'] failures = smry['failures'] if unreachable > 0 or failures > 0: status = stringc("FAILED", "red") else: status = stringc("OK", "bright green") pattern = "\tPlaybook %s, %s, %s, %s, %s" display(pattern % (status, # hostcolor(host, smry, False), colorize('ok', ok, 'bright green'), colorize('changed', changed, 'yellow'), colorize('unreachable', unreachable, 'red'), colorize('failed', failures, 'red')), screen_only=True ) display(pattern % (status, # hostcolor(host, smry, False), colorize('ok', ok, None), colorize('changed', changed, None), colorize('unreachable', unreachable, None), colorize('failed', failures, None)), log_only=True )
def on_ok(self, host, host_result): item = host_result.get('item', None) host_result2 = host_result.copy() host_result2.pop('invocation', None) changed = host_result.get('changed', False) ok_or_changed = 'ok' if changed: ok_or_changed = 'changed' # show verbose output for non-setup module results if --verbose is used msg = '' if not self.verbose or host_result2.get("verbose_override",None) is not None: if item: msg = "%s: [%s] => (item=%s)" % (ok_or_changed, host, item) else: if 'ansible_job_id' not in host_result or 'finished' in host_result: msg = "%s: [%s]" % (ok_or_changed, host) else: # verbose ... if item: msg = "%s: [%s] => (item=%s) => %s" % (ok_or_changed, host, item, utils.jsonify(host_result2)) else: if 'ansible_job_id' not in host_result or 'finished' in host_result2: msg = "%s: [%s] => %s" % (ok_or_changed, host, utils.jsonify(host_result2)) if msg != '': if not changed: print stringc(msg, 'green') else: print stringc(msg, 'yellow') super(PlaybookRunnerCallbacks, self).on_ok(host, host_result)
def on_failed(self, host, results, ignore_errors=False): results2 = results.copy() results2.pop('invocation', None) item = results2.get('item', None) parsed = results2.get('parsed', True) module_msg = '' if not parsed: module_msg = results2.pop('msg', None) stderr = results2.pop('stderr', None) stdout = results2.pop('stdout', None) if item: msg = "failed: [%s] => (item=%s) => %s" % (host, item, utils.jsonify(results2)) else: msg = "failed: [%s] => %s" % (host, utils.jsonify(results2)) print stringc(msg, 'red') if stderr: print stringc("stderr: %s" % stderr, 'red') if stdout: print stringc("stdout: %s" % stdout, 'red') if not parsed and module_msg: print stringc("invalid output was: %s" % module_msg, 'red') if ignore_errors: print stringc("...ignoring", 'yellow') super(PlaybookRunnerCallbacks, self).on_failed(host, results, ignore_errors=ignore_errors)
def helpdefault(self, module_name): if module_name in self.modules: in_path = utils.plugins.module_finder.find_plugin(module_name) oc, a, _ = ansible.utils.module_docs.get_docstring(in_path) print stringc(oc['short_description'], 'bright gray') print 'Parameters:' for opt in oc['options'].keys(): print ' ' + stringc(opt, 'white') + ' ' + oc['options'][opt]['description'][0]
def colorize(lead, num, color): """ Print 'lead' = 'num' in 'color' """ if num != 0 and color is not None: return "%s%s%-15s" % (ansible_color.stringc(lead, color), ansible_color.stringc("=", color), ansible_color.stringc(str(num), color)) else: return "%s=%-4s" % (lead, str(num))
def on_skipped(self, host, item=None): msg = '' if item: msg = "skipping: [%s] => (item=%s)" % (host, item) else: msg = "skipping: [%s]" % host print stringc(msg, 'cyan') super(PlaybookRunnerCallbacks, self).on_skipped(host, item)
def on_async_poll(self, host, res, jid, clock): if jid not in self._async_notified: self._async_notified[jid] = clock + 1 if self._async_notified[jid] > clock: self._async_notified[jid] = clock msg = "<job %s> polling, %ss remaining"%(jid, clock) print stringc(msg, 'cyan') super(PlaybookRunnerCallbacks, self).on_async_poll(host,res,jid,clock)
def hostcolor(host, stats, color=True): if ANSIBLE_COLOR and color: if stats["failures"] != 0 or stats["unreachable"] != 0: return "%-37s" % stringc(host, "red") elif stats["changed"] != 0: return "%-37s" % stringc(host, "yellow") else: return "%-37s" % stringc(host, "green") return "%-26s" % host
def hostcolor(host, stats, color=True): if color: if stats['failures'] != 0 or stats['unreachable'] != 0: return "%-37s" % ansible_color.stringc(host, 'red') elif stats['changed'] != 0: return "%-37s" % ansible_color.stringc(host, 'yellow') else: return "%-37s" % ansible_color.stringc(host, 'green') return "%-26s" % host
def format(self, match, colored=False): formatstr = u"{0} {1}:{2}" if colored: return formatstr.format(stringc(u"[{0}]".format(match.rule.id), 'bright red'), stringc(match.filename, 'blue'), stringc(match.linenumber, 'cyan')) else: return formatstr.format(match.rule.id, match.filename, match.linenumber)
def hostcolor(host, stats): if ANSIBLE_COLOR: if stats['failures'] != 0 or stats['unreachable'] != 0: return "%-41s" % stringc(host, 'red') elif stats['changed'] != 0: return "%-41s" % stringc(host, 'yellow') else: return "%-41s" % stringc(host, 'green') return "%-30s" % host
def hostcolor(host, stats, color=True): if ANSIBLE_COLOR and color: if stats['failures'] != 0 or stats['unreachable'] != 0: return "%s" % stringc(host, 'red') elif stats['changed'] != 0: return "%s" % stringc(host, 'yellow') else: return "%s" % stringc(host, 'green') return "%s" % host
def on_unreachable(self, host, results): item = None if type(results) == dict: item = results.get('item', None) if item: msg = "fatal: [%s] => (item=%s) => %s" % (host, item, results) else: msg = "fatal: [%s] => %s" % (host, results) print stringc(msg, 'red') super(PlaybookRunnerCallbacks, self).on_unreachable(host, results)
def format(self, match, colored=False): formatstr = u"{0}:{1}: [{2}] {3}" if colored: return formatstr.format(stringc(match.filename, 'blue'), stringc(match.linenumber, 'cyan'), stringc(u"E{0}".format(match.rule.id), 'bright red'), stringc(match.message, 'red')) else: return formatstr.format(match.filename, match.linenumber, "E" + match.rule.id, match.message)
def colorize(lead, num, color): """ Print 'lead' = 'num' in 'color' :param lead: :param num: :param color: """ if num != 0 and ANSIBLE_COLOR and color is not None: return '%s%s%-15s' % (stringc(lead, color), stringc('=', color), stringc(str(num), color)) else: return '%s=%-4s' % (lead, str(num))
def format(self, match, colored=False): formatstr = u"{0}:{1}: [{2}] {3}" if colored: color.ANSIBLE_COLOR = True return formatstr.format( color.stringc(normpath(match.filename), 'blue'), color.stringc(str(match.linenumber), 'cyan'), color.stringc(u"E{0}".format(match.rule.id), 'bright red'), color.stringc(u"{0}".format(match.message), 'red')) else: return formatstr.format(normpath(match.filename), match.linenumber, "E" + match.rule.id, match.message)
def format(self, match, colored=False): formatstr = u"{0} {1}\n{2}:{3}\n{4}\n" if colored: color.ANSIBLE_COLOR = True return formatstr.format( color.stringc(u"[{0}]".format(match.rule.id), 'bright red'), color.stringc(match.message, 'red'), color.stringc(normpath(match.filename), 'blue'), color.stringc(str(match.linenumber), 'cyan'), color.stringc(u"{0}".format(match.line), 'purple')) else: return formatstr.format(match.rule.id, match.message, match.filename, match.linenumber, match.line)
def format(self, match, colored=False): formatstr = u"{0} {1}\n{2}:{3}\n{4}\n" if colored: return formatstr.format(stringc(u"[{0}]".format(match.rule.id), 'bright red'), stringc(match.message, 'red'), stringc(match.filename, 'blue'), stringc(match.linenumber, 'cyan'), stringc(match.line, 'purple')) else: return formatstr.format(match.rule.id, match.message, match.filename, match.linenumber, match.line)
def hostcolor(host, stats, color=True): """ Print the status colors :param host: The hostname :param stats: The stats from the ansible run :param color: Boolean for if colors should be shown """ if ANSIBLE_COLOR and color: if stats['failures'] != 0 or stats['unreachable'] != 0: return '%-37s' % stringc(host, 'red') elif stats['changed'] != 0: return '%-37s' % stringc(host, 'yellow') else: return '%-37s' % stringc(host, 'green') return '%-26s' % host
def display(msg, color=None, stderr=False, screen_only=False, log_only=False, runner=None): # prevent a very rare case of interlaced multiprocess I/O log_flock(runner) msg2 = msg if color: msg2 = stringc(msg, color) if not log_only: if not stderr: try: print msg2 except UnicodeEncodeError: print msg2.encode('utf-8') else: try: print >>sys.stderr, msg2 except UnicodeEncodeError: print >>sys.stderr, msg2.encode('utf-8') if constants.DEFAULT_LOG_PATH != '': while msg.startswith("\n"): msg = msg.replace("\n","") if not screen_only: if color == 'red': logger.error(msg) else: logger.info(msg) log_unflock(runner)
def display(msg, task_id, color=None, stderr=False, screen_only=False, log_only=False, runner=None): # prevent a very rare case of interlaced multiprocess I/O log_flock(runner) while msg.startswith("\n"): msg = msg.replace("\n", "") msg2 = msg + '\n' if color: msg2 = stringc(msg, color) + '\n' logger.debug('TASK_ID: {task_id} | MSG: {message}'.format(task_id=task_id, message=msg2)) # Pusblish the message on websocket redis_publisher = RedisPublisher(facility=task_id, broadcast=True) redis_message = RedisMessage(msg2) redis_publisher.publish_message(message=redis_message) # Store the message into a redis list to let the user get all # history of the logs redis_connection.rpush('tasks:' + task_id, msg2) # reset the expire of the list if settings.WS4REDIS_EXPIRE: if settings.WS4REDIS_EXPIRE > 0: redis_connection.expire('tasks:' + task_id, settings.WS4REDIS_EXPIRE) log_unflock(runner)
def on_error(self, host, err): item = err.get('item', None) msg = '' if item: msg = "err: [%s] => (item=%s) => %s" % (host, item, err) else: msg = "err: [%s] => %s" % (host, err) msg = stringc(msg, 'red') print >>sys.stderr, msg super(PlaybookRunnerCallbacks, self).on_error(host, err)
def print_stats(host, smry): ok = smry["ok"] changed = smry["changed"] unreachable = smry["unreachable"] failures = smry["failures"] if unreachable > 0 or failures > 0: status = stringc("FAILED", "red") else: status = stringc("OK", "bright green") pattern = "\tPlaybook %s, %s, %s, %s, %s" display( pattern % ( status, # hostcolor(host, smry, False), colorize("ok", ok, "bright green"), colorize("changed", changed, "yellow"), colorize("unreachable", unreachable, "red"), colorize("failed", failures, "red"), ), screen_only=True, ) display( pattern % ( status, # hostcolor(host, smry, False), colorize("ok", ok, None), colorize("changed", changed, None), colorize("unreachable", unreachable, None), colorize("failed", failures, None), ), log_only=True, )
def format(self, match, colored=False): formatstr = u"{0}:{1}: [{2}] [{3}] {4}" filename = normpath(match.filename) linenumber = str(match.linenumber) rule_id = u"E{0}".format(match.rule.id) severity = match.rule.severity message = str(match.message) if colored: color.ANSIBLE_COLOR = True filename = color.stringc(filename, 'blue') linenumber = color.stringc(linenumber, 'cyan') rule_id = color.stringc(rule_id, 'bright red') severity = color.stringc(severity, 'bright red') message = color.stringc(message, 'red') return formatstr.format( filename, linenumber, rule_id, severity, message, )
def format(self, match, colored=False): formatstr = u"{0}:{1}: [{2}] [{3}] {4}" filename = match.filename linenumber = str(match.linenumber) rule_id = u"E{0}".format(match.rule.id) severity = match.rule.severity message = str(match.message) if colored: color.ANSIBLE_COLOR = True filename = color.stringc(filename, 'blue') linenumber = color.stringc(linenumber, 'cyan') rule_id = color.stringc(rule_id, 'bright red') severity = color.stringc(severity, 'bright red') message = color.stringc(message, 'red') return formatstr.format( filename, linenumber, rule_id, severity, message, )
def on_no_hosts(self): print stringc("FATAL: no hosts matched or all hosts have already failed -- aborting\n", 'red') super(PlaybookRunnerCallbacks, self).on_no_hosts()
def verbose(msg, host=None, caplevel=2): if utils.VERBOSITY > caplevel: if host is None: print stringc(msg, 'blue') else: print stringc("<%s> %s" % (host, msg), 'blue')
def on_not_import_for_host(self, host, missing_file): msg = "%s: not importing file: %s" % (host, missing_file) print stringc(msg, 'cyan') call_callback_module('playbook_on_not_import_for_host', host, missing_file)
def on_import_for_host(self, host, imported_file): msg = "%s: importing %s" % (host, imported_file) print stringc(msg, 'cyan') call_callback_module('playbook_on_import_for_host', host, imported_file)
def on_no_hosts_remaining(self): print stringc("\nFATAL: all hosts have already failed -- aborting", 'red') call_callback_module('playbook_on_no_hosts_remaining')
def on_no_hosts_matched(self): print stringc("skipping: no hosts matched", 'cyan') call_callback_module('playbook_on_no_hosts_matched')
def on_async_failed(self, host, res, jid): msg = "<job %s> FAILED on %s"%(jid, host) print stringc(msg, 'red') super(PlaybookRunnerCallbacks, self).on_async_failed(host,res,jid)
def on_async_ok(self, host, res, jid): msg = "<job %s> finished on %s"%(jid, host) print stringc(msg, 'cyan') super(PlaybookRunnerCallbacks, self).on_async_ok(host, res, jid)