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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The following python libraries are required on the Ansible server:

- [`datadogpy`](https://github.com/DataDog/datadogpy/)
- `pyyaml` (install with `pip install pyyaml`)
- `packaging` (install with `pip install packaging`)

Ansible <=1.9 is no longer supported by this callback. The latest compatible
version is tagged with `1.0.2`.
Expand Down
23 changes: 18 additions & 5 deletions datadog_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@
try:
import datadog
import yaml
from packaging import version
HAS_MODULES = True
except ImportError:
HAS_MODULES = False


import ansible
from ansible.plugins.callback import CallbackBase
from __main__ import cli

ANSIBLE_ABOVE_28 = False
if HAS_MODULES and version.parse(ansible.__version__) >= version.parse('2.8.0'):
ANSIBLE_ABOVE_28 = True
from ansible.context import CLIARGS

DEFAULT_DD_URL = "https://api.datadoghq.com"

class CallbackModule(CallbackBase):
def __init__(self):
if not HAS_MODULES:
self.disabled = True
print('Datadog callback disabled: missing "datadog" and/or "yaml" python package.')
print('Datadog callback disabled: missing "datadog", "yaml", and/or "packaging" python package.')
else:
self.disabled = False
# Set logger level - datadog api and urllib3
Expand All @@ -32,8 +39,11 @@ def __init__(self):
self._playbook_name = None
self._start_time = time.time()
self._options = None
if cli:
self._options = cli.options
if HAS_MODULES and cli:
if ANSIBLE_ABOVE_28:
self._options = CLIARGS
else:
self._options = cli.options

# self.playbook is set in the `v2_playbook_on_start` callback method
self.playbook = None
Expand Down Expand Up @@ -217,14 +227,17 @@ def v2_playbook_on_start(self, playbook):
self.playbook = playbook

playbook_file_name = self.playbook._file_name
inventory = self._options.inventory
if ANSIBLE_ABOVE_28:
inventory = self._options['inventory']
else:
inventory = self._options.inventory

self.start_timer()

# Set the playbook name from its filename
self._playbook_name, _ = os.path.splitext(
os.path.basename(playbook_file_name))
if isinstance(inventory, list):
if isinstance(inventory, (list, tuple)):
inventory = ','.join(inventory)
self._inventory_name = ','.join([os.path.basename(os.path.realpath(name)) for name in inventory.split(',') if name])

Expand Down