55import datadog
66import yaml
77
8- try :
9- # Ansible v2
10- from ansible .plugins .callback import CallbackBase
11- from __main__ import cli
12- except ImportError :
13- # Ansible v1
14- CallbackBase = object
15- cli = None
8+ from ansible .plugins .callback import CallbackBase
9+ from __main__ import cli
1610
1711
1812class CallbackModule (CallbackBase ):
@@ -24,16 +18,16 @@ def __init__(self):
2418 if cli :
2519 self ._options = cli .options
2620
27- # self.playbook is either set by Ansible (v1), or by us in the `playbook_start ` callback method (v2)
21+ # self.playbook is set in the `v2_playbook_on_start ` callback method
2822 self .playbook = None
29- # self.play is either set by Ansible (v1), or by us in the `playbook_on_play_start` callback method (v2)
23+ # self.play is set in the `playbook_on_play_start` callback method
3024 self .play = None
3125
3226 # Load parameters from conf file
3327 def _load_conf (self , file_path ):
3428 conf_dict = {}
3529
36- if os .path .isfile (file_path ):
30+ if os .path .isfile (file_path ):
3731 with open (file_path , 'r' ) as conf_file :
3832 conf_dict = yaml .load (conf_file )
3933
@@ -114,19 +108,6 @@ def start_timer(self):
114108 def get_elapsed_time (self ):
115109 return time .time () - self ._start_time
116110
117- # Handle `playbook_on_start` callback, common to Ansible v1 & v2
118- def _handle_playbook_on_start (self , playbook_file_name , inventory ):
119- self .start_timer ()
120-
121- # Set the playbook name from its filename
122- self ._playbook_name , _ = os .path .splitext (
123- os .path .basename (playbook_file_name ))
124-
125- # inventory is a comma separated host list: ["host1,host2","host3,host4,"]
126- if isinstance (inventory , basestring ):
127- inventory = inventory .split (',' )
128- self ._inventory_name = ',' .join ([os .path .basename (os .path .realpath (name )) for name in inventory if name ])
129-
130111 # Default tags sent with events and metrics
131112 @property
132113 def default_tags (self ):
@@ -196,13 +177,6 @@ def runner_on_unreachable(self, host, res):
196177 host = host ,
197178 )
198179
199- # Implementation compatible with Ansible v1 only
200- def playbook_on_start (self ):
201- playbook_file_name = self .playbook .filename
202- inventory = self .playbook .inventory .host_list
203-
204- self ._handle_playbook_on_start (playbook_file_name , inventory )
205-
206180 # Implementation compatible with Ansible v2 only
207181 def v2_playbook_on_start (self , playbook ):
208182 # On Ansible v2, Ansible doesn't set `self.playbook` automatically
@@ -211,7 +185,14 @@ def v2_playbook_on_start(self, playbook):
211185 playbook_file_name = self .playbook ._file_name
212186 inventory = self ._options .inventory
213187
214- self ._handle_playbook_on_start (playbook_file_name , inventory )
188+ self .start_timer ()
189+
190+ # Set the playbook name from its filename
191+ self ._playbook_name , _ = os .path .splitext (
192+ os .path .basename (playbook_file_name ))
193+ if isinstance (inventory , list ):
194+ inventory = ',' .join (inventory )
195+ self ._inventory_name = ',' .join ([os .path .basename (os .path .realpath (name )) for name in inventory .split (',' ) if name ])
215196
216197 def v2_playbook_on_play_start (self , play ):
217198 # On Ansible v2, Ansible doesn't set `self.play` automatically
@@ -239,7 +220,8 @@ def v2_playbook_on_play_start(self, play):
239220 datadog .initialize (api_key = api_key , api_host = url )
240221
241222 self .send_playbook_event (
242- 'Ansible playbook "{0}" started by "{1}" against "{2}"' .format (
223+ 'Ansible play "{0}" started in playbook "{1}" by "{2}" against "{3}"' .format (
224+ self .play .name ,
243225 self ._playbook_name ,
244226 getpass .getuser (),
245227 self ._inventory_name ),
0 commit comments