Commit d2a2521b authored by Alain Takoudjou's avatar Alain Takoudjou

playbook: improve log_parse pluging, fix warning and indendation

parent 372ff44a
...@@ -15,58 +15,72 @@ class CallbackModule(baseModule): ...@@ -15,58 +15,72 @@ class CallbackModule(baseModule):
""" """
logs playbook results, per host, in /var/log/ansible/hosts logs playbook results, per host, in /var/log/ansible/hosts
""" """
log_path = '/var/log/ansible/hosts' log_path = '/var/log/ansible/hosts'
fd_list = {}
def __init__(self): def __init__(self):
if ANSIBLE_VERSION > 1: if ANSIBLE_VERSION > 1:
super(baseModule, self).__init__() super(CallbackModule, self).__init__()
if not os.path.exists(self.log_path): if not os.path.exists(self.log_path):
os.makedirs(self.log_path) os.makedirs(self.log_path)
else: else:
for filename in os.listdir(self.log_path): for filename in os.listdir(self.log_path):
filepath = os.path.join(self.log_path, filename) filepath = os.path.join(self.log_path, filename)
if os.path.exists(filepath) and os.path.isfile(filepath): if os.path.exists(filepath) and os.path.isfile(filepath):
os.unlink(filepath) os.unlink(filepath)
def writeLog(self, host, category, content):
if not self.fd_list.has_key(category):
self.fd_list[category] = open(
os.path.join(self.log_path, '%s_%s' % (host, category)), "a"
)
self.fd_list[category].write(content + '\n')
def log(self, host, category, data, ignore_errors=False): def log(self, host, category, data, ignore_errors=False):
if host == "localhost":
host = "127.0.0.1" # keep compatibility
if type(data) == dict:
if '_ansible_verbose_override' in data:
# avoid logging extraneous data
return
if type(data) == dict: data = data.copy()
if '_ansible_verbose_override' in data: content = json.dumps(data)
# avoid logging extraneous data
data = {} if ignore_errors:
else: category = '%s_IGNORED' % category
data = data.copy() self.writeLog(host, category, content)
data = json.dumps(data)
def _stats(self, stats):
if ignore_errors: for key in self.fd_list:
category = '%s_IGNORED' % category self.fd_list[key].close()
path = os.path.join("/var/log/ansible/hosts", '%s_%s' % (host, category))
with open(path, "a") as fd:
fd.write(data)
fd.write('\n')
def runner_on_failed(self, host, res, ignore_errors=False): def runner_on_failed(self, host, res, ignore_errors=False):
self.log(host, 'FAILED', res, ignore_errors) self.log(host, 'FAILED', res, ignore_errors)
def runner_on_ok(self, host, res): def runner_on_ok(self, host, res):
self.log(host, 'OK', res) self.log(host, 'OK', res)
def runner_on_skipped(self, host, item=None): def runner_on_skipped(self, host, item=None):
pass pass
def runner_on_unreachable(self, host, res): def runner_on_unreachable(self, host, res):
self.log(host, 'UNREACHABLE', res) self.log(host, 'UNREACHABLE', res)
def runner_on_async_failed(self, host, res, jid): def runner_on_async_failed(self, host, res, jid):
self.log(host, 'ASYNC_FAILED', res) self.log(host, 'ASYNC_FAILED', res)
def playbook_on_import_for_host(self, host, imported_file): def playbook_on_import_for_host(self, host, imported_file):
self.log(host, 'IMPORTED', imported_file) self.log(host, 'IMPORTED', imported_file)
def playbook_on_not_import_for_host(self, host, missing_file): def playbook_on_not_import_for_host(self, host, missing_file):
self.log(host, 'NOTIMPORTED', missing_file) self.log(host, 'NOTIMPORTED', missing_file)
def playbook_on_stats(self, stats):
self._stats(stats)
EOF EOF
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment