88try :
99 import datadog
1010 import yaml
11+ from packaging import version
1112 HAS_MODULES = True
1213except ImportError :
1314 HAS_MODULES = False
1415
1516
17+ import ansible
1618from ansible .plugins .callback import CallbackBase
1719from __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+
1926DEFAULT_DD_URL = "https://api.datadoghq.com"
2027
2128class 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