8
8
try :
9
9
import datadog
10
10
import yaml
11
+ from packaging import version
11
12
HAS_MODULES = True
12
13
except ImportError :
13
14
HAS_MODULES = False
14
15
15
16
17
+ import ansible
16
18
from ansible .plugins .callback import CallbackBase
17
19
from __main__ import cli
18
20
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
+
19
26
DEFAULT_DD_URL = "https://api.datadoghq.com"
20
27
21
28
class CallbackModule (CallbackBase ):
22
29
def __init__ (self ):
23
30
if not HAS_MODULES :
24
31
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.' )
26
33
else :
27
34
self .disabled = False
28
35
# Set logger level - datadog api and urllib3
@@ -32,8 +39,11 @@ def __init__(self):
32
39
self ._playbook_name = None
33
40
self ._start_time = time .time ()
34
41
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
37
47
38
48
# self.playbook is set in the `v2_playbook_on_start` callback method
39
49
self .playbook = None
@@ -217,14 +227,17 @@ def v2_playbook_on_start(self, playbook):
217
227
self .playbook = playbook
218
228
219
229
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
221
234
222
235
self .start_timer ()
223
236
224
237
# Set the playbook name from its filename
225
238
self ._playbook_name , _ = os .path .splitext (
226
239
os .path .basename (playbook_file_name ))
227
- if isinstance (inventory , list ):
240
+ if isinstance (inventory , ( list , tuple ) ):
228
241
inventory = ',' .join (inventory )
229
242
self ._inventory_name = ',' .join ([os .path .basename (os .path .realpath (name )) for name in inventory .split (',' ) if name ])
230
243
0 commit comments