jumpserver/apps/ops/ansible/callback.py

145 lines
4.3 KiB
Python
Raw Normal View History

from collections import defaultdict
2018-03-30 14:03:43 +00:00
2022-09-30 10:49:45 +00:00
class DefaultCallback:
2022-11-01 03:52:51 +00:00
STATUS_MAPPER = {
'successful': 'success',
'failure': 'failed',
'failed': 'failed',
2022-11-01 03:52:51 +00:00
'running': 'running',
'pending': 'pending',
'unknown': 'unknown'
}
2022-09-30 10:49:45 +00:00
def __init__(self):
self.result = dict(
ok=defaultdict(dict),
2022-09-30 10:49:45 +00:00
failures=defaultdict(dict),
dark=defaultdict(dict),
2022-09-30 10:49:45 +00:00
skipped=defaultdict(dict),
)
2022-09-30 10:49:45 +00:00
self.summary = dict(
ok=[],
failures={},
dark={},
skipped=[],
)
2022-10-08 08:55:14 +00:00
self.status = 'running'
2022-09-30 10:49:45 +00:00
self.finished = False
2022-10-17 03:22:21 +00:00
@property
def host_results(self):
results = {}
for state, hosts in self.result.items():
for host, items in hosts.items():
results[host] = items
return results
2022-09-30 10:49:45 +00:00
def is_success(self):
2022-11-01 03:52:51 +00:00
return self.status != 'success'
2022-09-29 12:44:45 +00:00
2022-09-30 10:49:45 +00:00
def event_handler(self, data, **kwargs):
2022-09-29 12:44:45 +00:00
event = data.get('event', None)
2022-09-30 10:49:45 +00:00
if not event:
return
event_data = data.get('event_data', {})
host = event_data.get('remote_addr', '')
task = event_data.get('task', '')
res = event_data.get('res', {})
handler = getattr(self, event, self.on_any)
handler(event_data, host=host, task=task, res=res)
def runner_on_ok(self, event_data, host=None, task=None, res=None):
detail = {
'action': event_data.get('task_action', ''),
'res': res,
'rc': res.get('rc', 0),
'stdout': res.get('stdout', ''),
}
self.result['ok'][host][task] = detail
2022-10-28 10:28:41 +00:00
def runner_on_failed(self, event_data, host=None, task=None, res=None, **kwargs):
2022-09-30 10:49:45 +00:00
detail = {
'action': event_data.get('task_action', ''),
'res': res,
'rc': res.get('rc', 0),
'stdout': res.get('stdout', ''),
'stderr': ';'.join([res.get('stderr', ''), res.get('msg', '')]).strip(';')
}
self.result['failures'][host][task] = detail
2017-03-05 15:30:14 +00:00
2022-09-30 10:49:45 +00:00
def runner_on_skipped(self, event_data, host=None, task=None, **kwargs):
detail = {
'action': event_data.get('task_action', ''),
'res': {},
'rc': 0,
}
self.result['skipped'][host][task] = detail
def runner_on_unreachable(self, event_data, host=None, task=None, res=None, **kwargs):
detail = {
'action': event_data.get('task_action', ''),
'res': res,
'rc': 255,
'stderr': ';'.join([res.get('stderr', ''), res.get('msg', '')]).strip(';')
}
self.result['dark'][host][task] = detail
2017-03-05 15:30:14 +00:00
2022-09-30 10:49:45 +00:00
def runner_on_start(self, event_data, **kwargs):
pass
2022-10-09 12:54:11 +00:00
def runner_retry(self, event_data, **kwargs):
pass
2022-09-30 10:49:45 +00:00
def runner_on_file_diff(self, event_data, **kwargs):
pass
2022-09-30 10:49:45 +00:00
def runner_item_on_failed(self, event_data, **kwargs):
pass
2022-09-30 10:49:45 +00:00
def runner_item_on_skipped(self, event_data, **kwargs):
pass
2022-09-30 10:49:45 +00:00
def playbook_on_play_start(self, event_data, **kwargs):
2018-12-10 11:51:35 +00:00
pass
2022-09-30 10:49:45 +00:00
def playbook_on_stats(self, event_data, **kwargs):
failed = []
for i in ['dark', 'failures']:
for host, tasks in self.result[i].items():
failed.append(host)
error = ''
for task, detail in tasks.items():
error += f'{task}: {detail["stderr"]};'
self.summary[i][host] = error.strip(';')
self.summary['ok'] = list(set(self.result['ok'].keys()) - set(failed))
self.summary['skipped'] = list(set(self.result['skipped'].keys()) - set(failed))
def playbook_on_include(self, event_data, **kwargs):
2017-03-05 15:30:14 +00:00
pass
2022-09-30 10:49:45 +00:00
def playbook_on_notify(self, event_data, **kwargs):
pass
2017-03-05 15:30:14 +00:00
2022-09-30 10:49:45 +00:00
def playbook_on_vars_prompt(self, event_data, **kwargs):
pass
2017-03-05 15:30:14 +00:00
2022-09-30 10:49:45 +00:00
def playbook_on_handler_task_start(self, event_data, **kwargs):
pass
2017-03-05 15:30:14 +00:00
2022-09-30 10:49:45 +00:00
def playbook_on_no_hosts_matched(self, event_data, **kwargs):
pass
2017-03-05 15:30:14 +00:00
2022-09-30 10:49:45 +00:00
def playbook_on_no_hosts_remaining(self, event_data, **kwargs):
pass
2017-03-05 15:30:14 +00:00
2022-09-30 10:49:45 +00:00
def warning(self, event_data, **kwargs):
pass
2017-03-05 15:30:14 +00:00
2022-09-30 10:49:45 +00:00
def on_any(self, event_data, **kwargs):
pass
2017-03-05 15:30:14 +00:00
2022-09-30 10:49:45 +00:00
def status_handler(self, data, **kwargs):
2022-11-01 03:52:51 +00:00
status = data.get('status', '')
self.status = self.STATUS_MAPPER.get(status, 'unknown')