| 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: default |
| 9 |
type: stdout |
| 10 |
short_description: default Ansible screen output |
| 11 |
version_added: historical |
| 12 |
description: |
| 13 |
- This is the default output callback for ansible-playbook. |
| 14 |
extends_documentation_fragment: |
| 15 |
- default_callback |
| 16 |
- result_format_callback |
| 17 |
requirements: |
| 18 |
- set as stdout in configuration |
| 19 |
""" |
| 20 |
|
| 21 |
import typing as _t |
| 22 |
|
| 23 |
from ansible import constants as C |
| 24 |
from ansible import context |
| 25 |
from ansible.executor.task_result import CallbackTaskResult |
| 26 |
from ansible.playbook.task_include import TaskInclude |
| 27 |
from ansible.plugins.callback import CallbackBase |
| 28 |
from ansible.utils.color import colorize, hostcolor |
| 29 |
from ansible.utils.fqcn import add_internal_fqcns |
| 30 |
|
| 31 |
if _t.TYPE_CHECKING: |
| 32 |
from ansible.playbook.included_file import IncludedFile |
| 33 |
|
| 34 |
|
| 35 |
class CallbackModule(CallbackBase): |
| 36 |
|
| 37 |
""" |
| 38 |
This is the default callback interface, which simply prints messages |
| 39 |
to stdout when new callback events are received. |
| 40 |
""" |
| 41 |
|
| 42 |
CALLBACK_VERSION = 2.0 |
| 43 |
CALLBACK_TYPE = 'stdout' |
| 44 |
CALLBACK_NAME = 'default' |
| 45 |
|
| 46 |
ANSIBLE_SUPPORTS_MASKING = True |
| 47 |
|
| 48 |
def __init__(self): |
| 49 |
|
| 50 |
self._play = None |
| 51 |
self._last_task_banner = None |
| 52 |
self._last_task_name = None |
| 53 |
self._task_type_cache = {} |
| 54 |
super(CallbackModule, self).__init__() |
| 55 |
|
| 56 |
def v2_runner_on_failed(self, result: CallbackTaskResult, ignore_errors: bool = False) -> None: |
| 57 |
host_label = self.host_label(result) |
| 58 |
|
| 59 |
if self._last_task_banner != result.task._uuid: |
| 60 |
self._print_task_banner(result.task) |
| 61 |
|
| 62 |
self._handle_warnings_and_exception(result) |
| 63 |
|
| 64 |
# FIXME: this method should not exist, delegate "suggested keys to display" to the plugin or something... As-is, the placement of this |
| 65 |
# call obliterates `results`, which causes a task summary to be printed on loop failures, which we don't do anywhere else. |
| 66 |
self._clean_results(result.result, result.task.action) |
| 67 |
|
| 68 |
if result.task.loop and 'results' in result.result: |
| 69 |
self._process_items(result) |
| 70 |
else: |
| 71 |
if self._display.verbosity < 2 and self.get_option('show_task_path_on_failure'): |
| 72 |
self._print_task_path(result.task) |
| 73 |
msg = "fatal: [%s]: FAILED! => %s" % (host_label, self._dump_results(result.result)) |
| 74 |
self._display.display(msg, color=C.COLOR_ERROR, stderr=self.get_option('display_failed_stderr')) |
| 75 |
|
| 76 |
if ignore_errors: |
| 77 |
self._display.display("...ignoring", color=C.COLOR_SKIP) |
| 78 |
|
| 79 |
def v2_runner_on_ok(self, result: CallbackTaskResult) -> None: |
| 80 |
host_label = self.host_label(result) |
| 81 |
|
| 82 |
if isinstance(result.task, TaskInclude): |
| 83 |
if self._last_task_banner != result.task._uuid: |
| 84 |
self._print_task_banner(result.task) |
| 85 |
return |
| 86 |
elif result.result.get('changed', False): |
| 87 |
if self._last_task_banner != result.task._uuid: |
| 88 |
self._print_task_banner(result.task) |
| 89 |
|
| 90 |
msg = "changed: [%s]" % (host_label,) |
| 91 |
color = C.COLOR_CHANGED |
| 92 |
else: |
| 93 |
if not self.get_option('display_ok_hosts'): |
| 94 |
return |
| 95 |
|
| 96 |
if self._last_task_banner != result.task._uuid: |
| 97 |
self._print_task_banner(result.task) |
| 98 |
|
| 99 |
msg = "ok: [%s]" % (host_label,) |
| 100 |
color = C.COLOR_OK |
| 101 |
|
| 102 |
self._handle_warnings_and_exception(result) |
| 103 |
|
| 104 |
if result.task.loop and 'results' in result.result: |
| 105 |
self._process_items(result) |
| 106 |
else: |
| 107 |
self._clean_results(result.result, result.task.action) |
| 108 |
|
| 109 |
if self._run_is_verbose(result): |
| 110 |
msg += " => %s" % (self._dump_results(result.result),) |
| 111 |
self._display.display(msg, color=color) |
| 112 |
|
| 113 |
def v2_runner_on_skipped(self, result: CallbackTaskResult) -> None: |
| 114 |
if self.get_option('display_skipped_hosts'): |
| 115 |
|
| 116 |
self._clean_results(result.result, result.task.action) |
| 117 |
|
| 118 |
if self._last_task_banner != result.task._uuid: |
| 119 |
self._print_task_banner(result.task) |
| 120 |
|
| 121 |
self._handle_warnings_and_exception(result) |
| 122 |
|
| 123 |
if result.task.loop is not None and 'results' in result.result: |
| 124 |
self._process_items(result) |
| 125 |
|
| 126 |
msg = "skipping: [%s]" % result.host.get_name() |
| 127 |
if self._run_is_verbose(result): |
| 128 |
msg += " => %s" % self._dump_results(result.result) |
| 129 |
self._display.display(msg, color=C.COLOR_SKIP) |
| 130 |
|
| 131 |
def v2_runner_on_unreachable(self, result: CallbackTaskResult) -> None: |
| 132 |
if self._last_task_banner != result.task._uuid: |
| 133 |
self._print_task_banner(result.task) |
| 134 |
|
| 135 |
self._handle_warnings_and_exception(result) |
| 136 |
|
| 137 |
host_label = self.host_label(result) |
| 138 |
msg = "fatal: [%s]: UNREACHABLE! => %s" % (host_label, self._dump_results(result.result)) |
| 139 |
self._display.display(msg, color=C.COLOR_UNREACHABLE, stderr=self.get_option('display_failed_stderr')) |
| 140 |
|
| 141 |
if result.task.ignore_unreachable: |
| 142 |
self._display.display("...ignoring", color=C.COLOR_SKIP) |
| 143 |
|
| 144 |
def v2_playbook_on_no_hosts_matched(self): |
| 145 |
self._display.display("skipping: no hosts matched", color=C.COLOR_SKIP) |
| 146 |
|
| 147 |
def v2_playbook_on_no_hosts_remaining(self): |
| 148 |
self._display.banner("NO MORE HOSTS LEFT") |
| 149 |
|
| 150 |
def v2_playbook_on_task_start(self, task, is_conditional): |
| 151 |
self._task_start(task, prefix='TASK') |
| 152 |
|
| 153 |
def _task_start(self, task, prefix=None): |
| 154 |
# Cache output prefix for task if provided |
| 155 |
# This is needed to properly display 'RUNNING HANDLER' and similar |
| 156 |
# when hiding skipped/ok task results |
| 157 |
if prefix is not None: |
| 158 |
self._task_type_cache[task._uuid] = prefix |
| 159 |
|
| 160 |
# Preserve task name, as all vars may not be available for templating |
| 161 |
# when we need it later |
| 162 |
if self._play.strategy in add_internal_fqcns(('free', 'host_pinned')): |
| 163 |
# Explicitly set to None for strategy free/host_pinned to account for any cached |
| 164 |
# task title from a previous non-free play |
| 165 |
self._last_task_name = None |
| 166 |
else: |
| 167 |
self._last_task_name = task.get_name().strip() |
| 168 |
|
| 169 |
# Display the task banner immediately if we're not doing any filtering based on task result |
| 170 |
if self.get_option('display_skipped_hosts') and self.get_option('display_ok_hosts'): |
| 171 |
self._print_task_banner(task) |
| 172 |
|
| 173 |
def _print_task_banner(self, task): |
| 174 |
# args can be specified as no_log in several places: in the task or in |
| 175 |
# the argument spec. We can check whether the task is no_log but the |
| 176 |
# argument spec can't be because that is only run on the target |
| 177 |
# machine and we haven't run it there yet at this time. |
| 178 |
# |
| 179 |
# So we give people a config option to affect display of the args so |
| 180 |
# that they can secure this if they feel that their stdout is insecure |
| 181 |
# (shoulder surfing, logging stdout straight to a file, etc). |
| 182 |
args = '' |
| 183 |
# FIXME: the no_log value is not templated at this point, so any template will be considered truthy |
| 184 |
if not task.no_log and C.DISPLAY_ARGS_TO_STDOUT: |
| 185 |
args = u', '.join(u'%s=%s' % a for a in task.args.items()) |
| 186 |
args = u' %s' % args |
| 187 |
|
| 188 |
prefix = self._task_type_cache.get(task._uuid, 'TASK') |
| 189 |
|
| 190 |
# Use cached task name |
| 191 |
task_name = self._last_task_name |
| 192 |
if task_name is None: |
| 193 |
task_name = task.get_name().strip() |
| 194 |
|
| 195 |
if task.check_mode and self.get_option('check_mode_markers'): |
| 196 |
checkmsg = " [CHECK MODE]" |
| 197 |
else: |
| 198 |
checkmsg = "" |
| 199 |
self._display.banner(u"%s [%s%s]%s" % (prefix, task_name, args, checkmsg)) |
| 200 |
|
| 201 |
if self._display.verbosity >= 2: |
| 202 |
self._print_task_path(task) |
| 203 |
|
| 204 |
self._last_task_banner = task._uuid |
| 205 |
|
| 206 |
def v2_playbook_on_handler_task_start(self, task): |
| 207 |
self._task_start(task, prefix='RUNNING HANDLER') |
| 208 |
|
| 209 |
def v2_runner_on_start(self, host, task): |
| 210 |
if self.get_option('show_per_host_start'): |
| 211 |
self._display.display(" [started %s on %s]" % (task, host), color=C.COLOR_OK) |
| 212 |
|
| 213 |
def v2_playbook_on_play_start(self, play): |
| 214 |
name = play.get_name().strip() |
| 215 |
if play.check_mode and self.get_option('check_mode_markers'): |
| 216 |
checkmsg = " [CHECK MODE]" |
| 217 |
else: |
| 218 |
checkmsg = "" |
| 219 |
if not name: |
| 220 |
msg = u"PLAY%s" % checkmsg |
| 221 |
else: |
| 222 |
msg = u"PLAY [%s]%s" % (name, checkmsg) |
| 223 |
|
| 224 |
self._play = play |
| 225 |
|
| 226 |
self._display.banner(msg) |
| 227 |
|
| 228 |
def v2_on_file_diff(self, result: CallbackTaskResult) -> None: |
| 229 |
if result.task.loop and 'results' in result.result: |
| 230 |
for res in result.result['results']: |
| 231 |
if 'diff' in res and res['diff'] and res.get('changed', False): |
| 232 |
diff = self._get_diff(res['diff']) |
| 233 |
if diff: |
| 234 |
if self._last_task_banner != result.task._uuid: |
| 235 |
self._print_task_banner(result.task) |
| 236 |
self._display.display(diff) |
| 237 |
elif 'diff' in result.result and result.result['diff'] and result.result.get('changed', False): |
| 238 |
diff = self._get_diff(result.result['diff']) |
| 239 |
if diff: |
| 240 |
if self._last_task_banner != result.task._uuid: |
| 241 |
self._print_task_banner(result.task) |
| 242 |
self._display.display(diff) |
| 243 |
|
| 244 |
def v2_runner_item_on_ok(self, result: CallbackTaskResult) -> None: |
| 245 |
host_label = self.host_label(result) |
| 246 |
if isinstance(result.task, TaskInclude): |
| 247 |
return |
| 248 |
elif result.result.get('changed', False): |
| 249 |
if self._last_task_banner != result.task._uuid: |
| 250 |
self._print_task_banner(result.task) |
| 251 |
|
| 252 |
msg = 'changed' |
| 253 |
color = C.COLOR_CHANGED |
| 254 |
else: |
| 255 |
if not self.get_option('display_ok_hosts'): |
| 256 |
return |
| 257 |
|
| 258 |
if self._last_task_banner != result.task._uuid: |
| 259 |
self._print_task_banner(result.task) |
| 260 |
|
| 261 |
msg = 'ok' |
| 262 |
color = C.COLOR_OK |
| 263 |
|
| 264 |
self._handle_warnings_and_exception(result) |
| 265 |
|
| 266 |
msg = "%s: [%s] => (item=%s)" % (msg, host_label, self._get_item_label(result.result)) |
| 267 |
self._clean_results(result.result, result.task.action) |
| 268 |
if self._run_is_verbose(result): |
| 269 |
msg += " => %s" % self._dump_results(result.result) |
| 270 |
self._display.display(msg, color=color) |
| 271 |
|
| 272 |
def v2_runner_item_on_failed(self, result: CallbackTaskResult) -> None: |
| 273 |
if self._last_task_banner != result.task._uuid: |
| 274 |
self._print_task_banner(result.task) |
| 275 |
|
| 276 |
self._handle_warnings_and_exception(result) |
| 277 |
|
| 278 |
host_label = self.host_label(result) |
| 279 |
|
| 280 |
msg = "failed: [%s]" % (host_label,) |
| 281 |
self._clean_results(result.result, result.task.action) |
| 282 |
self._display.display( |
| 283 |
msg + " (item=%s) => %s" % (self._get_item_label(result.result), self._dump_results(result.result)), |
| 284 |
color=C.COLOR_ERROR, |
| 285 |
stderr=self.get_option('display_failed_stderr') |
| 286 |
) |
| 287 |
|
| 288 |
def v2_runner_item_on_skipped(self, result: CallbackTaskResult) -> None: |
| 289 |
if self.get_option('display_skipped_hosts'): |
| 290 |
if self._last_task_banner != result.task._uuid: |
| 291 |
self._print_task_banner(result.task) |
| 292 |
|
| 293 |
self._handle_warnings_and_exception(result) |
| 294 |
|
| 295 |
self._clean_results(result.result, result.task.action) |
| 296 |
msg = "skipping: [%s] => (item=%s) " % (result.host.get_name(), self._get_item_label(result.result)) |
| 297 |
if self._run_is_verbose(result): |
| 298 |
msg += " => %s" % self._dump_results(result.result) |
| 299 |
self._display.display(msg, color=C.COLOR_SKIP) |
| 300 |
|
| 301 |
def v2_playbook_on_include(self, included_file: IncludedFile) -> None: |
| 302 |
if not self.get_option("display_included_hosts"): |
| 303 |
return |
| 304 |
|
| 305 |
msg = 'included: %s for %s' % (included_file._filename, ", ".join([h.name for h in included_file._hosts])) |
| 306 |
label = self._get_item_label(included_file._vars) |
| 307 |
if label: |
| 308 |
msg += " => (item=%s)" % label |
| 309 |
self._display.display(msg, color=C.COLOR_INCLUDED) |
| 310 |
|
| 311 |
def v2_playbook_on_stats(self, stats): |
| 312 |
self._display.banner("PLAY RECAP") |
| 313 |
|
| 314 |
hosts = sorted(stats.processed.keys()) |
| 315 |
for h in hosts: |
| 316 |
t = stats.summarize(h) |
| 317 |
|
| 318 |
self._display.display( |
| 319 |
u"%s : %s %s %s %s %s %s %s" % ( |
| 320 |
hostcolor(h, t), |
| 321 |
colorize(u'ok', t['ok'], C.COLOR_OK), |
| 322 |
colorize(u'changed', t['changed'], C.COLOR_CHANGED), |
| 323 |
colorize(u'unreachable', t['unreachable'], C.COLOR_UNREACHABLE), |
| 324 |
colorize(u'failed', t['failures'], C.COLOR_ERROR), |
| 325 |
colorize(u'skipped', t['skipped'], C.COLOR_SKIP), |
| 326 |
colorize(u'rescued', t['rescued'], C.COLOR_OK), |
| 327 |
colorize(u'ignored', t['ignored'], C.COLOR_WARN), |
| 328 |
), |
| 329 |
screen_only=True |
| 330 |
) |
| 331 |
|
| 332 |
self._display.display( |
| 333 |
u"%s : %s %s %s %s %s %s %s" % ( |
| 334 |
hostcolor(h, t, False), |
| 335 |
colorize(u'ok', t['ok'], None), |
| 336 |
colorize(u'changed', t['changed'], None), |
| 337 |
colorize(u'unreachable', t['unreachable'], None), |
| 338 |
colorize(u'failed', t['failures'], None), |
| 339 |
colorize(u'skipped', t['skipped'], None), |
| 340 |
colorize(u'rescued', t['rescued'], None), |
| 341 |
colorize(u'ignored', t['ignored'], None), |
| 342 |
), |
| 343 |
log_only=True |
| 344 |
) |
| 345 |
|
| 346 |
self._display.display("", screen_only=True) |
| 347 |
|
| 348 |
# print custom stats if required |
| 349 |
if stats.custom and self.get_option('show_custom_stats'): |
| 350 |
self._display.banner("CUSTOM STATS: ") |
| 351 |
# per host |
| 352 |
# TODO: come up with 'pretty format' |
| 353 |
for k in sorted(stats.custom.keys()): |
| 354 |
if k == '_run': |
| 355 |
continue |
| 356 |
self._display.display('\t%s: %s' % (k, self._dump_results(stats.custom[k], indent=1).replace('\n', ''))) |
| 357 |
|
| 358 |
# print per run custom stats |
| 359 |
if '_run' in stats.custom: |
| 360 |
self._display.display("", screen_only=True) |
| 361 |
self._display.display('\tRUN: %s' % self._dump_results(stats.custom['_run'], indent=1).replace('\n', '')) |
| 362 |
self._display.display("", screen_only=True) |
| 363 |
|
| 364 |
if context.CLIARGS['check'] and self.get_option('check_mode_markers'): |
| 365 |
self._display.banner("DRY RUN") |
| 366 |
|
| 367 |
def v2_playbook_on_start(self, playbook): |
| 368 |
if self._display.verbosity > 1: |
| 369 |
from os.path import basename |
| 370 |
self._display.banner("PLAYBOOK: %s" % basename(playbook._file_name)) |
| 371 |
|
| 372 |
# show CLI arguments |
| 373 |
if self._display.verbosity > 3: |
| 374 |
if context.CLIARGS.get('args'): |
| 375 |
self._display.display('Positional arguments: %s' % ' '.join(context.CLIARGS['args']), |
| 376 |
color=C.COLOR_VERBOSE, screen_only=True) |
| 377 |
|
| 378 |
for argument in (a for a in context.CLIARGS if a != 'args'): |
| 379 |
val = context.CLIARGS[argument] |
| 380 |
if val: |
| 381 |
self._display.display('%s: %s' % (argument, val), color=C.COLOR_VERBOSE, screen_only=True) |
| 382 |
|
| 383 |
if context.CLIARGS['check'] and self.get_option('check_mode_markers'): |
| 384 |
self._display.banner("DRY RUN") |
| 385 |
|
| 386 |
def v2_runner_retry(self, result: CallbackTaskResult) -> None: |
| 387 |
task_name = result.task_name or result.task |
| 388 |
host_label = self.host_label(result) |
| 389 |
msg = "FAILED - RETRYING: [%s]: %s (%d retries left)." % (host_label, task_name, result.result['retries'] - result.result['attempts']) |
| 390 |
if self._run_is_verbose(result, verbosity=2): |
| 391 |
msg += "Result was: %s" % self._dump_results(result.result) |
| 392 |
self._display.display(msg, color=C.COLOR_DEBUG) |
| 393 |
|
| 394 |
def v2_runner_on_async_poll(self, result: CallbackTaskResult) -> None: |
| 395 |
host = result.host.get_name() |
| 396 |
jid = result.result.get('ansible_job_id') |
| 397 |
started = result.result.get('started') |
| 398 |
finished = result.result.get('finished') |
| 399 |
self._display.display( |
| 400 |
'ASYNC POLL on %s: jid=%s started=%s finished=%s' % (host, jid, started, finished), |
| 401 |
color=C.COLOR_DEBUG |
| 402 |
) |
| 403 |
|
| 404 |
def v2_runner_on_async_ok(self, result: CallbackTaskResult) -> None: |
| 405 |
host = result.host.get_name() |
| 406 |
jid = result.result.get('ansible_job_id') |
| 407 |
self._display.display("ASYNC OK on %s: jid=%s" % (host, jid), color=C.COLOR_DEBUG) |
| 408 |
|
| 409 |
def v2_runner_on_async_failed(self, result: CallbackTaskResult) -> None: |
| 410 |
host = result.host.get_name() |
| 411 |
|
| 412 |
# Attempt to get the async job ID. If the job does not finish before the |
| 413 |
# async timeout value, the ID may be within the unparsed 'async_result' dict. |
| 414 |
jid = result.result.get('ansible_job_id') |
| 415 |
if not jid and 'async_result' in result.result: |
| 416 |
jid = result.result['async_result'].get('ansible_job_id') |
| 417 |
self._display.display("ASYNC FAILED on %s: jid=%s" % (host, jid), color=C.COLOR_DEBUG) |
| 418 |
|
| 419 |
def v2_playbook_on_notify(self, handler, host): |
| 420 |
if self._display.verbosity > 1: |
| 421 |
self._display.display("NOTIFIED HANDLER %s for %s" % (handler.get_name(), host), color=C.COLOR_VERBOSE, screen_only=True) |