Skip to content

Commit a7c4f10

Browse files
committed
Drop anbsible <2.0 support since it's in EOL
1 parent d58407d commit a7c4f10

File tree

3 files changed

+20
-34
lines changed

3 files changed

+20
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CHANGELOG
33

44
# 2.0.0 / 2017-12-12
55
- [FEATURE] Add support for getting api_key from hostvars and thus from vault. See [#25][]
6+
- [BREAKING CHANGE] Drop support for ansible <2.0
67

78
# 1.0.2 / 2017-12-11
89
* [BUGFIX] Avoid failure when using ansible 2.4. See [#27][]

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ A callback to send Ansible events and metrics to Datadog.
44

55
## Requirements
66

7-
Ansible >=1.1
7+
Ansible >=2.0
88

99
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`)
1313

14+
Ansible <=1.9 is no longer supported by this callback. The latest compatible
15+
version is tagged with `1.0.2`.
16+
1417
## Installation
1518

1619
Once the required libraries (see above) have been installed on the server:

datadog_callback.py

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@
55
import datadog
66
import 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

1812
class 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

Comments
 (0)