1# (c) 2017 Ansible Project
2# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4from __future__ import (absolute_import, division, print_function)
5__metaclass__ = type
6
7DOCUMENTATION = '''
8    callback: debug
9    type: stdout
10    short_description: formatted stdout/stderr display
11    description:
12      - Use this callback to sort through extensive debug output
13    version_added: "2.4"
14    extends_documentation_fragment:
15      - default_callback
16    requirements:
17      - set as stdout in configuration
18'''
19
20from ansible.plugins.callback.default import CallbackModule as CallbackModule_default
21
22
23class CallbackModule(CallbackModule_default):  # pylint: disable=too-few-public-methods,no-init
24    '''
25    Override for the default callback module.
26
27    Render std err/out outside of the rest of the result which it prints with
28    indentation.
29    '''
30    CALLBACK_VERSION = 2.0
31    CALLBACK_TYPE = 'stdout'
32    CALLBACK_NAME = 'debug'
33
34    def _dump_results(self, result, indent=None, sort_keys=True, keep_invocation=False):
35        '''Return the text to output for a result.'''
36
37        # Enable JSON identation
38        result['_ansible_verbose_always'] = True
39
40        save = {}
41        for key in ['stdout', 'stdout_lines', 'stderr', 'stderr_lines', 'msg', 'module_stdout', 'module_stderr']:
42            if key in result:
43                save[key] = result.pop(key)
44
45        output = CallbackModule_default._dump_results(self, result)
46
47        for key in ['stdout', 'stderr', 'msg', 'module_stdout', 'module_stderr']:
48            if key in save and save[key]:
49                output += '\n\n%s:\n\n%s\n' % (key.upper(), save[key])
50
51        for key, value in save.items():
52            result[key] = value
53
54        return output
55