Skip to content

Instantly share code, notes, and snippets.

@drazul
Forked from cliffano/human_log.py
Last active March 30, 2016 09:44
Show Gist options
  • Save drazul/144587760c41094a48d1 to your computer and use it in GitHub Desktop.
Save drazul/144587760c41094a48d1 to your computer and use it in GitHub Desktop.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from ansible import constants
FIELDS = ['cmd', 'command', 'start', 'end', 'delta', 'msg', 'stdout', 'stderr']
class CallbackModule(object):
def human_log(self, data):
if constants.DEFAULT_LOG_PATH != '':
f = open(constants.DEFAULT_LOG_PATH, 'a')
if type(data) == type(dict()):
for field in FIELDS:
if field in data.keys():
string = "%s" % self._format_output(data[field])
output = u'\n{0}:\n{1}'.format(field, string.replace("\\n", "\n"))
print output
if constants.DEFAULT_LOG_PATH != '':
f.write(output)
if constants.DEFAULT_LOG_PATH != '':
f.write('\n\n')
def _format_output(self, output):
# If output is a dict
if type(output) == dict:
return json.dumps(output, indent=2)
# If output is a list of dicts
if type(output) == list and type(output[0]) == dict:
# This gets a little complicated because it potentially means
# nested results, usually because of with_items.
real_output = list()
for index, item in enumerate(output):
copy = item
if type(item) == dict:
for field in FIELDS:
if field in item.keys():
copy[field] = self._format_output(item[field])
real_output.append(copy)
return json.dumps(output, indent=2)
# If output is a list of strings
if type(output) == list and type(output[0]) != dict:
# Strip newline characters
real_output = list()
for item in output:
if "\n" in item:
for string in item.split("\n"):
real_output.append(string)
else:
real_output.append(item)
# Reformat lists with line breaks only if the total length is
# >75 chars
if len("".join(real_output)) > 75:
return "\n" + "\n".join(real_output)
else:
return " ".join(real_output)
# Otherwise it's a string, just return it
return output
def on_any(self, *args, **kwargs):
pass
def runner_on_failed(self, host, res, ignore_errors=False):
self.human_log(res)
def runner_on_ok(self, host, res):
self.human_log(res)
def runner_on_error(self, host, msg):
pass
def runner_on_skipped(self, host, item=None):
pass
def runner_on_unreachable(self, host, res):
self.human_log(res)
def runner_on_no_hosts(self):
pass
def runner_on_async_poll(self, host, res, jid, clock):
self.human_log(res)
def runner_on_async_ok(self, host, res, jid):
self.human_log(res)
def runner_on_async_failed(self, host, res, jid):
self.human_log(res)
def playbook_on_start(self):
pass
def playbook_on_notify(self, host, handler):
pass
def playbook_on_no_hosts_matched(self):
pass
def playbook_on_no_hosts_remaining(self):
pass
def playbook_on_task_start(self, name, is_conditional):
pass
def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False,
salt_size=None, salt=None, default=None):
pass
def playbook_on_setup(self):
pass
def playbook_on_import_for_host(self, host, imported_file):
pass
def playbook_on_not_import_for_host(self, host, missing_file):
pass
def playbook_on_play_start(self, pattern):
pass
def playbook_on_stats(self, stats):
pass
@snevs
Copy link

snevs commented Mar 30, 2016

Hi,

It doesn't work at all for me:

changed: [192.168.1.11] => {"changed": true, "cmd": "uname -r", "delta": "0:00:00.007840", "end": "2016-03-30 09:43:36.564371", "rc": 0, "start": "2016-03-30 09:43:36.556531", "stderr": "", "stdout": "2.6.39-400.250.2.el6uek.x86_64", "warnings": []}

PLAY RECAP ********************************************************************
192.168.1.11 : ok=3 changed=3 unreachable=0 failed=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment