16
16
from ansible .plugins .callback import CallbackBase
17
17
from __main__ import cli
18
18
19
+ DEFAULT_DD_URL = "https://api.datadoghq.com"
19
20
20
21
class CallbackModule (CallbackBase ):
21
22
def __init__ (self ):
@@ -57,7 +58,10 @@ def _load_conf(self, file_path):
57
58
with open (file_path , 'r' ) as conf_file :
58
59
conf_dict = yaml .load (conf_file )
59
60
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
61
65
62
66
# Send event to Datadog
63
67
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):
232
236
233
237
# Read config and hostvars
234
238
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 )
236
240
237
241
# If there is no api key defined in config file, try to get it from hostvars
238
242
if api_key == '' :
@@ -244,13 +248,23 @@ def v2_playbook_on_play_start(self, play):
244
248
else :
245
249
try :
246
250
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' )
247
255
except Exception as e :
248
256
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 ))
249
257
self .disabled = True
250
258
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
+
251
265
# Set up API client and send a start event
252
266
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 )
254
268
255
269
self .send_playbook_event (
256
270
'Ansible play "{0}" started in playbook "{1}" by "{2}" against "{3}"' .format (
0 commit comments