| 1 |
from __future__ import unicode_literals |
| 2 |
import io |
| 3 |
import os |
| 4 |
|
| 5 |
from ansible import constants as C |
| 6 |
from ansible.module_utils import six |
| 7 |
|
| 8 |
try: |
| 9 |
from ansible.plugins import callback_loader |
| 10 |
except ImportError: |
| 11 |
from ansible.plugins.loader import callback_loader |
| 12 |
|
| 13 |
try: |
| 14 |
pprint = __import__(os.environ['NICE_STDOUT_PPRINT']) |
| 15 |
except KeyError: |
| 16 |
pprint = None |
| 17 |
|
| 18 |
DefaultModule = callback_loader.get('default', class_only=True) |
| 19 |
DOCUMENTATION = ''' |
| 20 |
callback: nice_stdout |
| 21 |
type: stdout |
| 22 |
extends_documentation_fragment: |
| 23 |
- default_callback |
| 24 |
options: |
| 25 |
check_mode_markers: |
| 26 |
name: Show markers when running in check mode |
| 27 |
description: |
| 28 |
- "Toggle to control displaying markers when running in check mode. The markers are C(DRY RUN) |
| 29 |
at the beggining and ending of playbook execution (when calling C(ansible-playbook --check)) |
| 30 |
and C(CHECK MODE) as a suffix at every play and task that is run in check mode." |
| 31 |
type: bool |
| 32 |
default: no |
| 33 |
version_added: 2.9 |
| 34 |
env: |
| 35 |
- name: ANSIBLE_CHECK_MODE_MARKERS |
| 36 |
ini: |
| 37 |
- key: check_mode_markers |
| 38 |
section: defaults |
| 39 |
''' |
| 40 |
|
| 41 |
|
| 42 |
def printi(tio, obj, key=None, indent=0): |
| 43 |
def write(s, *args): |
| 44 |
if args: |
| 45 |
s %= args |
| 46 |
tio.write(' ' * indent) |
| 47 |
if key is not None: |
| 48 |
tio.write('%s: ' % (key,)) |
| 49 |
tio.write(s) |
| 50 |
tio.write('\n') |
| 51 |
|
| 52 |
if isinstance(obj, (list, tuple)): |
| 53 |
write('[') |
| 54 |
for i, obj2 in enumerate(obj): |
| 55 |
printi(tio, obj2, key=i, indent=indent+1) |
| 56 |
key = None |
| 57 |
write(']') |
| 58 |
elif isinstance(obj, dict): |
| 59 |
write('{') |
| 60 |
for key2, obj2 in sorted(six.iteritems(obj)): |
| 61 |
if not (key2.startswith('_ansible_') or |
| 62 |
key2.endswith('_lines')): |
| 63 |
printi(tio, obj2, key=key2, indent=indent+1) |
| 64 |
key = None |
| 65 |
write('}') |
| 66 |
elif isinstance(obj, six.text_type): |
| 67 |
for line in obj.splitlines(): |
| 68 |
write('%s', line.rstrip('\r\n')) |
| 69 |
elif isinstance(obj, six.binary_type): |
| 70 |
obj = obj.decode('utf-8', 'replace') |
| 71 |
for line in obj.splitlines(): |
| 72 |
write('%s', line.rstrip('\r\n')) |
| 73 |
else: |
| 74 |
write('%r', obj) |
| 75 |
|
| 76 |
|
| 77 |
class CallbackModule(DefaultModule): |
| 78 |
CALLBACK_VERSION = 2.0 |
| 79 |
CALLBACK_TYPE = 'stdout' |
| 80 |
CALLBACK_NAME = 'nice_stdout' |
| 81 |
|
| 82 |
def _dump_results(self, result, *args, **kwargs): |
| 83 |
try: |
| 84 |
tio = io.StringIO() |
| 85 |
if pprint: |
| 86 |
pprint.pprint(result, stream=tio) |
| 87 |
else: |
| 88 |
printi(tio, result) |
| 89 |
return tio.getvalue() #.encode('ascii', 'replace') |
| 90 |
except: |
| 91 |
import traceback |
| 92 |
traceback.print_exc() |
| 93 |
raise |
| 94 |
|
| 95 |
def v2_runner_on_failed(self, result, ignore_errors=False): |
| 96 |
delegated_vars = result._result.get('_ansible_delegated_vars') |
| 97 |
self._clean_results(result._result, result._task.action) |
| 98 |
|
| 99 |
if self._play.strategy == 'free' and self._last_task_banner != result._task._uuid: |
| 100 |
self._print_task_banner(result._task) |
| 101 |
|
| 102 |
self._handle_exception(result._result) |
| 103 |
self._handle_warnings(result._result) |
| 104 |
|
| 105 |
if result._task.loop and 'results' in result._result: |
| 106 |
return |
| 107 |
|
| 108 |
if delegated_vars: |
| 109 |
msg = "[%s -> %s]: FAILED! => %s" % ( |
| 110 |
result._host.get_name(), |
| 111 |
delegated_vars['ansible_host'], |
| 112 |
self._dump_results(result._result), |
| 113 |
) |
| 114 |
else: |
| 115 |
msg = "[%s]: FAILED! => %s" % ( |
| 116 |
result._host.get_name(), |
| 117 |
self._dump_results(result._result), |
| 118 |
) |
| 119 |
|
| 120 |
s = "fatal: %s: %s" % ( |
| 121 |
result._task.get_path() or '(dynamic task)', |
| 122 |
msg, |
| 123 |
) |
| 124 |
self._display.display(s, color=C.COLOR_ERROR) |