class ModelResultsCollector(CallbackBase): """ 直接执行模块命令的回调类 """ unique_key = str(uuid.uuid4()) def __init__(self, *args, **kwargs): super(ModelResultsCollector, self).__init__(*args, **kwargs) self.redis_instance = RedisOps(settings.REDIS_HOST, settings.REDIS_PORT, 7) self.channel_key = self.unique_key def v2_runner_on_unreachable(self, result): if 'msg' in result._result: data = '<code style="color: #FF0000">\n{host} | unreachable | rc={rc} >> \n{stdout}\n</code>'.format( host=result._host.name, rc=result._result.get('rc'), stdout=result._result.get('msg')) else: data = '<code style="color: #FF0000">\n{host} | unreachable >> \n{stdout}\n</code>'.format( host=result._host.name, stdout=json.dumps(result._result, indent=4)) self.redis_instance.publish(self.channel_key, data) def v2_runner_on_ok(self, result, *args, **kwargs): if 'rc' in result._result and 'stdout' in result._result: data = '<code style="color: #008000">\n{host} | success | rc={rc} >> \n{stdout}\n</code>'.format( host=result._host.name, rc=result._result.get('rc'), stdout=result._result.get('stdout')) elif 'results' in result._result and 'rc' in result._result: data = '<code style="color: #008000">\n{host} | success | rc={rc} >> \n{stdout}\n</code>'.format( host=result._host.name, rc=result._result.get('rc'), stdout=result._result.get('results')[0]) elif 'module_stdout' in result._result and 'rc' in result._result: data = '<code style="color: #008000">\n{host} | success | rc={rc} >> \n{stdout}\n</code>'.format( host=result._host.name, rc=result._result.get('rc'), stdout=result._result.get('module_stdout').encode().decode( 'utf-8')) else: data = '<code style="color: #008000">\n{host} | success >> \n{stdout}\n</code>'.format( host=result._host.name, stdout=json.dumps(result._result, indent=4)) self.redis_instance.publish(self.channel_key, data) def v2_runner_on_failed(self, result, *args, **kwargs): if 'stderr' in result._result: data = '<code style="color: #FF0000">\n{host} | failed | rc={rc} >> \n{stdout}\n</code>'.format( host=result._host.name, rc=result._result.get('rc'), stdout=result._result.get('stderr').encode().decode('utf-8')) else: data = '<code style="color: #FF0000">\n{host} | failed >> \n{stdout}\n</code>'.format( host=result._host.name, stdout=json.dumps(result._result, indent=4)) self.redis_instance.publish(self.channel_key, data)
class PlayBookResultsCollector(CallbackBase): """ 执行playbook的回调类 """ unique_key = str(uuid.uuid4()) def __init__(self, *args, **kwargs): super(PlayBookResultsCollector, self).__init__(*args, **kwargs) self.task_skipped = [] self.task_failed = [] self.task_status = {} self.task_unreachable = [] self.task_ok = [] self.redis_instance = RedisOps(settings.REDIS_HOST, settings.REDIS_PORT, 8) self.channel_key = self.unique_key def v2_playbook_on_play_start(self, play): name = play.get_name().strip() if not name: msg = '<code style="color: #FFFFFF">\nPLAY {}\n</code>'.format('*' * 100) else: msg = '<code style="color: #FFFFFF">\nPLAY [{}] {}\n</code>'.format(name, '*' * 100) self.redis_instance.publish(self.channel_key, msg) def v2_playbook_on_task_start(self, task, is_conditional): self.redis_instance.publish(self.channel_key, '<code style="color: #FFFFFF">\nTASK [{}] {}\n</code>'.format(task.get_name(), '*' * 100)) def v2_runner_on_ok(self, result, *args, **kwargs): if result.is_changed(): data = '<code style="color: #FFFF00">[{}]=> changed\n</code>'.format(result._host.name) else: data = '<code style="color: #008000">[{}]=> ok\n</code>'.format(result._host.name) self.redis_instance.publish(self.channel_key, data) def v2_runner_on_failed(self, result, *args, **kwargs): if 'changed' in result._result: del result._result['changed'] data = '<code style="color: #FF0000">[{}]=> {}: {}\n</code>'.format(result._host.name, 'failed', self._dump_results(result._result)) self.redis_instance.publish(self.channel_key, data) def v2_runner_on_unreachable(self, result): if 'changed' in result._result: del result._result['changed'] data = '<code style="color: #FF0000">[{}]=> {}: {}\n</code>'.format(result._host.name, 'unreachable', self._dump_results(result._result)) self.redis_instance.publish(self.channel_key, data) def v2_runner_on_skipped(self, result): if 'changed' in result._result: del result._result['changed'] data = '<code style="color: #FFFF00">[{}]=> {}: {}\n</code>'.format(result._host.name, 'skipped', self._dump_results(result._result)) self.redis_instance.publish(self.channel_key, data) def v2_playbook_on_stats(self, stats): hosts = sorted(stats.processed.keys()) data = '<code style="color: #FFFFFF">\nPLAY RECAP {}\n'.format('*' * 100) self.redis_instance.publish(self.channel_key, data) for h in hosts: s = stats.summarize(h) msg = '<code style="color: #FFFFFF">{} : ok={} changed={} unreachable={} failed={} skipped={}\n</code>'.format( h, s['ok'], s['changed'], s['unreachable'], s['failures'], s['skipped']) self.redis_instance.publish(self.channel_key, msg)