From cc594080f6e8e10a7715796f4cddb9613e27c2cb Mon Sep 17 00:00:00 2001 From: Rachel Domagalski Date: Fri, 24 May 2019 11:44:26 -0700 Subject: [PATCH 1/3] add hook to handle incompatibilities between ansible 2.7 and ansible 2.8 --- datadog_callback.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/datadog_callback.py b/datadog_callback.py index 4220084..7590c7e 100644 --- a/datadog_callback.py +++ b/datadog_callback.py @@ -4,6 +4,7 @@ import logging import os import time +from packaging import version try: import datadog @@ -13,9 +14,15 @@ HAS_MODULES = False +import ansible from ansible.plugins.callback import CallbackBase from __main__ import cli +ANSIBLE_NEW = False +if version.parse(ansible.__version__) >= version.parse('2.8.0'): + ANSIBLE_NEW = True + from ansible.context import CLIARGS + DEFAULT_DD_URL = "https://api.datadoghq.com" class CallbackModule(CallbackBase): @@ -33,7 +40,10 @@ def __init__(self): self._start_time = time.time() self._options = None if cli: - self._options = cli.options + if ANSIBLE_NEW: + self._options = CLIARGS + else: + self._options = cli.options # self.playbook is set in the `v2_playbook_on_start` callback method self.playbook = None @@ -217,14 +227,17 @@ def v2_playbook_on_start(self, playbook): self.playbook = playbook playbook_file_name = self.playbook._file_name - inventory = self._options.inventory + if ANSIBLE_NEW: + inventory = self._options['inventory'] + else: + inventory = self._options.inventory self.start_timer() # Set the playbook name from its filename self._playbook_name, _ = os.path.splitext( os.path.basename(playbook_file_name)) - if isinstance(inventory, list): + if isinstance(inventory, list) or isinstance(inventory, tuple): inventory = ','.join(inventory) self._inventory_name = ','.join([os.path.basename(os.path.realpath(name)) for name in inventory.split(',') if name]) From bc9755b54aa1a0df73f1e55b882a8e053a63b739 Mon Sep 17 00:00:00 2001 From: Rachel Domagalski Date: Tue, 28 May 2019 07:19:49 -0700 Subject: [PATCH 2/3] add packaging requirement to README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0eb1f6f..a3d0677 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ The following python libraries are required on the Ansible server: - [`datadogpy`](https://github.com/DataDog/datadogpy/) - `pyyaml` (install with `pip install pyyaml`) +- `packaging` (install with `pip install packaging`) Ansible <=1.9 is no longer supported by this callback. The latest compatible version is tagged with `1.0.2`. From 6ec67858a8db178a7e4a6949c5c005ea0283dc98 Mon Sep 17 00:00:00 2001 From: Rachel Domagalski Date: Wed, 29 May 2019 13:46:18 -0700 Subject: [PATCH 3/3] minor refactor --- datadog_callback.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/datadog_callback.py b/datadog_callback.py index 7590c7e..2a6703d 100644 --- a/datadog_callback.py +++ b/datadog_callback.py @@ -4,11 +4,11 @@ import logging import os import time -from packaging import version try: import datadog import yaml + from packaging import version HAS_MODULES = True except ImportError: HAS_MODULES = False @@ -18,9 +18,9 @@ from ansible.plugins.callback import CallbackBase from __main__ import cli -ANSIBLE_NEW = False -if version.parse(ansible.__version__) >= version.parse('2.8.0'): - ANSIBLE_NEW = True +ANSIBLE_ABOVE_28 = False +if HAS_MODULES and version.parse(ansible.__version__) >= version.parse('2.8.0'): + ANSIBLE_ABOVE_28 = True from ansible.context import CLIARGS DEFAULT_DD_URL = "https://api.datadoghq.com" @@ -29,7 +29,7 @@ class CallbackModule(CallbackBase): def __init__(self): if not HAS_MODULES: self.disabled = True - print('Datadog callback disabled: missing "datadog" and/or "yaml" python package.') + print('Datadog callback disabled: missing "datadog", "yaml", and/or "packaging" python package.') else: self.disabled = False # Set logger level - datadog api and urllib3 @@ -39,8 +39,8 @@ def __init__(self): self._playbook_name = None self._start_time = time.time() self._options = None - if cli: - if ANSIBLE_NEW: + if HAS_MODULES and cli: + if ANSIBLE_ABOVE_28: self._options = CLIARGS else: self._options = cli.options @@ -227,7 +227,7 @@ def v2_playbook_on_start(self, playbook): self.playbook = playbook playbook_file_name = self.playbook._file_name - if ANSIBLE_NEW: + if ANSIBLE_ABOVE_28: inventory = self._options['inventory'] else: inventory = self._options.inventory @@ -237,7 +237,7 @@ def v2_playbook_on_start(self, playbook): # Set the playbook name from its filename self._playbook_name, _ = os.path.splitext( os.path.basename(playbook_file_name)) - if isinstance(inventory, list) or isinstance(inventory, tuple): + if isinstance(inventory, (list, tuple)): inventory = ','.join(inventory) self._inventory_name = ','.join([os.path.basename(os.path.realpath(name)) for name in inventory.split(',') if name])