Skip to content

Commit 47e9d42

Browse files
szponekhush-hush
authored andcommitted
Add support for getting api_key from hostvars and thus from vault
1 parent a95b45c commit 47e9d42

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ as following:
2525
```
2626
api_key: <your-api-key>
2727
```
28+
alternatively (when using Ansible >=2.0) add:
29+
```
30+
datadog_api_key: <your-api-key>
31+
```
32+
to hostvars (preferably in the vault file) of the host ansible is being run from.
33+
34+
3. Be sure to whitelist the plugin in your ansible.cfg
35+
```
36+
[defaults]
37+
callback_whitelist = datadog_callback
38+
```
2839

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

datadog_callback.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
class CallbackModule(CallbackBase):
1919
def __init__(self):
20-
# Read config and set up API client
21-
api_key, url = self._load_conf(os.path.join(os.path.dirname(__file__), "datadog_callback.yml"))
22-
datadog.initialize(api_key=api_key, api_host=url)
2320

2421
self._playbook_name = None
2522
self._start_time = time.time()
@@ -35,8 +32,10 @@ def __init__(self):
3532
# Load parameters from conf file
3633
def _load_conf(self, file_path):
3734
conf_dict = {}
38-
with open(file_path, 'r') as conf_file:
39-
conf_dict = yaml.load(conf_file)
35+
36+
if os.path.isfile(file_path):
37+
with open(file_path, 'r') as conf_file:
38+
conf_dict = yaml.load(conf_file)
4039

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

@@ -126,15 +125,7 @@ def _handle_playbook_on_start(self, playbook_file_name, inventory):
126125
# inventory is a comma separated host list: ["host1,host2","host3,host4,"]
127126
if isinstance(inventory, basestring):
128127
inventory = inventory.split(',')
129-
inventory_name = ','.join([os.path.basename(os.path.realpath(name)) for name in inventory if name])
130-
131-
self.send_playbook_event(
132-
'Ansible playbook "{0}" started by "{1}" against "{2}"'.format(
133-
self._playbook_name,
134-
getpass.getuser(),
135-
inventory_name),
136-
event_type='start',
137-
)
128+
self._inventory_name = ','.join([os.path.basename(os.path.realpath(name)) for name in inventory if name])
138129

139130
# Default tags sent with events and metrics
140131
@property
@@ -225,6 +216,30 @@ def v2_playbook_on_start(self, playbook):
225216
def v2_playbook_on_play_start(self, play):
226217
# On Ansible v2, Ansible doesn't set `self.play` automatically
227218
self.play = play
219+
self.disabled = False
220+
221+
# Read config and hostvars
222+
api_key, url = self._load_conf(os.path.join(os.path.dirname(__file__), "datadog_callback.yml"))
223+
# If there is no api key defined in config file, try to get it from hostvars
224+
if api_key == '':
225+
226+
try:
227+
api_key = self.play.get_variable_manager()._hostvars['localhost']['datadog_api_key']
228+
except Exception, e:
229+
print '{0} is not set neither in the config file nor hostvars. Disabling Datadog callback plugin'.format(e)
230+
self.disabled = True
231+
232+
# Set up API client and send a start event
233+
if not self.disabled:
234+
datadog.initialize(api_key=api_key, api_host=url)
235+
236+
self.send_playbook_event(
237+
'Ansible playbook "{0}" started by "{1}" against "{2}"'.format(
238+
self._playbook_name,
239+
getpass.getuser(),
240+
self._inventory_name),
241+
event_type='start',
242+
)
228243

229244
def playbook_on_stats(self, stats):
230245
total_tasks = 0

0 commit comments

Comments
 (0)