Skip to content
Snippets Groups Projects
Commit 2ac09e43 authored by jurgenhaas's avatar jurgenhaas
Browse files

ansible-playbooks/general#59 Ansible output to ElasticSearch with timestamp and proper fields

parent 3cca2759
Branches
No related tags found
No related merge requests found
......@@ -56,7 +56,7 @@ class CallbackModule(CallbackBase):
"pip install fluent-logger")
else:
logging.basicConfig(level=logging.DEBUG)
self.logger = logging.getLogger('fluent.test')
self.logger = logging.getLogger('fluent.ansible')
self.logger.setLevel(logging.DEBUG)
self.handler = handler.FluentHandler(
......@@ -70,18 +70,31 @@ class CallbackModule(CallbackBase):
self.logger.addHandler(self.handler)
self.hostname = socket.gethostname()
self.session = str(uuid.uuid1())
self.company = os.getenv('ANSIBLE_COMPANY', '')
self.errors = 0
self.index = 0
def _log(self, severity, status, type, msg, extra=None, host=None, task=None):
if self.logger.isEnabledFor(severity):
self.index += 1
data = {
'host': self.hostname,
'session': self.session,
'index': self.index,
'status': status,
'msg': msg,
'ansible_type': type,
'ansible_company': self.company,
'ansible_playbook': self.playbook,
'ansible_task': task,
'ansible_host': host,
'ansible_result': extra,
}
self.logger._log(severity, json.dumps(data), None)
def v2_playbook_on_start(self, playbook):
self.playbook = playbook._file_name
data = {
'status': "OK",
'host': self.hostname,
'session': self.session,
'ansible_type': "start",
'ansible_playbook': self.playbook,
}
self.logger.info("START " + self.playbook, extra = data)
self._log(logging.INFO, 'OK', 'start', 'Start playbook %s' % self.playbook)
def v2_playbook_on_stats(self, stats):
summarize_stat = {}
......@@ -93,130 +106,19 @@ class CallbackModule(CallbackBase):
else:
status = "FAILED"
data = {
'status': status,
'host': self.hostname,
'session': self.session,
'ansible_type': "finish",
'ansible_playbook': self.playbook,
'ansible_result': json.dumps(summarize_stat), # deprecated field
}
self.logger.info(json.dumps(summarize_stat), extra = data)
self._log(logging.INFO, status, 'finish', 'Finish playbook %s' % self.playbook, extra=summarize_stat)
def v2_runner_on_ok(self, result, **kwargs):
task_name = str(result._task).replace('TASK: ','')
if task_name == 'setup':
data = {
'status': "OK",
'host': self.hostname,
'session': self.session,
'ansible_type': "setup",
'ansible_playbook': self.playbook,
'ansible_host': result._host.name,
'ansible_task': task_name,
'ansible_facts': self._dump_results(result._result) # deprecated field
}
if 'failed' in result._result.keys() and result._result['failed']:
self.errors += 1
status = 'FAILED'
elif 'changed' in result._result.keys() and result._result['changed']:
status = 'CHANGED'
else:
if 'changed' in result._result.keys():
changed = result._result['changed']
else:
changed = False
data = {
'status': "OK",
'host': self.hostname,
'session': self.session,
'ansible_changed': changed,
'ansible_type': "task",
'ansible_playbook': self.playbook,
'ansible_host': result._host.name,
'ansible_task': task_name,
'ansible_result': self._dump_results(result._result) # deprecated field
}
self.logger.info(self._dump_results(result._result), extra = data)
status = 'OK'
def v2_runner_on_skipped(self, result, **kwargs):
task_name = str(result._task).replace('TASK: ','')
data = {
'status': "SKIPPED",
'host': self.hostname,
'session': self.session,
'ansible_type': "task",
'ansible_playbook': self.playbook,
'ansible_task': task_name,
'ansible_host': result._host.name
}
self.logger.info("SKIPPED " + task_name, extra = data)
def v2_playbook_on_import_for_host(self, result, imported_file):
data = {
'status': "IMPORTED",
'host': self.hostname,
'session': self.session,
'ansible_type': "import",
'ansible_playbook': self.playbook,
'ansible_host': result._host.name,
'imported_file': imported_file
}
self.logger.info("IMPORT " + imported_file, extra = data)
def v2_playbook_on_not_import_for_host(self, result, missing_file):
data = {
'status': "NOT IMPORTED",
'host': self.hostname,
'session': self.session,
'ansible_type': "import",
'ansible_playbook': self.playbook,
'ansible_host': result._host.name,
'missing_file': missing_file
}
self.logger.info("NOT IMPORTED " + missing_file, extra = data)
self._log(logging.INFO, status, 'task', 'Task %s' % task_name, extra=result._result, host=result._host.name, task=task_name)
def v2_runner_on_failed(self, result, **kwargs):
task_name = str(result._task).replace('TASK: ','')
if 'changed' in result._result.keys():
changed = result._result['changed']
else:
changed = False
data = {
'status': "FAILED",
'host': self.hostname,
'session': self.session,
'ansible_changed': changed,
'ansible_type': "task",
'ansible_playbook': self.playbook,
'ansible_host': result._host.name,
'ansible_task': task_name,
'ansible_result': self._dump_results(result._result) # deprecated field
}
self.errors += 1
self.logger.error(self._dump_results(result._result), extra = data)
def v2_runner_on_unreachable(self, result, **kwargs):
task_name = str(result._task).replace('TASK: ','')
data = {
'status': "UNREACHABLE",
'host': self.hostname,
'session': self.session,
'ansible_type': "task",
'ansible_playbook': self.playbook,
'ansible_host': result._host.name,
'ansible_task': task_name,
'ansible_result': self._dump_results(result._result) # deprecated field
}
self.logger.error(self._dump_results(result._result), extra = data)
def v2_runner_on_async_failed(self, result, **kwargs):
task_name = str(result._task).replace('TASK: ','')
data = {
'status': "FAILED",
'host': self.hostname,
'session': self.session,
'ansible_type': "task",
'ansible_playbook': self.playbook,
'ansible_host': result._host.name,
'ansible_task': task_name,
'ansible_result': self._dump_results(result._result) # deprecated field
}
self.errors += 1
self.logger.error(self._dump_results(result._result), extra = data)
self.v2_runner_item_on_ok(result)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment