Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,38 @@ callback_whitelist = datadog_callback

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

## Inventory hostnames vs Datadog hostnames

By default, the events reported for individual hosts use inventory hostnames
as the value for the event `host` tag. This can lead to problems when Ansible
inventory hostnames are different than hostnames detected by the Datadog Agent.
In this case, the events are going to be reported for a seemingly non-existent
host (the inventory hostname), which will then disappear after some time
of inactivity. There are several possible solutions in this case. Let's assume
that we have a host `some.hostname.com` which is detected as
`datadog.detected.hostname.com` by the Datadog Agent:

* Use Ansible [inventory aliases](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#inventory-aliases):
* Original inventory file:
```
[servers]
some.hostname.com
```
* Adjusted inventory file using alias:
```
[servers]
datadog.detected.hostname.com ansible_host=some.hostname.com
```
* Overwrite the `get_dd_hostname` method in `datadog_callback.py`:
```
def get_dd_hostname(self, ansible_hostname):
""" This function allows providing custom logic that transforms an Ansible
inventory hostname to a Datadog hostname.
"""
dd_hostname = ansible_hostname.replace("some.", "datadog.detected.")
return dd_hostname
```

## Contributing to ansible-datadog-callback

1. Fork it
Expand Down
13 changes: 13 additions & 0 deletions datadog_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,17 @@ def format_result(res):

return event_text, module_name_tag

def get_dd_hostname(self, ansible_hostname):
""" This function allows providing custom logic that transforms an Ansible
inventory hostname to a Datadog hostname.
"""
dd_hostname = ansible_hostname
# provide your code to obtain Datadog hostname from Ansible inventory hostname
return dd_hostname

### Ansible callbacks ###
def runner_on_failed(self, host, res, ignore_errors=False):
host = self.get_dd_hostname(host)
# don't post anything if user asked to ignore errors
if ignore_errors:
return
Expand All @@ -201,6 +210,7 @@ def runner_on_failed(self, host, res, ignore_errors=False):
)

def runner_on_ok(self, host, res):
host = self.get_dd_hostname(host)
# Only send an event when the task has changed on the host
if res.get('changed'):
event_text, module_name_tag = self.format_result(res)
Expand All @@ -213,6 +223,7 @@ def runner_on_ok(self, host, res):
)

def runner_on_unreachable(self, host, res):
host = self.get_dd_hostname(host)
event_text = "\n$$$\n{0}\n$$$\n".format(res)
self.send_task_event(
'Ansible failed on unreachable host "{0}"'.format(host),
Expand Down Expand Up @@ -294,6 +305,7 @@ def playbook_on_stats(self, stats):
total_errors = 0
error_hosts = []
for host in stats.processed:
host = self.get_dd_hostname(host)
# Aggregations for the event text
summary = stats.summarize(host)
total_tasks += sum([summary['ok'], summary['failures'], summary['skipped']])
Expand Down Expand Up @@ -327,6 +339,7 @@ def playbook_on_stats(self, stats):
event_title += ' with errors'
event_text += "\nErrors occurred on the following hosts:\n%%%\n"
for host, failures, unreachable in error_hosts:
host = self.get_dd_hostname(host)
event_text += "- `{0}` (failure: {1}, unreachable: {2})\n".format(
host,
failures,
Expand Down