Debian Codegraph @

lib/ansible/plugins/callback/oneline.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: oneline
9 type: stdout
10 short_description: oneline Ansible screen output
11 version_added: historical
12 description:
13 - This is the output callback used by the C(-o)/C(--one-line) command line option.
14 deprecated:
15 why: The P(ansible.builtin.oneline#callback) callback is no longer recommended due to long term instability.
16 alternatives: Use a stable callback with test coverage, or consider vendoring and/or adding this plugin to a collection.
17 removed_in: "2.23"
18 """
19
20 from ansible import constants as C
21 from ansible.plugins.callback import CallbackBase
22 from ansible.template import Templar
23 from ansible.executor.task_result import CallbackTaskResult
24
25
26 class CallbackModule(CallbackBase):
27
28 """
29 This is the default callback interface, which simply prints messages
30 to stdout when new callback events are received.
31 """
32
33 CALLBACK_VERSION = 2.0
34 CALLBACK_TYPE = 'stdout'
35 CALLBACK_NAME = 'oneline'
36
37 ANSIBLE_SUPPORTS_MASKING = True
38
39 def _command_generic_msg(self, hostname, result, caption):
40 stdout = result.get('stdout', '').replace('\n', '\\n').replace('\r', '\\r')
41 if 'stderr' in result and result['stderr']:
42 stderr = result.get('stderr', '').replace('\n', '\\n').replace('\r', '\\r')
43 return "%s | %s | rc=%s | (stdout) %s (stderr) %s" % (hostname, caption, result.get('rc', -1), stdout, stderr)
44 else:
45 return "%s | %s | rc=%s | (stdout) %s" % (hostname, caption, result.get('rc', -1), stdout)
46
47 def v2_runner_on_failed(self, result: CallbackTaskResult, ignore_errors: bool = False) -> None:
48 if 'exception' in result.result:
49 error_text = Templar().template(result.result['exception']) # transform to a string
50 if self._display.verbosity < 3:
51 # extract just the actual error message from the exception text
52 error = error_text.strip().split('\n')[-1]
53 msg = "An exception occurred during task execution. To see the full traceback, use -vvv. The error was: %s" % error
54 else:
55 msg = "An exception occurred during task execution. The full traceback is:\n" + error_text.replace('\n', '')
56
57 if result.task.action in C.MODULE_NO_JSON and 'module_stderr' not in result.result:
58 self._display.display(self._command_generic_msg(result.host.get_name(), result.result, 'FAILED'), color=C.COLOR_ERROR)
59 else:
60 self._display.display(msg, color=C.COLOR_ERROR)
61
62 self._display.display("%s | FAILED! => %s" % (result.host.get_name(), self._dump_results(result.result, indent=0).replace('\n', '')),
63 color=C.COLOR_ERROR)
64
65 def v2_runner_on_ok(self, result: CallbackTaskResult) -> None:
66
67 if result.result.get('changed', False):
68 color = C.COLOR_CHANGED
69 state = 'CHANGED'
70 else:
71 color = C.COLOR_OK
72 state = 'SUCCESS'
73
74 if result.task.action in C.MODULE_NO_JSON and 'ansible_job_id' not in result.result:
75 self._display.display(self._command_generic_msg(result.host.get_name(), result.result, state), color=color)
76 else:
77 self._display.display("%s | %s => %s" % (result.host.get_name(), state, self._dump_results(result.result, indent=0).replace('\n', '')),
78 color=color)
79
80 def v2_runner_on_unreachable(self, result: CallbackTaskResult) -> None:
81 self._display.display("%s | UNREACHABLE!: %s" % (result.host.get_name(), result.result.get('msg', '')), color=C.COLOR_UNREACHABLE)
82
83 def v2_runner_on_skipped(self, result: CallbackTaskResult) -> None:
84 self._display.display("%s | SKIPPED" % (result.host.get_name()), color=C.COLOR_SKIP)