Motr  M0
pretty_print.py
Go to the documentation of this file.
1 #
2 # Copyright (c) 2020 Seagate Technology LLC and/or its Affiliates
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # For any questions about this software or licensing,
17 # please email opensource@seagate.com or cortx-questions@seagate.com.
18 #
19 
20 # Ansible plugin for pretty-printing playbook tasks output
21 
22 from __future__ import (absolute_import, division, print_function)
23 from ansible.plugins.callback import CallbackBase
24 import json
25 __metaclass__ = type
26 
27 RECORDS = (
28  #'failed',
29  'msg',
30  'reason',
31  'results',
32  'stderr',
33  'stdout',
34 )
35 
36 class CallbackModule(CallbackBase):
37 
38 
39  CALLBACK_NAME = 'pretty_print'
40  CALLBACK_TYPE = 'notification'
41  CALLBACK_VERSION = 2.0
42  CALLBACK_NEEDS_WHITELIST = False
43 
44  #
45  # helpers ------------------------------
46  #
47 
48  def pretty_print(self, data):
49  if type(data) is dict:
50  for rec in RECORDS:
51  no_log = data.get('_ansible_no_log', False)
52  if rec in data and data[rec] and not no_log:
53  output = self._format(data[rec]).replace("\\n", "\n")
54  self._display.display("{0}: {1}".format(rec, output),
55  log_only=False)
56 
57  def _format(self, output):
58  if type(output) is dict:
59  return json.dumps(output, indent=2, sort_keys=True)
60 
61  # output may contain nested results when a task uses 'with_items'
62  if type(output) is list and type(output[0]) is dict:
63  formatted_output = []
64  for i, elem in enumerate(output):
65  if type(elem) is dict:
66  for rec in set(RECORDS) & set(elem):
67  formatted_output.append( self._format(elem[rec]) )
68  if len(formatted_output) == 1:
69  return formatted_output[0]
70  else:
71  return '\n ' + '\n '.join(formatted_output)
72 
73  if type(output) is list and type(output[0]) is not dict:
74  if len(output) == 1:
75  return output[0]
76  else:
77  return '\n ' + '\n '.join(output)
78 
79  return str(output)
80 
81  #
82  # V1 methods ---------------------------
83  #
84 
85  def runner_on_async_failed(self, host, res, jid):
86  self.pretty_print(res)
87 
88  def runner_on_async_ok(self, host, res, jid):
89  self.pretty_print(res)
90 
91  def runner_on_async_poll(self, host, res, jid, clock):
92  self.pretty_print(res)
93 
94  def runner_on_failed(self, host, res, ignore_errors=False):
95  self.pretty_print(res)
96 
97  def runner_on_ok(self, host, res):
98  self.pretty_print(res)
99 
100  def runner_on_unreachable(self, host, res):
101  self.pretty_print(res)
102 
103  #
104  # V2 methods ---------------------------
105  #
106 
107  def v2_runner_on_async_failed(self, result):
108  self.pretty_print(result._result)
109 
110  def v2_runner_on_async_ok(self, host, result):
111  self.pretty_print(result._result)
112 
113  def v2_runner_on_async_poll(self, result):
114  self.pretty_print(result._result)
115 
116  def v2_runner_on_failed(self, result, ignore_errors=False):
117  self.pretty_print(result._result)
118 
119  def v2_runner_on_ok(self, result):
120  self.pretty_print(result._result)
121 
122  def v2_runner_on_unreachable(self, result):
123  self.pretty_print(result._result)
def runner_on_async_ok(self, host, res, jid)
Definition: pretty_print.py:88
def runner_on_failed(self, host, res, ignore_errors=False)
Definition: pretty_print.py:94
def v2_runner_on_ok(self, result)
def v2_runner_on_unreachable(self, result)
def runner_on_async_poll(self, host, res, jid, clock)
Definition: pretty_print.py:91
def v2_runner_on_failed(self, result, ignore_errors=False)
def pretty_print(self, data)
Definition: pretty_print.py:48
def v2_runner_on_async_ok(self, host, result)
def runner_on_unreachable(self, host, res)
def runner_on_async_failed(self, host, res, jid)
Definition: pretty_print.py:85
format
Definition: hist.py:128
def runner_on_ok(self, host, res)
Definition: pretty_print.py:97
def _format(self, output)
Definition: pretty_print.py:57
def v2_runner_on_async_poll(self, result)
int type
Definition: dir.c:1031
def v2_runner_on_async_failed(self, result)