Skip to content

Commit 8b7cc45

Browse files
author
Slavek Kabrda
authored
Merge pull request #60 from DataDog/slavek.kabrda/hostname-mismatch
Explain Ansible/Datadog hostname mismatch, allow overriding hostname
2 parents 39f844c + 592084c commit 8b7cc45

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,38 @@ callback_whitelist = datadog_callback
8181

8282
You should start seeing Ansible events and metrics appear on Datadog when your playbook is run.
8383

84+
## Inventory hostnames vs Datadog hostnames
85+
86+
By default, the events reported for individual hosts use inventory hostnames
87+
as the value for the event `host` tag. This can lead to problems when Ansible
88+
inventory hostnames are different than hostnames detected by the Datadog Agent.
89+
In this case, the events are going to be reported for a seemingly non-existent
90+
host (the inventory hostname), which will then disappear after some time
91+
of inactivity. There are several possible solutions in this case. Let's assume
92+
that we have a host `some.hostname.com` which is detected as
93+
`datadog.detected.hostname.com` by the Datadog Agent:
94+
95+
* Use Ansible [inventory aliases](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#inventory-aliases):
96+
* Original inventory file:
97+
```
98+
[servers]
99+
some.hostname.com
100+
```
101+
* Adjusted inventory file using alias:
102+
```
103+
[servers]
104+
datadog.detected.hostname.com ansible_host=some.hostname.com
105+
```
106+
* Overwrite the `get_dd_hostname` method in `datadog_callback.py`:
107+
```
108+
def get_dd_hostname(self, ansible_hostname):
109+
""" This function allows providing custom logic that transforms an Ansible
110+
inventory hostname to a Datadog hostname.
111+
"""
112+
dd_hostname = ansible_hostname.replace("some.", "datadog.detected.")
113+
return dd_hostname
114+
```
115+
84116
## Contributing to ansible-datadog-callback
85117
86118
1. Fork it

datadog_callback.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 += "\nErrors 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

Comments
 (0)