Skip to content

Takes contributor change suggested by filipenf:use-inventory-hostname, #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ Once the required libraries (see above) have been installed on the server:
1. Copy `datadog_callback.py` to your playbook callback directory (by default
`callback_plugins/` in your playbook's root directory). Create the directory
if it doesn't exist.
2. You have 3 ways to set your API key. The callback will first use the
environment variable, then the configuration file, then hostvars/vault.
2. You have 4 ways to set your API key. The callback will first use the
environment variable, then the configuration file, then hostvars/vault,
and a specific host's hostvars. For more information, see the environment
variable configuration.

##### Using environment variable

Expand All @@ -37,6 +39,11 @@ variable `DATADOG_SITE=datadoghq.eu`.
To send data to a custom URL you can set the environment
variable `DATADOG_URL=<custom URL>`.

To configure the callback to use variables from a specific inventory host,
you can set the environment variable `ANSIBLE_DATADOG_CALLBACK_INVENTORY_HOST`.
The callback will then query that host's variables for the API key, datadog URL,
and datadog site.

##### Using a yaml file

Create a `datadog_callback.yml` file alongside `datadog_callback.py`,
Expand Down
45 changes: 31 additions & 14 deletions datadog_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import getpass
import logging
import os
import socket
import time


try:
import datadog
import yaml
Expand Down Expand Up @@ -241,6 +243,25 @@ def v2_playbook_on_start(self, playbook):
inventory = ','.join(inventory)
self._inventory_name = ','.join([os.path.basename(os.path.realpath(name)) for name in inventory.split(',') if name])

def get_from_hostvars(self, variable_name, required=False):
hostvars = self.play.get_variable_manager()._hostvars
if not hostvars:
raise Exception("hostvars could not be loaded")
hosts = [ 'localhost', socket.gethostname() ]
if os.environ.get("ANSIBLE_DATADOG_CALLBACK_INVENTORY_HOST"):
hosts.insert(0, os.environ.get("ANSIBLE_DATADOG_CALLBACK_INVENTORY_HOST"))
for host in hosts:
if host not in hostvars:
continue
value = hostvars[host].get(variable_name, None)
if value is not None:
return value
if required:
raise Exception("variable {} is not defined in the host vars for hosts: {}".format(
variable_name, hosts))
else:
return None

def v2_playbook_on_play_start(self, play):
# On Ansible v2, Ansible doesn't set `self.play` automatically
self.play = play
Expand All @@ -253,21 +274,17 @@ def v2_playbook_on_play_start(self, play):

# If there is no api key defined in config file, try to get it from hostvars
if api_key == '':
hostvars = self.play.get_variable_manager()._hostvars

if not hostvars:
print("No api_key found in the config file ({0}) and hostvars aren't set: disabling Datadog callback plugin".format(config_path))
try:
api_key = self.get_from_hostvars('datadog_api_key', True)
if not dd_url:
dd_url = self.get_from_hostvars('datadog_url')

if not dd_site:
dd_site = self.get_from_hostvars('datadog_site')
except Exception as e:
print('No "api_key" found in the config file ({0}) and could not load from the inventory ({1}): disabling Datadog callback plugin'.
format(config_path, e))
self.disabled = True
else:
try:
api_key = hostvars['localhost']['datadog_api_key']
if not dd_url:
dd_url = hostvars['localhost'].get('datadog_url')
if not dd_site:
dd_site = hostvars['localhost'].get('datadog_site')
except Exception as e:
print('No "api_key" found in the config file ({0}) and "datadog_api_key" is not set in the hostvars: disabling Datadog callback plugin'.format(config_path))
self.disabled = True

if not dd_url:
if dd_site:
Expand Down