1616from ansible .plugins .callback import CallbackBase
1717from __main__ import cli
1818
19+ DEFAULT_DD_URL = "https://api.datadoghq.com"
1920
2021class 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