-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathhuman_log.py
238 lines (172 loc) · 6.76 KB
/
human_log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Inspired from: https://github.com/redhat-openstack/khaleesi/blob/master/plugins/callbacks/human_log.py
# Further improved support Ansible 2.0
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.plugins.callback import CallbackBase
import sys
if sys.version[0] == '2':
reload(sys)
sys.setdefaultencoding("utf-8")
try:
import simplejson as json
except ImportError:
import json
# Fields to reformat output for
FIELDS = ['cmd', 'command', 'start', 'end', 'delta', 'msg', 'stdout',
'stderr', 'results', 'failed', 'reason']
class CallbackModule(CallbackBase):
"""
Ansible callback plugin for human-readable result logging
"""
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = 'notification'
CALLBACK_NAME = 'human_log'
CALLBACK_NEEDS_WHITELIST = False
def human_log(self, data):
if type(data) == dict:
for field in FIELDS:
no_log = data.get('_ansible_no_log', False)
if field in data.keys() and data[field] and no_log != True:
output = self._format_output(data[field])
# The following two lines are a hack to make it work with UTF-8 characters
if type(output) != list:
output = output.encode('utf-8', 'replace')
self._display.display("\n{0}:\n{1}".format(field, output.replace("\\n","\n")), log_only=False)
def _format_output(self, output):
# Strip unicode
if type(output) == unicode:
output = output.encode(sys.getdefaultencoding(), 'replace')
# If output is a dict
if type(output) == dict:
return json.dumps(output, indent=2, sort_keys=True)
# If output is a list of dicts
if type(output) == list and type(output[0]) == dict:
# This gets a little complicated because it potentially means
# nested results, usually because of with_items.
real_output = list()
for index, item in enumerate(output):
copy = item
if type(item) == dict:
for field in FIELDS:
if field in item.keys():
copy[field] = self._format_output(item[field])
real_output.append(copy)
return json.dumps(output, indent=2, sort_keys=True)
# If output is a list of strings
if type(output) == list and type(output[0]) != dict:
return '\n'.join(output)
# Otherwise it's a string, (or an int, float, etc.) just return it
return str(output)
def on_any(self, *args, **kwargs):
pass
def runner_on_failed(self, host, res, ignore_errors=False):
self.human_log(res)
def runner_on_ok(self, host, res):
self.human_log(res)
def runner_on_skipped(self, host, item=None):
pass
def runner_on_unreachable(self, host, res):
self.human_log(res)
def runner_on_no_hosts(self):
pass
def runner_on_async_poll(self, host, res, jid, clock):
self.human_log(res)
def runner_on_async_ok(self, host, res, jid):
self.human_log(res)
def runner_on_async_failed(self, host, res, jid):
self.human_log(res)
def playbook_on_start(self):
pass
def playbook_on_notify(self, host, handler):
pass
def playbook_on_no_hosts_matched(self):
pass
def playbook_on_no_hosts_remaining(self):
pass
def playbook_on_task_start(self, name, is_conditional):
pass
def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None):
pass
def playbook_on_setup(self):
pass
def playbook_on_import_for_host(self, host, imported_file):
pass
def playbook_on_not_import_for_host(self, host, missing_file):
pass
def playbook_on_play_start(self, name):
pass
def playbook_on_stats(self, stats):
pass
def on_file_diff(self, host, diff):
pass
####### V2 METHODS ######
def v2_on_any(self, *args, **kwargs):
pass
def v2_runner_on_failed(self, result, ignore_errors=False):
self.human_log(result._result)
def v2_runner_on_ok(self, result):
self.human_log(result._result)
def v2_runner_on_skipped(self, result):
pass
def v2_runner_on_unreachable(self, result):
self.human_log(result._result)
def v2_runner_on_no_hosts(self, task):
pass
def v2_runner_on_async_poll(self, result):
self.human_log(result._result)
def v2_runner_on_async_ok(self, host, result):
self.human_log(result._result)
def v2_runner_on_async_failed(self, result):
self.human_log(result._result)
def v2_playbook_on_start(self, playbook):
pass
def v2_playbook_on_notify(self, result, handler):
pass
def v2_playbook_on_no_hosts_matched(self):
pass
def v2_playbook_on_no_hosts_remaining(self):
pass
def v2_playbook_on_task_start(self, task, is_conditional):
pass
def v2_playbook_on_vars_prompt(self, varname, private=True, prompt=None,
encrypt=None, confirm=False, salt_size=None,
salt=None, default=None):
pass
def v2_playbook_on_setup(self):
pass
def v2_playbook_on_import_for_host(self, result, imported_file):
pass
def v2_playbook_on_not_import_for_host(self, result, missing_file):
pass
def v2_playbook_on_play_start(self, play):
pass
def v2_playbook_on_stats(self, stats):
pass
def v2_on_file_diff(self, result):
pass
def v2_playbook_on_item_ok(self, result):
pass
def v2_playbook_on_item_failed(self, result):
pass
def v2_playbook_on_item_skipped(self, result):
pass
def v2_playbook_on_include(self, included_file):
pass
def v2_playbook_item_on_ok(self, result):
pass
def v2_playbook_item_on_failed(self, result):
pass
def v2_playbook_item_on_skipped(self, result):
pass