28
28
29
29
DEFAULT_DD_URL = "https://api.datadoghq.com"
30
30
31
+
31
32
class CallbackModule (CallbackBase ):
32
33
def __init__ (self ):
33
34
if IMPORT_ERROR is not None :
@@ -175,24 +176,24 @@ def pluralize(number, noun):
175
176
176
177
# format helper for event_text
177
178
@staticmethod
178
- def format_result (res ):
179
+ def format_result (result ):
180
+ res = result ._result
181
+ module_name = result ._task .action
179
182
msg = "$$$\n {0}\n $$$\n " .format (res ['msg' ]) if res .get ('msg' ) else ""
180
- module_name = 'undefined'
181
183
182
184
if res .get ('censored' ):
183
185
event_text = res .get ('censored' )
184
186
elif not res .get ('invocation' ):
185
187
event_text = msg
186
188
else :
187
189
invocation = res ['invocation' ]
188
- module_name = invocation .get ('module_name' , 'undefined' )
189
190
event_text = "$$$\n {0}[{1}]\n $$$\n " .format (module_name , invocation .get ('module_args' , '' ))
190
191
event_text += msg
191
- if 'module_stdout ' in res :
192
+ if 'stdout ' in res :
192
193
# On Ansible v2, details on internal failures of modules are not reported in the `msg`,
193
194
# so we have to extract the info differently
194
195
event_text += "$$$\n {0}\n {1}\n $$$\n " .format (
195
- res .get ('module_stdout ' , '' ), res .get ('module_stderr ' , '' ))
196
+ res .get ('stdout ' , '' ), res .get ('stderr ' , '' ))
196
197
197
198
module_name_tag = 'module:{0}' .format (module_name )
198
199
@@ -207,13 +208,13 @@ def get_dd_hostname(self, ansible_hostname):
207
208
return dd_hostname
208
209
209
210
### Ansible callbacks ###
210
- def runner_on_failed (self , host , res , ignore_errors = False ):
211
- host = self .get_dd_hostname (host )
211
+ def v2_runner_on_failed (self , result , ignore_errors = False ):
212
+ host = self .get_dd_hostname (result . _host . get_name () )
212
213
# don't post anything if user asked to ignore errors
213
214
if ignore_errors :
214
215
return
215
216
216
- event_text , module_name_tag = self .format_result (res )
217
+ event_text , module_name_tag = self .format_result (result )
217
218
self .send_task_event (
218
219
'Ansible task failed on "{0}"' .format (host ),
219
220
alert_type = 'error' ,
@@ -222,11 +223,11 @@ def runner_on_failed(self, host, res, ignore_errors=False):
222
223
host = host ,
223
224
)
224
225
225
- def runner_on_ok (self , host , res ):
226
- host = self .get_dd_hostname (host )
226
+ def v2_runner_on_ok (self , result ):
227
+ host = self .get_dd_hostname (result . _host . get_name () )
227
228
# Only send an event when the task has changed on the host
228
- if res .get ('changed' ):
229
- event_text , module_name_tag = self .format_result (res )
229
+ if result . _result .get ('changed' ):
230
+ event_text , module_name_tag = self .format_result (result )
230
231
self .send_task_event (
231
232
'Ansible task changed on "{0}"' .format (host ),
232
233
alert_type = 'success' ,
@@ -235,8 +236,9 @@ def runner_on_ok(self, host, res):
235
236
host = host ,
236
237
)
237
238
238
- def runner_on_unreachable (self , host , res ):
239
- host = self .get_dd_hostname (host )
239
+ def v2_runner_on_unreachable (self , result ):
240
+ res = result ._result
241
+ host = self .get_dd_hostname (result ._host .get_name ())
240
242
event_text = "\n $$$\n {0}\n $$$\n " .format (res )
241
243
self .send_task_event (
242
244
'Ansible failed on unreachable host "{0}"' .format (host ),
0 commit comments