126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
# File 'lib/dapp/dimg/builder/ansible/assets.rb', line 126
def live_py
%{
# (c) 2012-2014, Michael DeHaan <[email protected]>
# (c) 2017 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = '''
callback: live
type: stdout
short_description: screen output for solo mode
version_added: historical
description:
- Solo mode with live stdout for raw and script tasks with fallback to minimal
'''
from ansible.plugins.callback import CallbackBase
from ansible import constants as C
from ansible.vars.manager import strip_internal_keys
import json, re
class CallbackModule(CallbackBase):
'''
This is the default callback interface, which simply prints messages
to stdout when new callback events are received.
'''
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = 'stdout'
CALLBACK_NAME = 'live'
# name for this tasks can be generated from free_form
FREE_FORM_MODULES = ('raw', 'script', 'command', 'shell')
#ERROR! this task 'debug' has extra params, which is only allowed in the following modules: command, win_command, shell, win_shell, script, include, include_vars, include_tasks, include_role, import_tasks, import_role, add_host, group_by, set_fact, raw, meta
def __init__(self):
super(CallbackModule, self).__init__()
self._play = None
def _task_header(self, task, msg):
name = task.name
if not name:
if task.action in self.FREE_FORM_MODULES:
name = task.args.get('_raw_params', '')
if task.action == 'file':
name = task.args.get('path')
if task.action == 'copy':
name = task.args.get('dest')
if task.action == 'group':
name = task.args.get('name')
if task.action == 'user':
name = task.args.get('name')
if task.action == 'get_url':
name = task.args.get('url')
if task.action == 'getent':
db = task.args.get('database')
key = task.args.get('key')
name = '%s %s' % (db, key)
if task.action == 'apt':
name = task.args.get('name')
if task.action == 'composer':
name = task.args.get('command', 'install')
name = re.sub(r'\s+', r' ', name)
if len(name) > 25 :
name = '%s...' % name[0:22]
return u'%s [%s] %s' % (task.action, name, msg)
def _display_command_generic_msg(self, task, result, caption, color):
''' output the result of a command run '''
self._display.display("%s | rc=%s >>" % (self._task_header(task, caption), result.get('rc', -1)), color)
msg = result.get('msg')
if msg:
self._display.display(msg, color)
# prevent dublication in case of live_stdout
if not result.get('live_stdout', False):
stdout = result.get('stdout', None)
if stdout:
self._display.display("stdout was:", color=C.COLOR_HIGHLIGHT)
self._display.display(stdout)
stderr = result.get('stderr', '')
if stderr:
self._display.display("stderr was:", color=C.COLOR_HIGHLIGHT)
self._display.display(stderr, color=C.COLOR_ERROR)
def _display_debug_msg(self, task, result):
color = C.COLOR_OK
if task.args.get('msg'):
self._display.display("debug msg", color=C.COLOR_HIGHLIGHT)
self._display.display(result.get('msg', ''), color)
if task.args.get('var'):
self._display.display("debug var \'%s\'" % task.args.get('var'), color=C.COLOR_HIGHLIGHT)
var_obj = result.get(task.args.get('var'), '')
if isinstance(var_obj, str):
if 'IS NOT DEFINED' in var_obj:
color = C.COLOR_ERROR
path = task.get_path()
if path:
self._display.display(u"task path: %s" % path, color=C.COLOR_DEBUG)
self._display.display(var_obj, color)
else:
self._display.display(json.dumps(var_obj, indent=4), color)
# TODO remove stdout here if live_stdout!
# TODO handle results for looped tasks
def _dump_results(self, result, indent=None, sort_keys=True, keep_invocation=False):
if not indent and (result.get('_ansible_verbose_always') or self._display.verbosity > 2):
indent = 4
# All result keys stating with _ansible_ are internal, so remove them from the result before we output anything.
abridged_result = strip_internal_keys(result)
# remove invocation unless specifically wanting it
if not keep_invocation and self._display.verbosity < 3 and 'invocation' in result:
del abridged_result['invocation']
# remove diff information from screen output
if self._display.verbosity < 3 and 'diff' in result:
del abridged_result['diff']
# remove exception from screen output
if 'exception' in abridged_result:
del abridged_result['exception']
# remove msg, failed, changed
if 'msg' in abridged_result:
del abridged_result['msg']
if 'failed' in abridged_result:
del abridged_result['failed']
if 'changed' in abridged_result:
del abridged_result['changed']
if len(abridged_result) > 0:
return json.dumps(abridged_result, indent=indent, ensure_ascii=False, sort_keys=sort_keys)
return ''
def v2_playbook_on_play_start(self, play):
self._play = play
# command [copy artifacts] started
# stdout
# ...
# command [copy artifacts] OK/FAILED/CHANGED
# STDERR: if failed
# ...
#
def v2_playbook_on_task_start(self, task, is_conditional):
self._display.v("TASK action=%s args=%s" % (task.action, json.dumps(task.args, indent=4)))
if task.action == 'debug':
return
if self._play.strategy != 'free':
self._display.display(self._task_header(task, "started"), color=C.COLOR_HIGHLIGHT)
def v2_runner_on_ok(self, result):
self._display.v("TASK action=%s OK => %s" % (result._task.action, json.dumps(result._result, indent=4)))
self._clean_results(result._result, result._task.action)
self._handle_warnings(result._result)
task = result._task
if task.action == 'debug':
self._display_debug_msg(result._task, result._result)
elif task.action in self.FREE_FORM_MODULES:
self._display_command_generic_msg(result._task, result._result, "SUCCESS", C.COLOR_OK)
else:
if 'changed' in result._result and result._result['changed']:
self._display.display("%s => %s" % (self._task_header(result._task, "SUCCESS"), self._dump_results(result._result, indent=4)), color=C.COLOR_CHANGED)
#self._display.display(self._task_header(task, "OK")"%s | SUCCESS => %s" % (result._host.get_name(), self._dump_results(result._result, indent=4)), color=C.COLOR_CHANGED)
#self._display.display("%s | SUCCESS => %s" % (result._host.get_name(), ), color=C.COLOR_CHANGED)
else:
self._display.display("%s => %s" % (self._task_header(result._task, "SUCCESS"), self._dump_results(result._result, indent=4)), color=C.COLOR_OK)
#self._display.display("%s | SUCCESS => %s" % (result._host.get_name(), self._dump_results(result._result, indent=4)), color=C.COLOR_OK)
def v2_runner_on_failed(self, result, ignore_errors=False):
self._display.v("TASK action=%s FAILED => %s" % (result._task.action, json.dumps(result._result, indent=4)))
self._handle_exception(result._result)
self._handle_warnings(result._result)
task = result._task
if task.action in self.FREE_FORM_MODULES:
self._display_command_generic_msg(result._task, result._result, "FAILED", C.COLOR_ERROR)
#elif result._task.action in C.MODULE_NO_JSON and 'module_stderr' not in result._result:
# self._display.display(self._command_generic_msg(result._host.get_name(), result._result, "FAILED"), color=C.COLOR_ERROR)
else:
self._display.display(self._task_header(result._task, "FAILED"), color=C.COLOR_ERROR)
if 'msg' in result._result:
self._display.display(result._result['msg'], color=C.COLOR_ERROR)
# clean system values from result and return a json
dump_result = self._dump_results(result._result, indent=4)
if dump_result:
self._display.display("Task result => %s" % (self._dump_results(result._result, indent=4)), color=C.COLOR_ERROR)
def v2_runner_on_skipped(self, result):
self._display.display("%s | SKIPPED" % (result._host.get_name()), color=C.COLOR_SKIP)
def v2_runner_on_unreachable(self, result):
self._display.display("%s | UNREACHABLE! => %s" % (result._host.get_name(), self._dump_results(result._result, indent=4)), color=C.COLOR_UNREACHABLE)
def v2_on_file_diff(self, result):
if 'diff' in result._result and result._result['diff']:
self._display.display(self._get_diff(result._result['diff']))
}
end
|