Skip to content

Commit

Permalink
custom_logger: except AnsibleError and try to get more info on failin…
Browse files Browse the repository at this point in the history
…g callback plugin
  • Loading branch information
elfiesmelfie committed Aug 16, 2024
1 parent 90c1a3e commit 0d409d2
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions callback_plugins/custom_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import os
import re


from ansible.errors import AnsibleCallbackError
from ansible.errors import AnsibleError
from ansible.plugins.callback import CallbackBase

DOCUMENTATION = '''
Expand Down Expand Up @@ -63,18 +66,35 @@ def log_task_result(self, host, result, task_name):
self.results[host][result] += 1

def log_summary_results(self, host):
# The issue is presenting itself in this method
file_path = os.path.join(self.output_dir, f"summary_results.log")
# temperorily add more detail to the output
print(self.results)

with open(file_path, 'w') as f:
f.write(f"Host: {host}\n")
f.write(f"Tasks Succeeded: {self.results[host]['passed']}\n")
f.write(f"Tasks Failed: {self.results[host]['failed']}\n")
f.write(f"Tasks Skipped: {self.results[host]['skipped']}\n")
f.write("Failed Tasks:\n")
for task_name in self.results[host]['failed_task_names']:
f.write(f" - {task_name}\n")
f.write("Succeeded Tasks:\n")
for task_name in self.results[host]['ok_task_names']:
f.write(f" - {task_name}\n")
# This is the failed issue.
# Why is it happening?
# Unknown. But does it happen for the other values.
# It is happening for compute-0
# I need to see the partial log file, so this should NOT cause a failure.
# try/catch ANY exception
# It could be a nice upgrade to use jinja2 template to render this file.
# IS the issue that there were 'f's preceeding the strings?
try:
f.write("Tasks Succeeded: {self.results[host]['passed']}\n")
f.write("Tasks Failed: {self.results[host]['failed']}\n")
f.write("Tasks Skipped: {self.results[host]['skipped']}\n")
f.write("Failed Tasks:\n")
for task_name in self.results[host]['failed_task_names']:
f.write(f" - {task_name}\n")
f.write("Succeeded Tasks:\n")
for task_name in self.results[host]['ok_task_names']:
f.write(f" - {task_name}\n")
except AnsibleError as e:
# this is probably an AnsibleCallbackError
print("Ooops, there was an error")
print(e)

def v2_runner_on_ok(self, result):
host = result._host.get_name()
Expand All @@ -89,4 +109,4 @@ def v2_runner_on_failed(self, result, ignore_errors=False):
def v2_runner_on_skipped(self, result):
host = result._host.get_name()
task_name = result._task.get_name()
self.log_task_result(host, 'skipped', task_name)
self.log_task_result(host, 'skipped', task_name)

0 comments on commit 0d409d2

Please sign in to comment.