2828
2929DEFAULT_DD_URL = "https://api.datadoghq.com"
3030
31+
3132class CallbackModule (CallbackBase ):
3233 def __init__ (self ):
3334 if IMPORT_ERROR is not None :
@@ -175,24 +176,24 @@ def pluralize(number, noun):
175176
176177 # format helper for event_text
177178 @staticmethod
178- def format_result (res ):
179+ def format_result (result ):
180+ res = result ._result
181+ module_name = result ._task .action
179182 msg = "$$$\n {0}\n $$$\n " .format (res ['msg' ]) if res .get ('msg' ) else ""
180- module_name = 'undefined'
181183
182184 if res .get ('censored' ):
183185 event_text = res .get ('censored' )
184186 elif not res .get ('invocation' ):
185187 event_text = msg
186188 else :
187189 invocation = res ['invocation' ]
188- module_name = invocation .get ('module_name' , 'undefined' )
189190 event_text = "$$$\n {0}[{1}]\n $$$\n " .format (module_name , invocation .get ('module_args' , '' ))
190191 event_text += msg
191- if 'module_stdout ' in res :
192+ if 'stdout ' in res :
192193 # On Ansible v2, details on internal failures of modules are not reported in the `msg`,
193194 # so we have to extract the info differently
194195 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 ' , '' ))
196197
197198 module_name_tag = 'module:{0}' .format (module_name )
198199
@@ -207,13 +208,13 @@ def get_dd_hostname(self, ansible_hostname):
207208 return dd_hostname
208209
209210 ### 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 () )
212213 # don't post anything if user asked to ignore errors
213214 if ignore_errors :
214215 return
215216
216- event_text , module_name_tag = self .format_result (res )
217+ event_text , module_name_tag = self .format_result (result )
217218 self .send_task_event (
218219 'Ansible task failed on "{0}"' .format (host ),
219220 alert_type = 'error' ,
@@ -222,11 +223,11 @@ def runner_on_failed(self, host, res, ignore_errors=False):
222223 host = host ,
223224 )
224225
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 () )
227228 # 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 )
230231 self .send_task_event (
231232 'Ansible task changed on "{0}"' .format (host ),
232233 alert_type = 'success' ,
@@ -235,8 +236,9 @@ def runner_on_ok(self, host, res):
235236 host = host ,
236237 )
237238
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 ())
240242 event_text = "\n $$$\n {0}\n $$$\n " .format (res )
241243 self .send_task_event (
242244 'Ansible failed on unreachable host "{0}"' .format (host ),
0 commit comments