From 15539c545274aba466d88ae32ff4042e5577840e Mon Sep 17 00:00:00 2001 From: Maxime Mouial Date: Fri, 23 Feb 2018 16:01:58 -0500 Subject: [PATCH] Avoid printing error about the conf file when customer use a vault --- CHANGELOG.md | 3 +++ datadog_callback.py | 15 ++++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53d01ae..ad94d89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ CHANGELOG ========= +# 2.4.1 / 2018-02-07 +- [BUGFIX] Avoid printing error about the conf file when using a vault. See [#34][] + # 2.4.0 / 2018-02-07 - [FEATURE] Add support of python3 (and still fully support python2). See [#33][] (thanks to [@DSpeichert][]) diff --git a/datadog_callback.py b/datadog_callback.py index 6faddf0..97a176c 100644 --- a/datadog_callback.py +++ b/datadog_callback.py @@ -49,19 +49,14 @@ def _set_logger_level(self, name, level=logging.WARNING): print(e) # Load parameters from conf file - def _load_conf(self): - file_path = os.environ.get('ANSIBLE_DATADOG_CALLBACK_CONF_FILE', os.path.join(os.path.dirname(__file__), "datadog_callback.yml")) - + def _load_conf(self, file_path): conf_dict = {} if os.path.isfile(file_path): with open(file_path, 'r') as conf_file: conf_dict = yaml.load(conf_file) - else: - print("Could not load configuration, invalid file: {}".format(file_path)) return os.environ.get('DATADOG_API_KEY', conf_dict.get('api_key', '')), conf_dict.get('url', 'https://app.datadoghq.com') - # Send event to Datadog def _send_event(self, title, alert_type=None, text=None, tags=None, host=None, event_type=None, event_object=None): if tags is None: @@ -234,19 +229,21 @@ def v2_playbook_on_play_start(self, play): return # Read config and hostvars - api_key, url = self._load_conf() + config_path = os.environ.get('ANSIBLE_DATADOG_CALLBACK_CONF_FILE', os.path.join(os.path.dirname(__file__), "datadog_callback.yml")) + api_key, url = self._load_conf(config_path) + # If there is no api key defined in config file, try to get it from hostvars if api_key == '': hostvars = self.play.get_variable_manager()._hostvars if not hostvars: - print("No api_key found in the config file and hostvars aren't set: disabling Datadog callback plugin") + print("No api_key found in the config file ({0}) and hostvars aren't set: disabling Datadog callback plugin".format(config_path)) self.disabled = True else: try: api_key = hostvars['localhost']['datadog_api_key'] except Exception as e: - print('No "api_key" found in the config file and {0} is not set in the hostvars: disabling Datadog callback plugin'.format(e)) + 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)) self.disabled = True # Set up API client and send a start event