Skip to content

Instantly share code, notes, and snippets.

@dmsimard
Forked from cliffano/human_log.py
Last active December 8, 2021 13:51
Show Gist options
  • Select an option

  • Save dmsimard/cd706de198c85a8255f6 to your computer and use it in GitHub Desktop.

Select an option

Save dmsimard/cd706de198c85a8255f6 to your computer and use it in GitHub Desktop.

Revisions

  1. David Moreau Simard revised this gist May 21, 2016. No changes.
  2. dmsimard revised this gist Dec 4, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion human_log.py
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ class CallbackModule(CallbackBase):
    def human_log(self, data):
    if type(data) == dict:
    for field in FIELDS:
    if field in data.keys():
    if field in data.keys() and data[field]:
    output = self._format_output(data[field])
    print("\n{0}: {1}".format(field, output.replace("\\n","\n")))

  3. dmsimard revised this gist Dec 3, 2015. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion human_log.py
    Original file line number Diff line number Diff line change
    @@ -37,6 +37,10 @@ def human_log(self, data):
    print("\n{0}: {1}".format(field, output.replace("\\n","\n")))

    def _format_output(self, output):
    # Strip unicode
    if type(output) == unicode:
    output = output.encode('ascii', 'replace')

    # If output is a dict
    if type(output) == dict:
    return json.dumps(output, indent=2)
    @@ -140,4 +144,4 @@ def playbook_on_play_start(self, pattern):
    pass

    def playbook_on_stats(self, stats):
    pass
    pass
  4. dmsimard revised this gist Dec 3, 2015. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions human_log.py
    Original file line number Diff line number Diff line change
    @@ -37,10 +37,6 @@ def human_log(self, data):
    print("\n{0}: {1}".format(field, output.replace("\\n","\n")))

    def _format_output(self, output):
    # Strip unicode
    if type(output) == unicode:
    output = output.encode('ascii', 'replace')

    # If output is a dict
    if type(output) == dict:
    return json.dumps(output, indent=2)
  5. dmsimard revised this gist Nov 25, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions human_log.py
    Original file line number Diff line number Diff line change
    @@ -37,6 +37,10 @@ def human_log(self, data):
    print("\n{0}: {1}".format(field, output.replace("\\n","\n")))

    def _format_output(self, output):
    # Strip unicode
    if type(output) == unicode:
    output = output.encode('ascii', 'replace')

    # If output is a dict
    if type(output) == dict:
    return json.dumps(output, indent=2)
  6. dmsimard revised this gist Nov 24, 2015. 1 changed file with 40 additions and 15 deletions.
    55 changes: 40 additions & 15 deletions human_log.py
    Original file line number Diff line number Diff line change
    @@ -11,14 +11,17 @@
    # You should have received a copy of the GNU General Public License
    # along with this program. If not, see <http://www.gnu.org/licenses/>.

    # Adapted and improved from: https://gist.github.com/cliffano/9868180
    # Made compatible with Ansible v2, tailored to standard core callbacks
    # Inspired from: https://gist.github.com/cliffano/9868180
    # Improved and made compatible with Ansible v2

    from __future__ import (absolute_import, division, print_function)
    __metaclass__ = type
    import pprint

    from ansible.plugins.callback import CallbackBase
    try:
    import simplejson as json
    except ImportError:
    import json

    # Fields to reformat output for
    FIELDS = ['cmd', 'command', 'start', 'end', 'delta', 'msg', 'stdout',
    @@ -31,23 +34,44 @@ def human_log(self, data):
    for field in FIELDS:
    if field in data.keys():
    output = self._format_output(data[field])
    print("\n{0}: {1}".format(field, output))
    print("\n{0}: {1}".format(field, output.replace("\\n","\n")))

    def _format_output(self, output):
    # If output is a dict or a list of dicts
    if type(output) == dict or (type(output) == list and
    type(output[0]) == dict):
    pp = pprint.PrettyPrinter(indent=4)
    return pp.pprint(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]) == str or
    type(output[0] == unicode)):
    # Reformat lists with line breaks if the total length is >75 chars
    if len("".join(output)) > 75:
    return "\n" + "\n".join(output)
    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(output)
    return " ".join(real_output)

    # Otherwise it's a string, just return it
    return output
    @@ -61,6 +85,7 @@ def runner_on_failed(self, host, res, ignore_errors=False):
    def runner_on_ok(self, host, res):
    self.human_log(res)


    def runner_on_error(self, host, msg):
    pass

  7. dmsimard revised this gist Nov 24, 2015. 1 changed file with 28 additions and 11 deletions.
    39 changes: 28 additions & 11 deletions human_log.py
    Original file line number Diff line number Diff line change
    @@ -11,29 +11,46 @@
    # You should have received a copy of the GNU General Public License
    # along with this program. If not, see <http://www.gnu.org/licenses/>.

    # Adapted from: https://gist.github.com/cliffano/9868180
    # Adapted and improved from: https://gist.github.com/cliffano/9868180
    # Made compatible with Ansible v2, tailored to standard core callbacks

    from __future__ import (absolute_import, division, print_function)
    __metaclass__ = type
    import pprint

    from ansible.plugins.callback import CallbackBase

    # Fields to reformat output for
    FIELDS = [ 'cmd', 'command', 'start', 'end', 'delta', 'msg', 'stdout',
    'stderr', 'results' ]
    FIELDS = ['cmd', 'command', 'start', 'end', 'delta', 'msg', 'stdout',
    'stderr', 'results']


    class CallbackModule(CallbackBase):
    def human_log(self, res):
    if type(res) == dict:
    def human_log(self, data):
    if type(data) == dict:
    for field in FIELDS:
    if field in res.keys():
    if field == "results":
    output = "\n".join(res[field])
    else:
    output = res[field]
    print(u'{0}:\n{1}'.format(field, output))
    if field in data.keys():
    output = self._format_output(data[field])
    print("\n{0}: {1}".format(field, output))

    def _format_output(self, output):
    # If output is a dict or a list of dicts
    if type(output) == dict or (type(output) == list and
    type(output[0]) == dict):
    pp = pprint.PrettyPrinter(indent=4)
    return pp.pprint(output)

    # If output is a list of strings
    if type(output) == list and (type(output[0]) == str or
    type(output[0] == unicode)):
    # Reformat lists with line breaks if the total length is >75 chars
    if len("".join(output)) > 75:
    return "\n" + "\n".join(output)
    else:
    return " ".join(output)

    # Otherwise it's a string, just return it
    return output

    def on_any(self, *args, **kwargs):
    pass
  8. dmsimard revised this gist Nov 24, 2015. 1 changed file with 65 additions and 53 deletions.
    118 changes: 65 additions & 53 deletions human_log.py
    Original file line number Diff line number Diff line change
    @@ -10,80 +10,92 @@
    #
    # You should have received a copy of the GNU General Public License
    # along with this program. If not, see <http://www.gnu.org/licenses/>.

    FIELDS = ['cmd', 'command', 'start', 'end', 'delta', 'msg', 'stdout', 'stderr']

    def human_log(res):
    # Adapted from: https://gist.github.com/cliffano/9868180
    # Made compatible with Ansible v2, tailored to standard core callbacks

    if type(res) == type(dict()):
    for field in FIELDS:
    if field in res.keys():
    # use default encoding, check out sys.setdefaultencoding
    print u'\n{0}:\n{1}'.format(field, res[field])
    # or use specific encoding, e.g. utf-8
    #print '\n{0}:\n{1}'.format(field, res[field].encode('utf-8'))
    from __future__ import (absolute_import, division, print_function)
    __metaclass__ = type

    class CallbackModule(object):
    from ansible.plugins.callback import CallbackBase

    def on_any(self, *args, **kwargs):
    pass
    # Fields to reformat output for
    FIELDS = [ 'cmd', 'command', 'start', 'end', 'delta', 'msg', 'stdout',
    'stderr', 'results' ]

    def runner_on_failed(self, host, res, ignore_errors=False):
    human_log(res)

    def runner_on_ok(self, host, res):
    human_log(res)
    class CallbackModule(CallbackBase):
    def human_log(self, res):
    if type(res) == dict:
    for field in FIELDS:
    if field in res.keys():
    if field == "results":
    output = "\n".join(res[field])
    else:
    output = res[field]
    print(u'{0}:\n{1}'.format(field, output))

    def runner_on_error(self, host, msg):
    pass
    def on_any(self, *args, **kwargs):
    pass

    def runner_on_skipped(self, host, item=None):
    pass
    def runner_on_failed(self, host, res, ignore_errors=False):
    self.human_log(res)

    def runner_on_unreachable(self, host, res):
    human_log(res)
    def runner_on_ok(self, host, res):
    self.human_log(res)

    def runner_on_no_hosts(self):
    pass
    def runner_on_error(self, host, msg):
    pass

    def runner_on_async_poll(self, host, res, jid, clock):
    human_log(res)
    def runner_on_skipped(self, host, item=None):
    pass

    def runner_on_async_ok(self, host, res, jid):
    human_log(res)
    def runner_on_unreachable(self, host, res):
    self.human_log(res)

    def runner_on_async_failed(self, host, res, jid):
    human_log(res)
    def runner_on_no_hosts(self):
    pass

    def playbook_on_start(self):
    pass
    def runner_on_async_poll(self, host, res, jid, clock):
    self.human_log(res)

    def playbook_on_notify(self, host, handler):
    pass
    def runner_on_async_ok(self, host, res, jid):
    self.human_log(res)

    def playbook_on_no_hosts_matched(self):
    pass
    def runner_on_async_failed(self, host, res, jid):
    self.human_log(res)

    def playbook_on_no_hosts_remaining(self):
    pass
    def playbook_on_start(self):
    pass

    def playbook_on_task_start(self, name, is_conditional):
    pass
    def playbook_on_notify(self, host, handler):
    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_no_hosts_matched(self):
    pass

    def playbook_on_setup(self):
    pass
    def playbook_on_no_hosts_remaining(self):
    pass

    def playbook_on_import_for_host(self, host, imported_file):
    pass
    def playbook_on_task_start(self, name, is_conditional):
    pass

    def playbook_on_not_import_for_host(self, host, missing_file):
    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_play_start(self, pattern):
    pass
    def playbook_on_setup(self):
    pass

    def playbook_on_stats(self, stats):
    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
  9. @cliffano cliffano revised this gist Sep 13, 2015. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions human_log.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,16 @@
    # 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/>.

    FIELDS = ['cmd', 'command', 'start', 'end', 'delta', 'msg', 'stdout', 'stderr']

    def human_log(res):
  10. @cliffano cliffano revised this gist Nov 16, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions human_log.py
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,10 @@ def human_log(res):
    if type(res) == type(dict()):
    for field in FIELDS:
    if field in res.keys():
    # use default encoding, check out sys.setdefaultencoding
    print u'\n{0}:\n{1}'.format(field, res[field])
    # or use specific encoding, e.g. utf-8
    #print '\n{0}:\n{1}'.format(field, res[field].encode('utf-8'))

    class CallbackModule(object):

  11. @cliffano cliffano revised this gist Nov 9, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion human_log.py
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ def human_log(res):
    if type(res) == type(dict()):
    for field in FIELDS:
    if field in res.keys():
    print '\n{0}:\n{1}'.format(field, res[field])
    print u'\n{0}:\n{1}'.format(field, res[field])

    class CallbackModule(object):

  12. @cliffano cliffano revised this gist May 13, 2014. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions human_log.py
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,11 @@
    FIELDS = ['cmd', 'command', 'start', 'end', 'delta', 'msg', 'stdout', 'stderr']

    def human_log(res):
    fields = ['cmd', 'command', 'start', 'end', 'delta', 'msg', 'stdout', 'stderr']
    for field in fields:
    if field in res:
    print '\n{0}:\n{1}'.format(field, res[field])

    if type(res) == type(dict()):
    for field in FIELDS:
    if field in res.keys():
    print '\n{0}:\n{1}'.format(field, res[field])

    class CallbackModule(object):

  13. @cliffano cliffano revised this gist Apr 11, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion human_log.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    def human_log(res):
    fields = ['cmd', 'start', 'end', 'delta', 'stdout', 'stderr']
    fields = ['cmd', 'command', 'start', 'end', 'delta', 'msg', 'stdout', 'stderr']
    for field in fields:
    if field in res:
    print '\n{0}:\n{1}'.format(field, res[field])
  14. @cliffano cliffano created this gist Mar 30, 2014.
    70 changes: 70 additions & 0 deletions human_log.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    def human_log(res):
    fields = ['cmd', 'start', 'end', 'delta', 'stdout', 'stderr']
    for field in fields:
    if field in res:
    print '\n{0}:\n{1}'.format(field, res[field])

    class CallbackModule(object):

    def on_any(self, *args, **kwargs):
    pass

    def runner_on_failed(self, host, res, ignore_errors=False):
    human_log(res)

    def runner_on_ok(self, host, res):
    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):
    human_log(res)

    def runner_on_no_hosts(self):
    pass

    def runner_on_async_poll(self, host, res, jid, clock):
    human_log(res)

    def runner_on_async_ok(self, host, res, jid):
    human_log(res)

    def runner_on_async_failed(self, host, res, jid):
    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