Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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][])

Expand Down
15 changes: 6 additions & 9 deletions datadog_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down