| 1 |
# Copyright (c) 2016, Dag Wieers <[email protected]> |
| 2 |
# Copyright (c) 2017 Ansible Project |
| 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 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
name: dense |
| 10 |
type: stdout |
| 11 |
short_description: Minimal stdout output |
| 12 |
extends_documentation_fragment: |
| 13 |
- ansible.builtin.default_callback |
| 14 |
description: |
| 15 |
- When in verbose mode it acts the same as the default callback. |
| 16 |
author: |
| 17 |
- Dag Wieers (@dagwieers) |
| 18 |
requirements: |
| 19 |
- set as stdout in configuration |
| 20 |
""" |
| 21 |
|
| 22 |
HAS_OD = False |
| 23 |
try: |
| 24 |
from collections import OrderedDict |
| 25 |
|
| 26 |
HAS_OD = True |
| 27 |
except ImportError: |
| 28 |
pass |
| 29 |
|
| 30 |
import sys |
| 31 |
from collections.abc import MutableMapping, MutableSequence |
| 32 |
|
| 33 |
from ansible.plugins.callback.default import CallbackModule as CallbackModule_default |
| 34 |
from ansible.utils.color import colorize, hostcolor |
| 35 |
from ansible.utils.display import Display |
| 36 |
|
| 37 |
display = Display() |
| 38 |
|
| 39 |
|
| 40 |
# Design goals: |
| 41 |
# |
| 42 |
# + On screen there should only be relevant stuff |
| 43 |
# - How far are we ? (during run, last line) |
| 44 |
# - What issues occurred |
| 45 |
# - What changes occurred |
| 46 |
# - Diff output (in diff-mode) |
| 47 |
# |
| 48 |
# + If verbosity increases, act as default output |
| 49 |
# So that users can easily switch to default for troubleshooting |
| 50 |
# |
| 51 |
# + Rewrite the output during processing |
| 52 |
# - We use the cursor to indicate where in the task we are. |
| 53 |
# Output after the prompt is the output of the previous task. |
| 54 |
# - If we would clear the line at the start of a task, there would often |
| 55 |
# be no information at all, so we leave it until it gets updated |
| 56 |
# |
| 57 |
# + Use the same color-conventions of Ansible |
| 58 |
# |
| 59 |
# + Ensure the verbose output (-v) is also dense. |
| 60 |
# Remove information that is not essential (eg. timestamps, status) |
| 61 |
|
| 62 |
|
| 63 |
# TODO: |
| 64 |
# |
| 65 |
# + Properly test for terminal capabilities, and fall back to default |
| 66 |
# + Modify Ansible mechanism so we don't need to use sys.stdout directly |
| 67 |
# + Find an elegant solution for progress bar line wrapping |
| 68 |
|
| 69 |
|
| 70 |
# FIXME: Importing constants as C simply does not work, beats me :-/ |
| 71 |
# from ansible import constants as C |
| 72 |
class C: |
| 73 |
COLOR_HIGHLIGHT = "white" |
| 74 |
COLOR_VERBOSE = "blue" |
| 75 |
COLOR_WARN = "bright purple" |
| 76 |
COLOR_ERROR = "red" |
| 77 |
COLOR_DEBUG = "dark gray" |
| 78 |
COLOR_DEPRECATE = "purple" |
| 79 |
COLOR_SKIP = "cyan" |
| 80 |
COLOR_UNREACHABLE = "bright red" |
| 81 |
COLOR_OK = "green" |
| 82 |
COLOR_CHANGED = "yellow" |
| 83 |
|
| 84 |
|
| 85 |
# Taken from Dstat |
| 86 |
class vt100: |
| 87 |
black = "\033[0;30m" |
| 88 |
darkred = "\033[0;31m" |
| 89 |
darkgreen = "\033[0;32m" |
| 90 |
darkyellow = "\033[0;33m" |
| 91 |
darkblue = "\033[0;34m" |
| 92 |
darkmagenta = "\033[0;35m" |
| 93 |
darkcyan = "\033[0;36m" |
| 94 |
gray = "\033[0;37m" |
| 95 |
|
| 96 |
darkgray = "\033[1;30m" |
| 97 |
red = "\033[1;31m" |
| 98 |
green = "\033[1;32m" |
| 99 |
yellow = "\033[1;33m" |
| 100 |
blue = "\033[1;34m" |
| 101 |
magenta = "\033[1;35m" |
| 102 |
cyan = "\033[1;36m" |
| 103 |
white = "\033[1;37m" |
| 104 |
|
| 105 |
blackbg = "\033[40m" |
| 106 |
redbg = "\033[41m" |
| 107 |
greenbg = "\033[42m" |
| 108 |
yellowbg = "\033[43m" |
| 109 |
bluebg = "\033[44m" |
| 110 |
magentabg = "\033[45m" |
| 111 |
cyanbg = "\033[46m" |
| 112 |
whitebg = "\033[47m" |
| 113 |
|
| 114 |
reset = "\033[0;0m" |
| 115 |
bold = "\033[1m" |
| 116 |
reverse = "\033[2m" |
| 117 |
underline = "\033[4m" |
| 118 |
|
| 119 |
clear = "\033[2J" |
| 120 |
# clearline = '\033[K' |
| 121 |
clearline = "\033[2K" |
| 122 |
save = "\033[s" |
| 123 |
restore = "\033[u" |
| 124 |
save_all = "\0337" |
| 125 |
restore_all = "\0338" |
| 126 |
linewrap = "\033[7h" |
| 127 |
nolinewrap = "\033[7l" |
| 128 |
|
| 129 |
up = "\033[1A" |
| 130 |
down = "\033[1B" |
| 131 |
right = "\033[1C" |
| 132 |
left = "\033[1D" |
| 133 |
|
| 134 |
|
| 135 |
colors = dict( |
| 136 |
ok=vt100.darkgreen, |
| 137 |
changed=vt100.darkyellow, |
| 138 |
skipped=vt100.darkcyan, |
| 139 |
ignored=vt100.cyanbg + vt100.red, |
| 140 |
failed=vt100.darkred, |
| 141 |
unreachable=vt100.red, |
| 142 |
) |
| 143 |
|
| 144 |
states = ("skipped", "ok", "changed", "failed", "unreachable") |
| 145 |
|
| 146 |
|
| 147 |
class CallbackModule(CallbackModule_default): |
| 148 |
""" |
| 149 |
This is the dense callback interface, where screen estate is still valued. |
| 150 |
""" |
| 151 |
|
| 152 |
CALLBACK_VERSION = 2.0 |
| 153 |
CALLBACK_TYPE = "stdout" |
| 154 |
CALLBACK_NAME = "dense" |
| 155 |
|
| 156 |
def __init__(self): |
| 157 |
# From CallbackModule |
| 158 |
self._display = display |
| 159 |
|
| 160 |
if HAS_OD: |
| 161 |
self.disabled = False |
| 162 |
self.super_ref = super() |
| 163 |
self.super_ref.__init__() |
| 164 |
|
| 165 |
# Attributes to remove from results for more density |
| 166 |
self.removed_attributes = ( |
| 167 |
# 'changed', |
| 168 |
"delta", |
| 169 |
# 'diff', |
| 170 |
"end", |
| 171 |
"failed", |
| 172 |
"failed_when_result", |
| 173 |
"invocation", |
| 174 |
"start", |
| 175 |
"stdout_lines", |
| 176 |
) |
| 177 |
|
| 178 |
# Initiate data structures |
| 179 |
self.hosts = OrderedDict() |
| 180 |
self.keep = False |
| 181 |
self.shown_title = False |
| 182 |
self.count = dict(play=0, handler=0, task=0) |
| 183 |
self.type = "foo" |
| 184 |
|
| 185 |
# Start immediately on the first line |
| 186 |
sys.stdout.write(vt100.reset + vt100.save + vt100.clearline) |
| 187 |
sys.stdout.flush() |
| 188 |
else: |
| 189 |
display.warning( |
| 190 |
"The 'dense' callback plugin requires OrderedDict which is not available in this version of python, disabling." |
| 191 |
) |
| 192 |
self.disabled = True |
| 193 |
|
| 194 |
def __del__(self): |
| 195 |
sys.stdout.write(f"{vt100.restore}{vt100.reset}\n{vt100.save}{vt100.clearline}") |
| 196 |
|
| 197 |
def _add_host(self, result, status): |
| 198 |
name = result._host.get_name() |
| 199 |
|
| 200 |
# Add a new status in case a failed task is ignored |
| 201 |
if status == "failed" and result._task.ignore_errors: |
| 202 |
status = "ignored" |
| 203 |
|
| 204 |
# Check if we have to update an existing state (when looping over items) |
| 205 |
if name not in self.hosts: |
| 206 |
self.hosts[name] = dict(state=status) |
| 207 |
elif states.index(self.hosts[name]["state"]) < states.index(status): |
| 208 |
self.hosts[name]["state"] = status |
| 209 |
|
| 210 |
# Store delegated hostname, if needed |
| 211 |
delegated_vars = result._result.get("_ansible_delegated_vars", None) |
| 212 |
if delegated_vars: |
| 213 |
self.hosts[name]["delegate"] = delegated_vars["ansible_host"] |
| 214 |
|
| 215 |
# Print progress bar |
| 216 |
self._display_progress(result) |
| 217 |
|
| 218 |
# # Ensure that tasks with changes/failures stay on-screen, and during diff-mode |
| 219 |
# if status in ['changed', 'failed', 'unreachable'] or (result.get('_diff_mode', False) and result._resultget('diff', False)): |
| 220 |
# Ensure that tasks with changes/failures stay on-screen |
| 221 |
if status in ["changed", "failed", "unreachable"]: |
| 222 |
self.keep = True |
| 223 |
|
| 224 |
if self._display.verbosity == 1: |
| 225 |
# Print task title, if needed |
| 226 |
self._display_task_banner() |
| 227 |
self._display_results(result, status) |
| 228 |
|
| 229 |
def _clean_results(self, result): |
| 230 |
# Remove non-essential attributes |
| 231 |
for attr in self.removed_attributes: |
| 232 |
if attr in result: |
| 233 |
del result[attr] |
| 234 |
|
| 235 |
# Remove empty attributes (list, dict, str) |
| 236 |
for attr in result.copy(): |
| 237 |
if isinstance(result[attr], (MutableSequence, MutableMapping, bytes, str)): |
| 238 |
if not result[attr]: |
| 239 |
del result[attr] |
| 240 |
|
| 241 |
def _handle_exceptions(self, result): |
| 242 |
if "exception" in result: |
| 243 |
# Remove the exception from the result so it is not shown every time |
| 244 |
del result["exception"] |
| 245 |
|
| 246 |
if self._display.verbosity == 1: |
| 247 |
return "An exception occurred during task execution. To see the full traceback, use -vvv." |
| 248 |
|
| 249 |
def _display_progress(self, result=None): |
| 250 |
# Always rewrite the complete line |
| 251 |
sys.stdout.write(vt100.restore + vt100.reset + vt100.clearline + vt100.nolinewrap + vt100.underline) |
| 252 |
sys.stdout.write(f"{self.type} {self.count[self.type]}:") |
| 253 |
sys.stdout.write(vt100.reset) |
| 254 |
sys.stdout.flush() |
| 255 |
|
| 256 |
# Print out each host in its own status-color |
| 257 |
for name in self.hosts: |
| 258 |
sys.stdout.write(" ") |
| 259 |
if self.hosts[name].get("delegate", None): |
| 260 |
sys.stdout.write(f"{self.hosts[name]['delegate']}>") |
| 261 |
sys.stdout.write(colors[self.hosts[name]["state"]] + name + vt100.reset) |
| 262 |
sys.stdout.flush() |
| 263 |
|
| 264 |
sys.stdout.write(vt100.linewrap) |
| 265 |
|
| 266 |
def _display_task_banner(self): |
| 267 |
if not self.shown_title: |
| 268 |
self.shown_title = True |
| 269 |
sys.stdout.write(vt100.restore + vt100.reset + vt100.clearline + vt100.underline) |
| 270 |
sys.stdout.write(f"{self.type} {self.count[self.type]}: {self.task.get_name().strip()}") |
| 271 |
sys.stdout.write(f"{vt100.restore}{vt100.reset}\n{vt100.save}{vt100.clearline}") |
| 272 |
sys.stdout.flush() |
| 273 |
else: |
| 274 |
sys.stdout.write(vt100.restore + vt100.reset + vt100.clearline) |
| 275 |
self.keep = False |
| 276 |
|
| 277 |
def _display_results(self, result, status): |
| 278 |
# Leave the previous task on screen (as it has changes/errors) |
| 279 |
if self._display.verbosity == 0 and self.keep: |
| 280 |
sys.stdout.write(f"{vt100.restore}{vt100.reset}\n{vt100.save}{vt100.clearline}") |
| 281 |
else: |
| 282 |
sys.stdout.write(vt100.restore + vt100.reset + vt100.clearline) |
| 283 |
self.keep = False |
| 284 |
|
| 285 |
self._clean_results(result._result) |
| 286 |
|
| 287 |
dump = "" |
| 288 |
if result._task.action == "include": |
| 289 |
return |
| 290 |
elif status == "ok": |
| 291 |
return |
| 292 |
elif status == "ignored": |
| 293 |
dump = self._handle_exceptions(result._result) |
| 294 |
elif status == "failed": |
| 295 |
dump = self._handle_exceptions(result._result) |
| 296 |
elif status == "unreachable": |
| 297 |
dump = result._result["msg"] |
| 298 |
|
| 299 |
if not dump: |
| 300 |
dump = self._dump_results(result._result) |
| 301 |
|
| 302 |
if result._task.loop and "results" in result._result: |
| 303 |
self._process_items(result) |
| 304 |
else: |
| 305 |
sys.stdout.write(f"{colors[status] + status}: ") |
| 306 |
|
| 307 |
delegated_vars = result._result.get("_ansible_delegated_vars", None) |
| 308 |
if delegated_vars: |
| 309 |
sys.stdout.write( |
| 310 |
f"{vt100.reset}{result._host.get_name()}>{colors[status]}{delegated_vars['ansible_host']}" |
| 311 |
) |
| 312 |
else: |
| 313 |
sys.stdout.write(result._host.get_name()) |
| 314 |
|
| 315 |
sys.stdout.write(f": {dump}\n") |
| 316 |
sys.stdout.write(f"{vt100.reset}{vt100.save}{vt100.clearline}") |
| 317 |
sys.stdout.flush() |
| 318 |
|
| 319 |
if status == "changed": |
| 320 |
self._handle_warnings(result._result) |
| 321 |
|
| 322 |
def v2_playbook_on_play_start(self, play): |
| 323 |
# Leave the previous task on screen (as it has changes/errors) |
| 324 |
if self._display.verbosity == 0 and self.keep: |
| 325 |
sys.stdout.write(f"{vt100.restore}{vt100.reset}\n{vt100.save}{vt100.clearline}{vt100.bold}") |
| 326 |
else: |
| 327 |
sys.stdout.write(vt100.restore + vt100.reset + vt100.clearline + vt100.bold) |
| 328 |
|
| 329 |
# Reset at the start of each play |
| 330 |
self.keep = False |
| 331 |
self.count.update(dict(handler=0, task=0)) |
| 332 |
self.count["play"] += 1 |
| 333 |
self.play = play |
| 334 |
|
| 335 |
# Write the next play on screen IN UPPERCASE, and make it permanent |
| 336 |
name = play.get_name().strip() |
| 337 |
if not name: |
| 338 |
name = "unnamed" |
| 339 |
sys.stdout.write(f"PLAY {self.count['play']}: {name.upper()}") |
| 340 |
sys.stdout.write(f"{vt100.restore}{vt100.reset}\n{vt100.save}{vt100.clearline}") |
| 341 |
sys.stdout.flush() |
| 342 |
|
| 343 |
def v2_playbook_on_task_start(self, task, is_conditional): |
| 344 |
# Leave the previous task on screen (as it has changes/errors) |
| 345 |
if self._display.verbosity == 0 and self.keep: |
| 346 |
sys.stdout.write(f"{vt100.restore}{vt100.reset}\n{vt100.save}{vt100.clearline}{vt100.underline}") |
| 347 |
else: |
| 348 |
# Do not clear line, since we want to retain the previous output |
| 349 |
sys.stdout.write(vt100.restore + vt100.reset + vt100.underline) |
| 350 |
|
| 351 |
# Reset at the start of each task |
| 352 |
self.keep = False |
| 353 |
self.shown_title = False |
| 354 |
self.hosts = OrderedDict() |
| 355 |
self.task = task |
| 356 |
self.type = "task" |
| 357 |
|
| 358 |
# Enumerate task if not setup (task names are too long for dense output) |
| 359 |
if task.get_name() != "setup": |
| 360 |
self.count["task"] += 1 |
| 361 |
|
| 362 |
# Write the next task on screen (behind the prompt is the previous output) |
| 363 |
sys.stdout.write(f"{self.type} {self.count[self.type]}.") |
| 364 |
sys.stdout.write(vt100.reset) |
| 365 |
sys.stdout.flush() |
| 366 |
|
| 367 |
def v2_playbook_on_handler_task_start(self, task): |
| 368 |
# Leave the previous task on screen (as it has changes/errors) |
| 369 |
if self._display.verbosity == 0 and self.keep: |
| 370 |
sys.stdout.write(f"{vt100.restore}{vt100.reset}\n{vt100.save}{vt100.clearline}{vt100.underline}") |
| 371 |
else: |
| 372 |
sys.stdout.write(vt100.restore + vt100.reset + vt100.clearline + vt100.underline) |
| 373 |
|
| 374 |
# Reset at the start of each handler |
| 375 |
self.keep = False |
| 376 |
self.shown_title = False |
| 377 |
self.hosts = OrderedDict() |
| 378 |
self.task = task |
| 379 |
self.type = "handler" |
| 380 |
|
| 381 |
# Enumerate handler if not setup (handler names may be too long for dense output) |
| 382 |
if task.get_name() != "setup": |
| 383 |
self.count[self.type] += 1 |
| 384 |
|
| 385 |
# Write the next task on screen (behind the prompt is the previous output) |
| 386 |
sys.stdout.write(f"{self.type} {self.count[self.type]}.") |
| 387 |
sys.stdout.write(vt100.reset) |
| 388 |
sys.stdout.flush() |
| 389 |
|
| 390 |
def v2_playbook_on_cleanup_task_start(self, task): |
| 391 |
# TBD |
| 392 |
sys.stdout.write("cleanup.") |
| 393 |
sys.stdout.flush() |
| 394 |
|
| 395 |
def v2_runner_on_failed(self, result, ignore_errors=False): |
| 396 |
self._add_host(result, "failed") |
| 397 |
|
| 398 |
def v2_runner_on_ok(self, result): |
| 399 |
if result._result.get("changed", False): |
| 400 |
self._add_host(result, "changed") |
| 401 |
else: |
| 402 |
self._add_host(result, "ok") |
| 403 |
|
| 404 |
def v2_runner_on_skipped(self, result): |
| 405 |
self._add_host(result, "skipped") |
| 406 |
|
| 407 |
def v2_runner_on_unreachable(self, result): |
| 408 |
self._add_host(result, "unreachable") |
| 409 |
|
| 410 |
def v2_runner_on_include(self, included_file): |
| 411 |
pass |
| 412 |
|
| 413 |
def v2_runner_on_file_diff(self, result, diff): |
| 414 |
sys.stdout.write(vt100.bold) |
| 415 |
self.super_ref.v2_runner_on_file_diff(result, diff) |
| 416 |
sys.stdout.write(vt100.reset) |
| 417 |
|
| 418 |
def v2_on_file_diff(self, result): |
| 419 |
sys.stdout.write(vt100.bold) |
| 420 |
self.super_ref.v2_on_file_diff(result) |
| 421 |
sys.stdout.write(vt100.reset) |
| 422 |
|
| 423 |
# Old definition in v2.0 |
| 424 |
def v2_playbook_item_on_ok(self, result): |
| 425 |
self.v2_runner_item_on_ok(result) |
| 426 |
|
| 427 |
def v2_runner_item_on_ok(self, result): |
| 428 |
if result._result.get("changed", False): |
| 429 |
self._add_host(result, "changed") |
| 430 |
else: |
| 431 |
self._add_host(result, "ok") |
| 432 |
|
| 433 |
# Old definition in v2.0 |
| 434 |
def v2_playbook_item_on_failed(self, result): |
| 435 |
self.v2_runner_item_on_failed(result) |
| 436 |
|
| 437 |
def v2_runner_item_on_failed(self, result): |
| 438 |
self._add_host(result, "failed") |
| 439 |
|
| 440 |
# Old definition in v2.0 |
| 441 |
def v2_playbook_item_on_skipped(self, result): |
| 442 |
self.v2_runner_item_on_skipped(result) |
| 443 |
|
| 444 |
def v2_runner_item_on_skipped(self, result): |
| 445 |
self._add_host(result, "skipped") |
| 446 |
|
| 447 |
def v2_playbook_on_no_hosts_remaining(self): |
| 448 |
if self._display.verbosity == 0 and self.keep: |
| 449 |
sys.stdout.write(f"{vt100.restore}{vt100.reset}\n{vt100.save}{vt100.clearline}") |
| 450 |
else: |
| 451 |
sys.stdout.write(vt100.restore + vt100.reset + vt100.clearline) |
| 452 |
self.keep = False |
| 453 |
|
| 454 |
sys.stdout.write(f"{vt100.white + vt100.redbg}NO MORE HOSTS LEFT") |
| 455 |
sys.stdout.write(f"{vt100.restore}{vt100.reset}\n{vt100.save}{vt100.clearline}") |
| 456 |
sys.stdout.flush() |
| 457 |
|
| 458 |
def v2_playbook_on_include(self, included_file): |
| 459 |
pass |
| 460 |
|
| 461 |
def v2_playbook_on_stats(self, stats): |
| 462 |
if self._display.verbosity == 0 and self.keep: |
| 463 |
sys.stdout.write(f"{vt100.restore}{vt100.reset}\n{vt100.save}{vt100.clearline}") |
| 464 |
else: |
| 465 |
sys.stdout.write(vt100.restore + vt100.reset + vt100.clearline) |
| 466 |
|
| 467 |
# In normal mode screen output should be sufficient, summary is redundant |
| 468 |
if self._display.verbosity == 0: |
| 469 |
return |
| 470 |
|
| 471 |
sys.stdout.write(vt100.bold + vt100.underline) |
| 472 |
sys.stdout.write("SUMMARY") |
| 473 |
|
| 474 |
sys.stdout.write(f"{vt100.restore}{vt100.reset}\n{vt100.save}{vt100.clearline}") |
| 475 |
sys.stdout.flush() |
| 476 |
|
| 477 |
hosts = sorted(stats.processed.keys()) |
| 478 |
for h in hosts: |
| 479 |
t = stats.summarize(h) |
| 480 |
self._display.display( |
| 481 |
f"{hostcolor(h, t)} : {colorize('ok', t['ok'], C.COLOR_OK)} {colorize('changed', t['changed'], C.COLOR_CHANGED)} " |
| 482 |
f"{colorize('unreachable', t['unreachable'], C.COLOR_UNREACHABLE)} {colorize('failed', t['failures'], C.COLOR_ERROR)} " |
| 483 |
f"{colorize('rescued', t['rescued'], C.COLOR_OK)} {colorize('ignored', t['ignored'], C.COLOR_WARN)}", |
| 484 |
screen_only=True, |
| 485 |
) |
| 486 |
|
| 487 |
|
| 488 |
# When using -vv or higher, simply do the default action |
| 489 |
if display.verbosity >= 2 or not HAS_OD: |
| 490 |
CallbackModule = CallbackModule_default # type: ignore |