I have defined a callback like so:
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import sys
import pprint
from ansible.plugins.callback import CallbackBase
class JSONPrettyPrintCallback(CallbackBase):
printer = pprint.PrettyPrinter(indent=4, stream=sys.stdout)
def log(self, host, category, data):
# one json blob to rule them all
self.printer.pprint({'host': host, 'category': category, 'data': data})
In my Ansible config, I have the path defined:
[defaults]
callback_plugins = callback_plugins/
However, when i run my module, I still see the default Ansible output:
10.0.0.1 | SUCCESS => {
...
}
I'm running it like this:
ansible all -u myuser -m script -a 'path/to/script.py'
Is there something I need to do to format my output properly?
If you want to replace Ansible standard output, you should set stdout_callback parameter to your callback.
Update: stdout_callback parameter will affect only ansible-playbook
CLI output, as correctly noted in the comments.
I've investigated the code a bit, ansible
CLI supports only minimal
and oneline
(if -o
option is set) stdout callbacks.
The easiest way to change the output behavior is to place ./callback_plugins/minimal.py
in your playbook folder and modify it to your needs.
Or you can patch adhoc.py
to be able to accept other stdout plugins.
There was a bug reported https://github.com/ansible/ansible/issues/16194 and Ansible
CLI was just fixed in https://github.com/ansible/ansible/pull/26098 to respect custom stdout_callback
.
To have custom callbacks working you need to specify both stdout_callback
(ANSIBLE_STDOUT_CALLBACK
env) and bin_ansible_callbacks = Yes
(ANSIBLE_LOAD_CALLBACK_PLUGINS
env).
The change should be backported to both Ansible 2.2 and 2.3