@@ -185,8 +185,17 @@ def format_result(res):
185185
186186 return event_text , module_name_tag
187187
188+ def get_dd_hostname (self , ansible_hostname ):
189+ """ This function allows providing custom logic that transforms an Ansible
190+ inventory hostname to a Datadog hostname.
191+ """
192+ dd_hostname = ansible_hostname
193+ # provide your code to obtain Datadog hostname from Ansible inventory hostname
194+ return dd_hostname
195+
188196 ### Ansible callbacks ###
189197 def runner_on_failed (self , host , res , ignore_errors = False ):
198+ host = self .get_dd_hostname (host )
190199 # don't post anything if user asked to ignore errors
191200 if ignore_errors :
192201 return
@@ -201,6 +210,7 @@ def runner_on_failed(self, host, res, ignore_errors=False):
201210 )
202211
203212 def runner_on_ok (self , host , res ):
213+ host = self .get_dd_hostname (host )
204214 # Only send an event when the task has changed on the host
205215 if res .get ('changed' ):
206216 event_text , module_name_tag = self .format_result (res )
@@ -213,6 +223,7 @@ def runner_on_ok(self, host, res):
213223 )
214224
215225 def runner_on_unreachable (self , host , res ):
226+ host = self .get_dd_hostname (host )
216227 event_text = "\n $$$\n {0}\n $$$\n " .format (res )
217228 self .send_task_event (
218229 'Ansible failed on unreachable host "{0}"' .format (host ),
@@ -294,6 +305,7 @@ def playbook_on_stats(self, stats):
294305 total_errors = 0
295306 error_hosts = []
296307 for host in stats .processed :
308+ host = self .get_dd_hostname (host )
297309 # Aggregations for the event text
298310 summary = stats .summarize (host )
299311 total_tasks += sum ([summary ['ok' ], summary ['failures' ], summary ['skipped' ]])
@@ -327,6 +339,7 @@ def playbook_on_stats(self, stats):
327339 event_title += ' with errors'
328340 event_text += "\n Errors occurred on the following hosts:\n %%%\n "
329341 for host , failures , unreachable in error_hosts :
342+ host = self .get_dd_hostname (host )
330343 event_text += "- `{0}` (failure: {1}, unreachable: {2})\n " .format (
331344 host ,
332345 failures ,
0 commit comments