Debian Codegraph @

lib/ansible/plugins/callback/minimal.py

raw
1 # (c) 2012-2014, Michael DeHaan <[email protected]>
2 # (c) 2017 Ansible Project
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 from __future__ import annotations
6
7 DOCUMENTATION = """
8 name: minimal
9 type: stdout
10 short_description: minimal Ansible screen output
11 version_added: historical
12 description:
13 - This is the default output callback used by the ansible command (ad-hoc)
14 extends_documentation_fragment:
15 - result_format_callback
16 """
17
18 from ansible.executor.task_result import CallbackTaskResult
19 from ansible.plugins.callback import CallbackBase
20 from ansible import constants as C
21
22
23 class CallbackModule(CallbackBase):
24
25 """
26 This is the default callback interface, which simply prints messages
27 to stdout when new callback events are received.
28 """
29
30 CALLBACK_VERSION = 2.0
31 CALLBACK_TYPE = 'stdout'
32 CALLBACK_NAME = 'minimal'
33
34 ANSIBLE_SUPPORTS_MASKING = True
35
36 def _command_generic_msg(self, host, result, caption):
37 """ output the result of a command run """
38
39 buf = "%s | %s | rc=%s >>\n" % (host, caption, result.get('rc', -1))
40 buf += result.get('stdout', '')
41 buf += result.get('stderr', '')
42 buf += result.get('msg', '')
43
44 return buf + "\n"
45
46 def v2_runner_on_failed(self, result: CallbackTaskResult, ignore_errors: bool = False) -> None:
47 self._handle_warnings_and_exception(result)
48
49 if result.task.action in C.MODULE_NO_JSON and 'module_stderr' not in result.result:
50 self._display.display(self._command_generic_msg(result.host.get_name(), result.result, "FAILED"), color=C.COLOR_ERROR)
51 else:
52 self._display.display("%s | FAILED! => %s" % (result.host.get_name(), self._dump_results(result.result, indent=4)), color=C.COLOR_ERROR)
53
54 def v2_runner_on_ok(self, result: CallbackTaskResult) -> None:
55 self._handle_warnings_and_exception(result)
56
57 self._clean_results(result.result, result.task.action)
58
59 if result.result.get('changed', False):
60 color = C.COLOR_CHANGED
61 state = 'CHANGED'
62 else:
63 color = C.COLOR_OK
64 state = 'SUCCESS'
65
66 if result.task.action in C.MODULE_NO_JSON and 'ansible_job_id' not in result.result:
67 self._display.display(self._command_generic_msg(result.host.get_name(), result.result, state), color=color)
68 else:
69 self._display.display("%s | %s => %s" % (result.host.get_name(), state, self._dump_results(result.result, indent=4)), color=color)
70
71 def v2_runner_on_skipped(self, result: CallbackTaskResult) -> None:
72 self._handle_warnings_and_exception(result)
73
74 self._display.display("%s | SKIPPED" % (result.host.get_name()), color=C.COLOR_SKIP)
75
76 def v2_runner_on_unreachable(self, result: CallbackTaskResult) -> None:
77 self._handle_warnings_and_exception(result)
78
79 self._display.display("%s | UNREACHABLE! => %s" % (result.host.get_name(), self._dump_results(result.result, indent=4)), color=C.COLOR_UNREACHABLE)
80
81 def v2_on_file_diff(self, result):
82 if 'diff' in result.result and result.result['diff']:
83 self._display.display(self._get_diff(result.result['diff']))