Created
September 21, 2018 18:22
-
-
Save ParagDoke/b187d762dcc2788db25b69ab266617de to your computer and use it in GitHub Desktop.
Ansible callback plugin with best of selective and debug callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (c) 2017 Ansible Project | |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | |
from __future__ import (absolute_import, division, print_function) | |
__metaclass__ = type | |
DOCUMENTATION = ''' | |
callback: selective_debug | |
type: stdout | |
short_description: selective formatted stdout/stderr display | |
description: | |
- Use this callback like selective callback and get debug callback style stdout | |
version_added: "not yet added" | |
extends_documentation_fragment: | |
- default_callback | |
requirements: | |
- set as stdout in configuration | |
''' | |
from ansible.plugins.callback.default import CallbackModule as CallbackModule_default | |
class CallbackModule(CallbackModule_default): # pylint: disable=too-few-public-methods,no-init | |
''' | |
Override for the default callback module. | |
Render std err/out outside of the rest of the result which it prints with | |
indentation. | |
''' | |
CALLBACK_VERSION = 2.0 | |
CALLBACK_TYPE = 'stdout' | |
CALLBACK_NAME = 'selective_debug' | |
def v2_playbook_on_task_start(self, task, **kwargs): | |
pass | |
def v2_runner_on_ok(self, result, **kwargs): | |
if 'print_action' in result._task.tags or self._display.verbosity > 1: | |
print(self._dump_results(result)) | |
else: | |
print('.') | |
def _dump_results(self, original_result, indent=None, sort_keys=True, keep_invocation=False): | |
'''Return the text to output for a result.''' | |
# original_result is what playbook events receive ... of these, we use only _result and _task (for its name) | |
result = original_result._result | |
task_name = original_result._task.get_name() | |
# Enable JSON identation | |
result['_ansible_verbose_always'] = True | |
save = {} | |
# for debug module, avoid printing changed: false every time | |
for key in ['stdout', 'stdout_lines', 'stderr', 'stderr_lines', 'msg', 'module_stdout', 'module_stderr', 'changed']: | |
if key in result: | |
save[key] = result.pop(key) | |
output = CallbackModule_default._dump_results(self, result) | |
# no need to print empty map | |
if output == '{}': | |
output = '' | |
for key in ['stdout', 'stderr', 'msg', 'module_stdout', 'module_stderr']: | |
if key in save and save[key]: | |
output += '[%s] %s:\n%s' % (task_name, key, save[key]) | |
for key, value in save.items(): | |
result[key] = value | |
return output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ansible.cfg
:print_action
. Example: