Skip to content

Commit f70c645

Browse files
rsdcobalthush-hush
authored andcommitted
add hook to handle incompatibilities between ansible 2.7 and ansible 2.8 (#47)
Add hook to handle incompatibilities between ansible 2.7 and ansible 2.8
1 parent 3e4caf0 commit f70c645

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The following python libraries are required on the Ansible server:
1010

1111
- [`datadogpy`](https://github.com/DataDog/datadogpy/)
1212
- `pyyaml` (install with `pip install pyyaml`)
13+
- `packaging` (install with `pip install packaging`)
1314

1415
Ansible <=1.9 is no longer supported by this callback. The latest compatible
1516
version is tagged with `1.0.2`.

datadog_callback.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,28 @@
88
try:
99
import datadog
1010
import yaml
11+
from packaging import version
1112
HAS_MODULES = True
1213
except ImportError:
1314
HAS_MODULES = False
1415

1516

17+
import ansible
1618
from ansible.plugins.callback import CallbackBase
1719
from __main__ import cli
1820

21+
ANSIBLE_ABOVE_28 = False
22+
if HAS_MODULES and version.parse(ansible.__version__) >= version.parse('2.8.0'):
23+
ANSIBLE_ABOVE_28 = True
24+
from ansible.context import CLIARGS
25+
1926
DEFAULT_DD_URL = "https://api.datadoghq.com"
2027

2128
class CallbackModule(CallbackBase):
2229
def __init__(self):
2330
if not HAS_MODULES:
2431
self.disabled = True
25-
print('Datadog callback disabled: missing "datadog" and/or "yaml" python package.')
32+
print('Datadog callback disabled: missing "datadog", "yaml", and/or "packaging" python package.')
2633
else:
2734
self.disabled = False
2835
# Set logger level - datadog api and urllib3
@@ -32,8 +39,11 @@ def __init__(self):
3239
self._playbook_name = None
3340
self._start_time = time.time()
3441
self._options = None
35-
if cli:
36-
self._options = cli.options
42+
if HAS_MODULES and cli:
43+
if ANSIBLE_ABOVE_28:
44+
self._options = CLIARGS
45+
else:
46+
self._options = cli.options
3747

3848
# self.playbook is set in the `v2_playbook_on_start` callback method
3949
self.playbook = None
@@ -217,14 +227,17 @@ def v2_playbook_on_start(self, playbook):
217227
self.playbook = playbook
218228

219229
playbook_file_name = self.playbook._file_name
220-
inventory = self._options.inventory
230+
if ANSIBLE_ABOVE_28:
231+
inventory = self._options['inventory']
232+
else:
233+
inventory = self._options.inventory
221234

222235
self.start_timer()
223236

224237
# Set the playbook name from its filename
225238
self._playbook_name, _ = os.path.splitext(
226239
os.path.basename(playbook_file_name))
227-
if isinstance(inventory, list):
240+
if isinstance(inventory, (list, tuple)):
228241
inventory = ','.join(inventory)
229242
self._inventory_name = ','.join([os.path.basename(os.path.realpath(name)) for name in inventory.split(',') if name])
230243

0 commit comments

Comments
 (0)