Skip to content

Instantly share code, notes, and snippets.

@cliffano
Last active November 9, 2022 01:28
Show Gist options
  • Save cliffano/9868180 to your computer and use it in GitHub Desktop.
Save cliffano/9868180 to your computer and use it in GitHub Desktop.
# This gist is compatible with Ansible 1.x .
# For Ansible 2.x , please check out:
# - https://gist.github.com/dmsimard/cd706de198c85a8255f6
# - https://github.com/n0ts/ansible-human_log
# 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):
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):
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
Copy link

ghost commented May 10, 2016

It was raised in the past, but after updating it to support 2.0, why not pushing it to ansible (probably ansible-modules-extra)? It would be nice to have it available by default!

@Toub
Copy link

Toub commented Jul 23, 2016

+1 for pushing it to ansible

@stevenlee87
Copy link

ansible 2.1.1.0 don't support
[WARNING]: Failure using method (v2_runner_on_ok) in callback plugin (</usr/share/ansible_plugins/callback_plugins/human_log.CallbackModule object at 0x7fbf1d78f910>): runner_on_ok() takes
exactly 3 arguments (2 given)

@snevs
Copy link

snevs commented Oct 4, 2016

How to get rid of the double output? example: http://hastebin.com/obejuximun

@marcelloromani
Copy link

@snevs: I tried this with ansible 1.9 and the only way I could have double output was by using -v flags.
So omit them and the double output is gone :)

@welchwilmerck
Copy link

Re: double output. It's just how plugins work - see https://gist.github.com/dmsimard/cd706de198c85a8255f6#gistcomment-1908752

@DanyC97
Copy link

DanyC97 commented Nov 3, 2016

i have same issue like @stevenlee87 . Does it work for anyone running v 2.1+ ?

@jpic
Copy link

jpic commented Nov 7, 2016

Such an output plugin was merged in Ansible and has been released in 2.2.

To enable it, just export ANSIBLE_STDOUT_CALLBACK=debug environment variable.

ansible/ansible#16839

@DanyC97: this version should work with 2.1, otherwise try something like this: https://github.com/openshift/openshift-ansible/blob/master/callback_plugins/default.py

@mikeifomin
Copy link

@jpic big thanks to you!

@DanyC97
Copy link

DanyC97 commented Feb 7, 2017

@jpic thank you !

@yashjaiswal
Copy link

yashjaiswal commented Jun 27, 2017

@cliffano Using this, I get clean output but the output is not stored in ansible.log
Is there a way around that I use this callback, and also store logs in ansible.log, without having to print the output twice?

@nickallevato
Copy link

thank you for this!

@eromoe
Copy link

eromoe commented Nov 7, 2017

May I ask how to use this ???

ansible -m ping all -vvvv has error on my machine, but the log is too long, I have no idea how to pretty print it.

@cronnelly
Copy link

@jpic Many thanks!

@rhibiscusc
Copy link

rhibiscusc commented Dec 19, 2017

@alexlawrence do you have the error ( 'ascii' codec can't encode character u'\u03bc'
in position 696: ordinal not in range(128)) resolved? if so, would you please share the fix.

@shanedroid
Copy link

echoing the tanks here to @jpic - I had forked a version of this same plugin downstream from someone else only to find that with the version of ansible I am currently using 2.4.3.0 it was not even needed!

ansible/ansible#27078 (comment)
https://github.com/ansible/ansible/blob/v2.4.3.0-1/lib/ansible/plugins/callback/debug.py

@mahaboob
Copy link

Hi, i am new to Ansible. Can you please list the steps how to use it.
I have a playbook and i need to hook up this callback plugin to my playbook to create a log files.

any advise will be highly appreciated!!

@oldwwwwww
Copy link

The object has no property 'CallbackModule', I configured my ansible_plugins path to the callback plug-in, and now report this error.Please check it for me

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