-
-
Save dmsimard/cd706de198c85a8255f6 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/>. | |
# 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 | |
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', | |
'stderr', 'results'] | |
class CallbackModule(CallbackBase): | |
def human_log(self, data): | |
if type(data) == dict: | |
for field in FIELDS: | |
if field in data.keys() and data[field]: | |
output = self._format_output(data[field]) | |
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) | |
# 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 |
Good stuff.
Hi I am new to Ansible.
I am also tired with row format of debug log. This plugin will be very useful.
can you please let me know how to configure it ? Where to put this py file and how to use in ansible playbook.
My aim is to log output of task into one file. located on controlled machine.
Thanks in advance.
Thanks for the plugin. There are two problems though...
- The human readable output doesn't apply to the text written in the ansible logfile (if enabled).
- If you run a command in ansible with "ignore_errors: yes", in case of error the non-formated ouput is printed, and then the formated ouput follows...
I am copy pasting part of my output in order for you to better understand the second problem.
/tdpvmware/fcm/fmcima -l /home/tdpvmware/tdpvmware/config -N DEVICE_CLASS:FCBACKUP_SLM.0 -f backup' returned with code 0.", "#CHILD lunid:6005076801828070F800000000000211", "#PARENT lunname:V-slmesx01_lun0002", "#WARNING FMM1554W The agent 'acsgen V. 4.1.4.0' terminated with exit code 1.", "#INFO FMM0020I End of program at: Fri Mar 11 16:23:03 2016.", "#INFO FMM0021I Elapsed time: 01 min 38 sec.", "#INFO FMM0024I Return code is: 1.", "#PARAM STATUS=warning", "#END RUN 636 20160311162303884", "#END TASK 114", "#INFO FMM16014I The return code is 1.", "#END"], "warnings": []}
...ignoring
[after that part the ouput is displayed again but now formated as it should]
cmd: /opt/tivoli/tsm/tdpvmware/common/scripts/vmcli -f backup -T 114 --runnowstart: 2016-03-11 16:21:24.221963
end: 2016-03-11 16:23:05.543694
delta: 0:01:41.321731
stdout: #TASK 114 backup 20131008162736229
I'm seeing double output. Formatted and unformatted.
Re: double output. Plugins can only get in line to get their shot at processing whatever object is being passed around, not override other plugins. The vendor provided plugins/callback/default.py always runs, then this plugin gets its time with 'result', so you get what both produce.
The real fix is actually already sitting in vendor's plugins/callback/init.py, def _dump_results. Set indent to 4 instead of None or figure out how to set verbose_always. N. B. I'm looking at v2.1.2.0 while there seems to be work in this area on the devel branch. Maybe a way to activate indent=4 is already implemented.
@kamalgarg, from Configuring Callback Plugins in Ansible documentation:
You can activate a custom callback by either dropping it into a callback_plugins directory adjacent to your play or inside a role or by putting it in one of the callback directory sources configured in ansible.cfg.
Plugins are loaded in alphanumeric order; for example, a plugin implemented in a file named 1_first.py would run before a plugin file named 2_second.py.
Most callbacks shipped with Ansible are disabled by default and need to be whitelisted in your ansible.cfg file in order to function. For example:
#callback_whitelist = timer, mail, mycallbackplugin
So create a callback_plugins/
directory next to your playbooks, copy human_log.py
there, and add this to your ansible.cfg
:
callback_whitelist = human_log
FYI I added support for hiding output on tasks that have no_log: True
in this fork.
Hi,
Thanks a lot for youre efforts. I am getting below warning and logs are not being shown in human readable format:
[WARNING]: Failure using method (v2_runner_on_ok) in callback plugin (</home/i050396/new_upgrade/multidc/callback/human_log.CallbackModule object at 0x7fdb7280c828>): name 'unicode' is not
defined
Can you please help with it ?
Works fine for me with ansible 2.3.2
, thanks for this.
Have always output in double ... :-(
ansible v2.3.1
Python v2.7.5 on Red Hat v7.1
This works great, but does anyone know how to get this output to log to the file specified by log_path
in ansible.cfg?
Nice works 4 me, but my install pm2-syslog task returns something quite ugly
????????????????????????????????????????????????????????????????????????????????????????
? App name ? id ? mode ? pid ? status ? restart ? uptime ? cpu ? mem ? user ? watching ?
????????????????????????????????????????????????????????????????????????????????????????
Module activated
???????????????????????????????????????????????????????????????????????????????????
? Module ? version ? target PID ? status ? restart ? cpu ? memory ? user ?
???????????????????????????????????????????????????????????????????????????????????
? pm2-syslog ? N/A ? N/A ? online ? 0 ? 0% ? 11.625 MB ? node ?
???????????????????????????????????????????????????????????????????????????????????
Use `pm2 show <id|name>` to get more details about an app
Anybody some ideas to fix those ascii characters?
EDIT:
That solution works. Human readable and with UTF8 special ascii characters.
https://github.com/n0ts/ansible-human_log/blob/master/human_log.py
stdout gets properly formatted but (yum) results does not. Any ideas?
worth noting that this callback plugin may no longer be needed as the debug plugin can be used by default now, I have just switched to using that instead:
ansible/ansible#27078 (comment)
https://github.com/ansible/ansible/blob/v2.4.3.0-1/lib/ansible/plugins/callback/debug.py
https://gist.github.com/cliffano/9868180#gistcomment-1915662
Great, thank you.