Skip to content

Commit 84cb667

Browse files
committed
Support for dd_url through env vars and hostvars
This allow customer to use the ansible callback with the Datadog EU intake.
1 parent 2c7e28b commit 84cb667

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
CHANGELOG
22
=========
33

4+
# 2.5.0 / 2019-02-07
5+
* [FEATURE] Add support for "site" configuration.
6+
47
# 2.4.2 / 2018-06-08
58
- [BUGFIX] Fix yaml import broken by Ansible 2.5.
69

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ if it doesn't exist.
2828

2929
Set the environment variable `DATADOG_API_KEY`.
3030

31+
Optionally to send data to Datadog EU, you can set the environment
32+
variable `DATADOG_SITE=datadoghq.eu`.
33+
34+
To send data to a custom URL you can set the environment
35+
variable `DATADOG_URL=<custom URL>`.
36+
3137
##### Using a yaml file
3238

3339
Create a `datadog_callback.yml` file alongside `datadog_callback.py`,
@@ -36,6 +42,11 @@ as following:
3642

3743
```
3844
api_key: <your-api-key>
45+
46+
# optionally to send data to Datadog EU add the following setting
47+
site: datadoghq.eu
48+
# optionally to send data to a custom URL add the following setting
49+
url: <custom URl>
3950
```
4051

4152
You can specify a custom location for the configuration file using the
@@ -51,6 +62,12 @@ ANSIBLE_DATADOG_CALLBACK_CONF_FILE=/etc/datadog/callback_conf.yaml ansible-playb
5162
Alternatively you can use the hostvars of the host ansible is being run from (preferably in the vault file):
5263
```
5364
datadog_api_key: <your-api-key>
65+
66+
# Optionally to send data to Datadog EU add the following setting
67+
datadog_site: datadoghq.eu
68+
69+
# Optionally to send data to a custom URL add the following setting
70+
datadog_url: <custom URL>
5471
```
5572

5673
3. Be sure to whitelist the plugin in your ansible.cfg

datadog_callback.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ansible.plugins.callback import CallbackBase
1717
from __main__ import cli
1818

19+
DEFAULT_DD_URL = "https://api.datadoghq.com"
1920

2021
class CallbackModule(CallbackBase):
2122
def __init__(self):
@@ -57,7 +58,10 @@ def _load_conf(self, file_path):
5758
with open(file_path, 'r') as conf_file:
5859
conf_dict = yaml.load(conf_file)
5960

60-
return os.environ.get('DATADOG_API_KEY', conf_dict.get('api_key', '')), conf_dict.get('url', 'https://app.datadoghq.com')
61+
api_key = os.environ.get('DATADOG_API_KEY', conf_dict.get('api_key', ''))
62+
dd_url = os.environ.get('DATADOG_URL', conf_dict.get('url', ''))
63+
dd_site = os.environ.get('DATADOG_SITE', conf_dict.get('site', ''))
64+
return api_key, dd_url, dd_site
6165

6266
# Send event to Datadog
6367
def _send_event(self, title, alert_type=None, text=None, tags=None, host=None, event_type=None, event_object=None):
@@ -232,7 +236,7 @@ def v2_playbook_on_play_start(self, play):
232236

233237
# Read config and hostvars
234238
config_path = os.environ.get('ANSIBLE_DATADOG_CALLBACK_CONF_FILE', os.path.join(os.path.dirname(__file__), "datadog_callback.yml"))
235-
api_key, url = self._load_conf(config_path)
239+
api_key, dd_url, dd_site = self._load_conf(config_path)
236240

237241
# If there is no api key defined in config file, try to get it from hostvars
238242
if api_key == '':
@@ -244,13 +248,23 @@ def v2_playbook_on_play_start(self, play):
244248
else:
245249
try:
246250
api_key = hostvars['localhost']['datadog_api_key']
251+
if not dd_url:
252+
dd_url = hostvars['localhost'].get('datadog_url')
253+
if not dd_site:
254+
dd_site = hostvars['localhost'].get('datadog_site')
247255
except Exception as e:
248256
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))
249257
self.disabled = True
250258

259+
if not dd_url:
260+
if dd_site:
261+
dd_url = "https://api."+ dd_site
262+
else:
263+
dd_url = DEFAULT_DD_URL # default to Datadog US
264+
251265
# Set up API client and send a start event
252266
if not self.disabled:
253-
datadog.initialize(api_key=api_key, api_host=url)
267+
datadog.initialize(api_key=api_key, api_host=dd_url)
254268

255269
self.send_playbook_event(
256270
'Ansible play "{0}" started in playbook "{1}" by "{2}" against "{3}"'.format(

0 commit comments

Comments
 (0)