18
18
requirements:
19
19
- set as loki in configuration
20
20
options:
21
+ loki_url:
22
+ description: URL to access loki gateway
23
+ required: True
24
+ env:
25
+ - name: LOKI_URL
26
+ ini:
27
+ - section: loki
28
+ key: url
29
+ loki_username:
30
+ description: Username to access loki gateway
31
+ env:
32
+ - name: LOKI_USERNAME
33
+ ini:
34
+ - section: loki
35
+ key: username
36
+ loki_password:
37
+ description: Password to access loki gateway
38
+ env:
39
+ - name: LOKI_PASSWORD
40
+ ini:
41
+ - section: loki
42
+ key: password
43
+ loki_org_id:
44
+ description: Organization to use as a tenant when connecting to loki
45
+ env:
46
+ - name: LOKI_ORG_ID
47
+ ini:
48
+ - section: loki
49
+ key: org_id
50
+ loki_default_tags:
51
+ description: "Tags (key:value) to set for each log line"
52
+ env:
53
+ - name: LOKI_DEFAULT_TAGS
54
+ ini:
55
+ - section: loki
56
+ key: default_tags
57
+ type: list
21
58
result_format:
22
- name: Result format
23
59
default: json
24
60
description: Format used in results (will be set to json)
25
61
pretty_results:
26
- name: Print results pretty
27
62
default: False
28
63
description: Whether to print results pretty (will be set to false)
64
+ enabled_dumps:
65
+ description: |
66
+ Dumps to enable. The values playbook, diff, play, task and runner are available.
67
+ This usually requires that max_line_size in Loki is set to a higher value than 256kb.
68
+ type: list
69
+ env:
70
+ - name: LOKI_ENABLED_DUMPS
71
+ ini:
72
+ - section: loki
73
+ key: enabled_dumps
29
74
'''
30
75
31
76
@@ -37,28 +82,26 @@ class CallbackModule(CallbackBase):
37
82
CALLBACK_NAME = 'loki'
38
83
ALL_METRICS = ["changed" , "custom" , "dark" , "failures" , "ignored" , "ok" , "processed" , "rescued" , "skipped" ]
39
84
40
- def __init__ (self ):
41
- super ().__init__ ()
42
-
43
- if "LOKI_URL" not in os .environ :
44
- raise "LOKI_URL environment variable not specified."
45
-
85
+ def __init__ (self , display = None , options = None ):
86
+ super ().__init__ (display , options )
87
+ self .set_options ()
46
88
auth = ()
47
- if "LOKI_USERNAME" in os .environ and "LOKI_PASSWORD" in os .environ :
48
- auth = (os .environ ["LOKI_USERNAME" ], os .environ ["LOKI_PASSWORD" ])
89
+
90
+ if self .get_option ("loki_username" ) and self .get_option ("loki_password" ):
91
+ auth = (self .get_option ("loki_username" ), self .get_option ("loki_password" ))
49
92
50
93
headers = {}
51
- if "LOKI_ORG_ID" in os . environ :
52
- headers ["X-Scope-OrgID" ] = os . environ [ "LOKI_ORG_ID" ]
94
+ if self . get_option ( "loki_org_id" ) :
95
+ headers ["X-Scope-OrgID" ] = self . get_option ( "loki_org_id" )
53
96
54
97
tags = {}
55
- if "LOKI_DEFAULT_TAGS" in os . environ :
56
- for tagvalue in os . environ [ "LOKI_DEFAULT_TAGS" ]. split ( ", " ):
98
+ if self . get_option ( "loki_default_tags" ) :
99
+ for tagvalue in self . get_option ( "loki_default_tags " ):
57
100
(tag , value ) = tagvalue .split (":" )
58
101
tags [tag ] = value
59
102
60
103
handler = logging_loki .LokiHandler (
61
- url = os . environ [ "LOKI_URL" ] ,
104
+ url = self . get_option ( "loki_url" ) ,
62
105
tags = tags ,
63
106
auth = auth ,
64
107
headers = headers ,
@@ -77,35 +120,41 @@ def __init__(self):
77
120
self .set_option ("result_format" , "json" )
78
121
self .set_option ("pretty_results" , False )
79
122
123
+ def _dump_enabled (self , dump ):
124
+ return self .get_option ("enabled_dumps" ) and dump in self .get_option ("enabled_dumps" )
125
+
126
+
80
127
def v2_playbook_on_start (self , playbook ):
81
128
self .playbook = os .path .join (playbook ._basedir , playbook ._file_name )
82
129
self .run_timestamp = datetime .datetime .now ().isoformat ()
83
130
self .logger .info (
84
131
"Starting playbook %s" % self .playbook ,
85
132
extra = {"tags" : {"playbook" : self .playbook , "run_timestamp" : self .run_timestamp }}
86
133
)
87
- self .logger .debug (
88
- jsonpickle .encode (playbook .__dict__ ),
89
- extra = {"tags" : {"playbook" : self .playbook , "run_timestamp" : self .run_timestamp , "dump" : "playbook" }}
90
- )
134
+ if self ._dump_enabled ("playbook" ):
135
+ self .logger .debug (
136
+ jsonpickle .encode (playbook .__dict__ ),
137
+ extra = {"tags" : {"playbook" : self .playbook , "run_timestamp" : self .run_timestamp , "dump" : "playbook" }}
138
+ )
91
139
92
140
def v2_playbook_on_play_start (self , play ):
93
141
self .current_play = play .name
94
142
self .logger .info (
95
143
"Starting play %s" % play .name ,
96
144
extra = {"tags" : {"playbook" : self .playbook , "run_timestamp" : self .run_timestamp , "play" : self .current_play }}
97
145
)
98
- self .logger .debug (
99
- jsonpickle .encode (play .__dict__ ),
100
- extra = {
101
- "tags" : {
102
- "playbook" : self .playbook ,
103
- "run_timestamp" : self .run_timestamp ,
104
- "play" : self .current_play ,
105
- "dump" : "play"
146
+ if self ._dump_enabled ("play" ):
147
+ self .logger .debug (
148
+ jsonpickle .encode (play .__dict__ ),
149
+ extra = {
150
+ "tags" : {
151
+ "playbook" : self .playbook ,
152
+ "run_timestamp" : self .run_timestamp ,
153
+ "play" : self .current_play ,
154
+ "dump" : "play"
155
+ }
106
156
}
107
- }
108
- )
157
+ )
109
158
110
159
def v2_playbook_on_task_start (self , task , is_conditional ):
111
160
self .current_task = task .name
@@ -120,18 +169,19 @@ def v2_playbook_on_task_start(self, task, is_conditional):
120
169
}
121
170
}
122
171
)
123
- self .logger .debug (
124
- jsonpickle .encode (task .__dict__ ),
125
- extra = {
126
- "tags" : {
127
- "playbook" : self .playbook ,
128
- "run_timestamp" : self .run_timestamp ,
129
- "play" : self .current_play ,
130
- "task" : self .current_task ,
131
- "dump" : "task"
172
+ if self ._dump_enabled ("task" ):
173
+ self .logger .debug (
174
+ jsonpickle .encode (task .__dict__ ),
175
+ extra = {
176
+ "tags" : {
177
+ "playbook" : self .playbook ,
178
+ "run_timestamp" : self .run_timestamp ,
179
+ "play" : self .current_play ,
180
+ "task" : self .current_task ,
181
+ "dump" : "task"
182
+ }
132
183
}
133
- }
134
- )
184
+ )
135
185
136
186
def v2_runner_on_ok (self , result ):
137
187
self .logger .debug (
@@ -145,18 +195,19 @@ def v2_runner_on_ok(self, result):
145
195
}
146
196
}
147
197
)
148
- self .logger .debug (
149
- self ._dump_results (result ._result ),
150
- extra = {
151
- "tags" : {
152
- "playbook" : self .playbook ,
153
- "run_timestamp" : self .run_timestamp ,
154
- "play" : self .current_play ,
155
- "task" : self .current_task ,
156
- "dump" : "runner"
198
+ if self ._dump_enabled ("runner" ):
199
+ self .logger .debug (
200
+ self ._dump_results (result ._result ),
201
+ extra = {
202
+ "tags" : {
203
+ "playbook" : self .playbook ,
204
+ "run_timestamp" : self .run_timestamp ,
205
+ "play" : self .current_play ,
206
+ "task" : self .current_task ,
207
+ "dump" : "runner"
208
+ }
157
209
}
158
- }
159
- )
210
+ )
160
211
161
212
def v2_runner_on_failed (self , result , ignore_errors = False ):
162
213
level = logging .WARNING if ignore_errors else logging .ERROR
@@ -176,18 +227,19 @@ def v2_runner_on_failed(self, result, ignore_errors=False):
176
227
}
177
228
}
178
229
)
179
- self .logger .debug (
180
- self ._dump_results (result ._result ),
181
- extra = {
182
- "tags" : {
183
- "playbook" : self .playbook ,
184
- "run_timestamp" : self .run_timestamp ,
185
- "play" : self .current_play ,
186
- "task" : self .current_task ,
187
- "dump" : "runner"
230
+ if self ._dump_enabled ("runner" ):
231
+ self .logger .debug (
232
+ self ._dump_results (result ._result ),
233
+ extra = {
234
+ "tags" : {
235
+ "playbook" : self .playbook ,
236
+ "run_timestamp" : self .run_timestamp ,
237
+ "play" : self .current_play ,
238
+ "task" : self .current_task ,
239
+ "dump" : "runner"
240
+ }
188
241
}
189
- }
190
- )
242
+ )
191
243
192
244
def v2_runner_on_skipped (self , result ):
193
245
self .logger .info (
@@ -201,18 +253,19 @@ def v2_runner_on_skipped(self, result):
201
253
}
202
254
}
203
255
)
204
- self .logger .debug (
205
- self ._dump_results (result ._result ),
206
- extra = {
207
- "tags" : {
208
- "playbook" : self .playbook ,
209
- "run_timestamp" : self .run_timestamp ,
210
- "play" : self .current_play ,
211
- "task" : self .current_task ,
212
- "dump" : "runner"
256
+ if self ._dump_enabled ("runner" ):
257
+ self .logger .debug (
258
+ self ._dump_results (result ._result ),
259
+ extra = {
260
+ "tags" : {
261
+ "playbook" : self .playbook ,
262
+ "run_timestamp" : self .run_timestamp ,
263
+ "play" : self .current_play ,
264
+ "task" : self .current_task ,
265
+ "dump" : "runner"
266
+ }
213
267
}
214
- }
215
- )
268
+ )
216
269
217
270
def runner_on_unreachable (self , host , result ):
218
271
self .logger .error (
@@ -226,18 +279,19 @@ def runner_on_unreachable(self, host, result):
226
279
}
227
280
}
228
281
)
229
- self .logger .debug (
230
- self ._dump_results (result ),
231
- extra = {
232
- "tags" : {
233
- "playbook" : self .playbook ,
234
- "run_timestamp" : self .run_timestamp ,
235
- "play" : self .current_play ,
236
- "task" : self .current_task ,
237
- "dump" : "runner"
282
+ if self ._dump_enabled ("runner" ):
283
+ self .logger .debug (
284
+ self ._dump_results (result ),
285
+ extra = {
286
+ "tags" : {
287
+ "playbook" : self .playbook ,
288
+ "run_timestamp" : self .run_timestamp ,
289
+ "play" : self .current_play ,
290
+ "task" : self .current_task ,
291
+ "dump" : "runner"
292
+ }
238
293
}
239
- }
240
- )
294
+ )
241
295
242
296
def v2_playbook_on_no_hosts_matched (self ):
243
297
self .logger .error (
@@ -263,19 +317,20 @@ def v2_on_file_diff(self, result):
263
317
}
264
318
}
265
319
)
266
- for diff in diff_list :
267
- self .logger .debug (
268
- self ._serialize_diff (diff ),
269
- extra = {
270
- "tags" : {
271
- "playbook" : self .playbook ,
272
- "run_timestamp" : self .run_timestamp ,
273
- "play" : self .current_play ,
274
- "task" : self .current_task ,
275
- "dump" : "diff"
320
+ if self ._dump_enabled ("diff" ):
321
+ for diff in diff_list :
322
+ self .logger .debug (
323
+ self ._serialize_diff (diff ),
324
+ extra = {
325
+ "tags" : {
326
+ "playbook" : self .playbook ,
327
+ "run_timestamp" : self .run_timestamp ,
328
+ "play" : self .current_play ,
329
+ "task" : self .current_task ,
330
+ "dump" : "diff"
331
+ }
276
332
}
277
- }
278
- )
333
+ )
279
334
280
335
def v2_playbook_on_stats (self , stats ):
281
336
summarize_metrics = {}
0 commit comments