Skip to content

Commit a3ab04d

Browse files
committed
All users to set a custom configuration file
1 parent 7b8eb88 commit a3ab04d

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CHANGELOG
33

44
# 2.2.0 / 2017-12-27
55
- [FEATURE] Set log level to warning for the datadog and request packages. See [#24][] (thanks to @n0ts)
6+
- [FEATURE] Allow users to set a custom location for the configuration file.
67

78
# 2.1.0 / 2017-12-26
89
- [FEATURE] Disable callback if required python packages aren't installed. See [#28][] (thanks to @dobber)

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ as following:
2929
api_key: <your-api-key>
3030
```
3131

32-
alternatively you can use the hostvars of the host ansible is being run from (preferably in the vault file):
32+
You can specify a custom location for the configuration file using the
33+
`ANSIBLE_DATADOG_CALLBACK_CONF_FILE` environment file.
34+
35+
For exemple:
36+
```
37+
ANSIBLE_DATADOG_CALLBACK_CONF_FILE=/etc/datadog/callback_conf.yaml ansible-playbook ...
38+
```
39+
40+
Alternatively you can use the hostvars of the host ansible is being run from (preferably in the vault file):
3341
```
3442
datadog_api_key: <your-api-key>
3543
```

datadog_callback.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import getpass
2-
import os.path
32
import logging
3+
import os
44
import time
55

66
try:
@@ -49,12 +49,15 @@ def _set_logger_level(self, name, level=logging.WARNING):
4949
print e
5050

5151
# Load parameters from conf file
52-
def _load_conf(self, file_path):
53-
conf_dict = {}
52+
def _load_conf(self):
53+
file_path = os.environ.get('ANSIBLE_DATADOG_CALLBACK_CONF_FILE', os.path.join(os.path.dirname(__file__), "datadog_callback.yml"))
5454

55+
conf_dict = {}
5556
if os.path.isfile(file_path):
5657
with open(file_path, 'r') as conf_file:
5758
conf_dict = yaml.load(conf_file)
59+
else:
60+
print "Could not load configuration, invalid file: {}".format(file_path)
5861

5962
return conf_dict.get('api_key', ''), conf_dict.get('url', 'https://app.datadoghq.com')
6063

@@ -226,7 +229,7 @@ def v2_playbook_on_play_start(self, play):
226229
return
227230

228231
# Read config and hostvars
229-
api_key, url = self._load_conf(os.path.join(os.path.dirname(__file__), "datadog_callback.yml"))
232+
api_key, url = self._load_conf()
230233
# If there is no api key defined in config file, try to get it from hostvars
231234
if api_key == '':
232235
hostvars = self.play.get_variable_manager()._hostvars
@@ -238,7 +241,7 @@ def v2_playbook_on_play_start(self, play):
238241
try:
239242
api_key = hostvars['localhost']['datadog_api_key']
240243
except Exception, e:
241-
print '{0} is not set neither in the config file nor hostvars: disabling Datadog callback plugin'.format(e)
244+
print 'No "api_key" found in the config file and {0} is not set in the hostvars: disabling Datadog callback plugin'.format(e)
242245
self.disabled = True
243246

244247
# Set up API client and send a start event

0 commit comments

Comments
 (0)