| 1 |
# Copyright (c) 2023, Al Bowles <@akatch> |
| 2 |
# Copyright (c) 2012-2014, Michael DeHaan <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
# Make coding more python3-ish |
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
name: unixy |
| 11 |
type: stdout |
| 12 |
author: Al Bowles (@akatch) |
| 13 |
short_description: Condensed Ansible output |
| 14 |
description: |
| 15 |
- Consolidated Ansible output in the style of LINUX/UNIX startup logs. |
| 16 |
extends_documentation_fragment: |
| 17 |
- ansible.builtin.default_callback |
| 18 |
requirements: |
| 19 |
- set as stdout in configuration |
| 20 |
""" |
| 21 |
|
| 22 |
from os.path import basename |
| 23 |
|
| 24 |
from ansible import constants as C |
| 25 |
from ansible import context |
| 26 |
from ansible.module_utils.common.text.converters import to_text |
| 27 |
from ansible.plugins.callback.default import CallbackModule as CallbackModule_default |
| 28 |
from ansible.utils.color import colorize, hostcolor |
| 29 |
|
| 30 |
|
| 31 |
class CallbackModule(CallbackModule_default): |
| 32 |
""" |
| 33 |
Design goals: |
| 34 |
- Print consolidated output that looks like a *NIX startup log |
| 35 |
- Defaults should avoid displaying unnecessary information wherever possible |
| 36 |
|
| 37 |
TODOs: |
| 38 |
- Only display task names if the task runs on at least one host |
| 39 |
- Add option to display all hostnames on a single line in the appropriate result color (failures may have a separate line) |
| 40 |
- Consolidate stats display |
| 41 |
- Don't show play name if no hosts found |
| 42 |
""" |
| 43 |
|
| 44 |
CALLBACK_VERSION = 2.0 |
| 45 |
CALLBACK_TYPE = "stdout" |
| 46 |
CALLBACK_NAME = "community.general.unixy" |
| 47 |
|
| 48 |
def _run_is_verbose(self, result): |
| 49 |
return ( |
| 50 |
self._display.verbosity > 0 or "_ansible_verbose_always" in result._result |
| 51 |
) and "_ansible_verbose_override" not in result._result |
| 52 |
|
| 53 |
def _get_task_display_name(self, task): |
| 54 |
self.task_display_name = None |
| 55 |
display_name = task.get_name().strip().split(" : ") |
| 56 |
|
| 57 |
task_display_name = display_name[-1] |
| 58 |
if task_display_name.startswith("include"): |
| 59 |
return |
| 60 |
else: |
| 61 |
self.task_display_name = task_display_name |
| 62 |
|
| 63 |
def _preprocess_result(self, result): |
| 64 |
self.delegated_vars = result._result.get("_ansible_delegated_vars", None) |
| 65 |
self._handle_exception(result._result, use_stderr=self.get_option("display_failed_stderr")) |
| 66 |
self._handle_warnings(result._result) |
| 67 |
|
| 68 |
def _process_result_output(self, result, msg): |
| 69 |
task_host = result._host.get_name() |
| 70 |
task_result = f"{task_host} {msg}" |
| 71 |
|
| 72 |
if self._run_is_verbose(result): |
| 73 |
task_result = f"{task_host} {msg}: {self._dump_results(result._result, indent=4)}" |
| 74 |
return task_result |
| 75 |
|
| 76 |
if self.delegated_vars: |
| 77 |
task_delegate_host = self.delegated_vars["ansible_host"] |
| 78 |
task_result = f"{task_host} -> {task_delegate_host} {msg}" |
| 79 |
|
| 80 |
if result._result.get("msg") and result._result.get("msg") != "All items completed": |
| 81 |
task_result += f" | msg: {to_text(result._result.get('msg'))}" |
| 82 |
|
| 83 |
if result._result.get("stdout"): |
| 84 |
task_result += f" | stdout: {result._result.get('stdout')}" |
| 85 |
|
| 86 |
if result._result.get("stderr"): |
| 87 |
task_result += f" | stderr: {result._result.get('stderr')}" |
| 88 |
|
| 89 |
return task_result |
| 90 |
|
| 91 |
def v2_playbook_on_task_start(self, task, is_conditional): |
| 92 |
self._get_task_display_name(task) |
| 93 |
if self.task_display_name is not None: |
| 94 |
if task.check_mode and self.get_option("check_mode_markers"): |
| 95 |
self._display.display(f"{self.task_display_name} (check mode)...") |
| 96 |
else: |
| 97 |
self._display.display(f"{self.task_display_name}...") |
| 98 |
|
| 99 |
def v2_playbook_on_handler_task_start(self, task): |
| 100 |
self._get_task_display_name(task) |
| 101 |
if self.task_display_name is not None: |
| 102 |
if task.check_mode and self.get_option("check_mode_markers"): |
| 103 |
self._display.display(f"{self.task_display_name} (via handler in check mode)... ") |
| 104 |
else: |
| 105 |
self._display.display(f"{self.task_display_name} (via handler)... ") |
| 106 |
|
| 107 |
def v2_playbook_on_play_start(self, play): |
| 108 |
name = play.get_name().strip() |
| 109 |
if play.check_mode and self.get_option("check_mode_markers"): |
| 110 |
if name and play.hosts: |
| 111 |
msg = f"\n- {name} (in check mode) on hosts: {','.join(play.hosts)} -" |
| 112 |
else: |
| 113 |
msg = "- check mode -" |
| 114 |
else: |
| 115 |
if name and play.hosts: |
| 116 |
msg = f"\n- {name} on hosts: {','.join(play.hosts)} -" |
| 117 |
else: |
| 118 |
msg = "---" |
| 119 |
|
| 120 |
self._display.display(msg) |
| 121 |
|
| 122 |
def v2_runner_on_skipped(self, result, ignore_errors=False): |
| 123 |
if self.get_option("display_skipped_hosts"): |
| 124 |
self._preprocess_result(result) |
| 125 |
display_color = C.COLOR_SKIP |
| 126 |
msg = "skipped" |
| 127 |
|
| 128 |
task_result = self._process_result_output(result, msg) |
| 129 |
self._display.display(f" {task_result}", display_color) |
| 130 |
else: |
| 131 |
return |
| 132 |
|
| 133 |
def v2_runner_on_failed(self, result, ignore_errors=False): |
| 134 |
self._preprocess_result(result) |
| 135 |
display_color = C.COLOR_ERROR |
| 136 |
msg = "failed" |
| 137 |
item_value = self._get_item_label(result._result) |
| 138 |
if item_value: |
| 139 |
msg += f" | item: {item_value}" |
| 140 |
|
| 141 |
task_result = self._process_result_output(result, msg) |
| 142 |
self._display.display(f" {task_result}", display_color, stderr=self.get_option("display_failed_stderr")) |
| 143 |
|
| 144 |
def v2_runner_on_ok(self, result, msg="ok", display_color=C.COLOR_OK): |
| 145 |
self._preprocess_result(result) |
| 146 |
|
| 147 |
result_was_changed = "changed" in result._result and result._result["changed"] |
| 148 |
if result_was_changed: |
| 149 |
msg = "done" |
| 150 |
item_value = self._get_item_label(result._result) |
| 151 |
if item_value: |
| 152 |
msg += f" | item: {item_value}" |
| 153 |
display_color = C.COLOR_CHANGED |
| 154 |
task_result = self._process_result_output(result, msg) |
| 155 |
self._display.display(f" {task_result}", display_color) |
| 156 |
elif self.get_option("display_ok_hosts"): |
| 157 |
task_result = self._process_result_output(result, msg) |
| 158 |
self._display.display(f" {task_result}", display_color) |
| 159 |
|
| 160 |
def v2_runner_item_on_skipped(self, result): |
| 161 |
self.v2_runner_on_skipped(result) |
| 162 |
|
| 163 |
def v2_runner_item_on_failed(self, result): |
| 164 |
self.v2_runner_on_failed(result) |
| 165 |
|
| 166 |
def v2_runner_item_on_ok(self, result): |
| 167 |
self.v2_runner_on_ok(result) |
| 168 |
|
| 169 |
def v2_runner_on_unreachable(self, result): |
| 170 |
self._preprocess_result(result) |
| 171 |
|
| 172 |
msg = "unreachable" |
| 173 |
display_color = C.COLOR_UNREACHABLE |
| 174 |
task_result = self._process_result_output(result, msg) |
| 175 |
|
| 176 |
self._display.display(f" {task_result}", display_color, stderr=self.get_option("display_failed_stderr")) |
| 177 |
|
| 178 |
def v2_on_file_diff(self, result): |
| 179 |
if result._task.loop and "results" in result._result: |
| 180 |
for res in result._result["results"]: |
| 181 |
if "diff" in res and res["diff"] and res.get("changed", False): |
| 182 |
diff = self._get_diff(res["diff"]) |
| 183 |
if diff: |
| 184 |
self._display.display(diff) |
| 185 |
elif "diff" in result._result and result._result["diff"] and result._result.get("changed", False): |
| 186 |
diff = self._get_diff(result._result["diff"]) |
| 187 |
if diff: |
| 188 |
self._display.display(diff) |
| 189 |
|
| 190 |
def v2_playbook_on_stats(self, stats): |
| 191 |
self._display.display("\n- Play recap -", screen_only=True) |
| 192 |
|
| 193 |
hosts = sorted(stats.processed.keys()) |
| 194 |
for h in hosts: |
| 195 |
# TODO how else can we display these? |
| 196 |
t = stats.summarize(h) |
| 197 |
|
| 198 |
self._display.display( |
| 199 |
f" {hostcolor(h, t)} : {colorize('ok', t['ok'], C.COLOR_OK)} {colorize('changed', t['changed'], C.COLOR_CHANGED)} " |
| 200 |
f"{colorize('unreachable', t['unreachable'], C.COLOR_UNREACHABLE)} {colorize('failed', t['failures'], C.COLOR_ERROR)} " |
| 201 |
f"{colorize('rescued', t['rescued'], C.COLOR_OK)} {colorize('ignored', t['ignored'], C.COLOR_WARN)}", |
| 202 |
screen_only=True, |
| 203 |
) |
| 204 |
|
| 205 |
self._display.display( |
| 206 |
f" {hostcolor(h, t, False)} : {colorize('ok', t['ok'], None)} {colorize('changed', t['changed'], None)} " |
| 207 |
f"{colorize('unreachable', t['unreachable'], None)} {colorize('failed', t['failures'], None)} {colorize('rescued', t['rescued'], None)} " |
| 208 |
f"{colorize('ignored', t['ignored'], None)}", |
| 209 |
log_only=True, |
| 210 |
) |
| 211 |
if stats.custom and self.get_option("show_custom_stats"): |
| 212 |
self._display.banner("CUSTOM STATS: ") |
| 213 |
# per host |
| 214 |
# TODO: come up with 'pretty format' |
| 215 |
for k in sorted(stats.custom.keys()): |
| 216 |
if k == "_run": |
| 217 |
continue |
| 218 |
stat_val = self._dump_results(stats.custom[k], indent=1).replace("\n", "") |
| 219 |
self._display.display(f"\t{k}: {stat_val}") |
| 220 |
|
| 221 |
# print per run custom stats |
| 222 |
if "_run" in stats.custom: |
| 223 |
self._display.display("", screen_only=True) |
| 224 |
stat_val_run = self._dump_results(stats.custom["_run"], indent=1).replace("\n", "") |
| 225 |
self._display.display(f"\tRUN: {stat_val_run}") |
| 226 |
self._display.display("", screen_only=True) |
| 227 |
|
| 228 |
def v2_playbook_on_no_hosts_matched(self): |
| 229 |
self._display.display(" No hosts found!", color=C.COLOR_DEBUG) |
| 230 |
|
| 231 |
def v2_playbook_on_no_hosts_remaining(self): |
| 232 |
self._display.display(" Ran out of hosts!", color=C.COLOR_ERROR) |
| 233 |
|
| 234 |
def v2_playbook_on_start(self, playbook): |
| 235 |
if context.CLIARGS["check"] and self.get_option("check_mode_markers"): |
| 236 |
self._display.display(f"Executing playbook {basename(playbook._file_name)} in check mode") |
| 237 |
else: |
| 238 |
self._display.display(f"Executing playbook {basename(playbook._file_name)}") |
| 239 |
|
| 240 |
# show CLI arguments |
| 241 |
if self._display.verbosity > 3: |
| 242 |
if context.CLIARGS.get("args"): |
| 243 |
self._display.display( |
| 244 |
f"Positional arguments: {' '.join(context.CLIARGS['args'])}", |
| 245 |
color=C.COLOR_VERBOSE, |
| 246 |
screen_only=True, |
| 247 |
) |
| 248 |
|
| 249 |
for argument in (a for a in context.CLIARGS if a != "args"): |
| 250 |
val = context.CLIARGS[argument] |
| 251 |
if val: |
| 252 |
self._display.vvvv(f"{argument}: {val}") |
| 253 |
|
| 254 |
def v2_runner_retry(self, result): |
| 255 |
msg = f" Retrying... ({result._result['attempts']} of {result._result['retries']})" |
| 256 |
if self._run_is_verbose(result): |
| 257 |
msg += f"Result was: {self._dump_results(result._result)}" |
| 258 |
self._display.display(msg, color=C.COLOR_DEBUG) |