diff --git a/ansible_mitogen/loaders.py b/ansible_mitogen/loaders.py index b4b98962..85b6c0e5 100644 --- a/ansible_mitogen/loaders.py +++ b/ansible_mitogen/loaders.py @@ -31,7 +31,8 @@ Stable names for PluginLoader instances across Ansible versions. """ from __future__ import absolute_import -import distutils.version + +import ansible_mitogen.utils __all__ = [ 'action_loader', @@ -42,7 +43,6 @@ __all__ = [ 'strategy_loader', ] -import ansible ANSIBLE_VERSION_MIN = (2, 10) ANSIBLE_VERSION_MAX = (2, 11) @@ -68,10 +68,7 @@ def assert_supported_release(): Throw AnsibleError with a descriptive message in case of being loaded into an unsupported Ansible release. """ - v = ansible.__version__ - if not isinstance(v, tuple): - v = tuple(distutils.version.LooseVersion(v).version) - + v = ansible_mitogen.utils.ansible_version if v[:2] < ANSIBLE_VERSION_MIN: raise ansible.errors.AnsibleError( OLD_VERSION_MSG % (v, ANSIBLE_VERSION_MIN) diff --git a/ansible_mitogen/mixins.py b/ansible_mitogen/mixins.py index 7e7a3ff0..c4d793e8 100644 --- a/ansible_mitogen/mixins.py +++ b/ansible_mitogen/mixins.py @@ -53,6 +53,8 @@ import mitogen.utils import ansible_mitogen.connection import ansible_mitogen.planner import ansible_mitogen.target +import ansible_mitogen.utils + from ansible.module_utils._text import to_text try: @@ -226,7 +228,7 @@ class ActionModuleMixin(ansible.plugins.action.ActionBase): with a pipelined call to :func:`ansible_mitogen.target.prune_tree`. """ LOG.debug('_remove_tmp_path(%r)', tmp_path) - if tmp_path is None and ansible.__version__ > '2.6': + if tmp_path is None and ansible_mitogen.utils.ansible_version[:2] >= (2, 6): tmp_path = self._connection._shell.tmpdir # 06f73ad578d if tmp_path is not None: self._connection.get_chain().call_no_reply( @@ -335,7 +337,7 @@ class ActionModuleMixin(ansible.plugins.action.ActionBase): def _set_temp_file_args(self, module_args, wrap_async): # Ansible>2.5 module_utils reuses the action's temporary directory if # one exists. Older versions error if this key is present. - if ansible.__version__ > '2.5': + if ansible_mitogen.utils.ansible_version[:2] >= (2, 5): if wrap_async: # Sharing is not possible with async tasks, as in that case, # the directory must outlive the action plug-in. @@ -346,7 +348,7 @@ class ActionModuleMixin(ansible.plugins.action.ActionBase): # If _ansible_tmpdir is unset, Ansible>2.6 module_utils will use # _ansible_remote_tmp as the location to create the module's temporary # directory. Older versions error if this key is present. - if ansible.__version__ > '2.6': + if ansible_mitogen.utils.ansible_version[:2] >= (2, 6): module_args['_ansible_remote_tmp'] = ( self._connection.get_good_temp_dir() ) @@ -393,7 +395,7 @@ class ActionModuleMixin(ansible.plugins.action.ActionBase): ) ) - if tmp and ansible.__version__ < '2.5' and delete_remote_tmp: + if tmp and delete_remote_tmp and ansible_mitogen.utils.ansible_version[:2] < (2, 5): # Built-in actions expected tmpdir to be cleaned up automatically # on _execute_module(). self._remove_tmp_path(tmp) diff --git a/ansible_mitogen/utils.py b/ansible_mitogen/utils.py new file mode 100644 index 00000000..399c181a --- /dev/null +++ b/ansible_mitogen/utils.py @@ -0,0 +1,13 @@ +from __future__ import absolute_import + +import distutils.version + +import ansible + +__all__ = [ + 'ansible_version', +] + +ansible_version = tuple(distutils.version.LooseVersion(ansible.__version__).version) +del distutils +del ansible diff --git a/tests/ansible/lib/modules/custom_python_uses_distro.py b/tests/ansible/lib/modules/custom_python_uses_distro.py index 1fc31b4e..6b3a356b 100644 --- a/tests/ansible/lib/modules/custom_python_uses_distro.py +++ b/tests/ansible/lib/modules/custom_python_uses_distro.py @@ -5,14 +5,22 @@ import ansible from ansible.module_utils.basic import AnsibleModule -if ansible.__version__ > '2.8': +def try_int(s): + try: + return int(s, 10) + except ValueError: + return s + +ansible_version = tuple(try_int(s) for s in ansible.__version__.split('.')) + +if ansible_version[:2] >= (2, 8): from ansible.module_utils import distro else: distro = None def main(): module = AnsibleModule(argument_spec={}) - if ansible.__version__ > '2.8': + if ansible_version[:2] >= (2, 8): module.exit_json(info=distro.info()) else: module.exit_json(info={'id': None})