diff --git a/README.md b/README.md index a3d0677..406fe8f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/datadog_callback.py b/datadog_callback.py index 469e6c8..33b167a 100644 --- a/datadog_callback.py +++ b/datadog_callback.py @@ -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 @@ -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) @@ -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), @@ -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']]) @@ -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,