Fix ansible.__version__ comparisons with multi-digit components

Ansible 2.8 is older than Ansible 2.10, but `'2.8' < '2.10' == False`
pull/878/head
Alex Willmer 3 years ago
parent 465ac8abff
commit d9b8d50d4e

@ -31,7 +31,8 @@ Stable names for PluginLoader instances across Ansible versions.
""" """
from __future__ import absolute_import from __future__ import absolute_import
import distutils.version
import ansible_mitogen.utils
__all__ = [ __all__ = [
'action_loader', 'action_loader',
@ -42,7 +43,6 @@ __all__ = [
'strategy_loader', 'strategy_loader',
] ]
import ansible
ANSIBLE_VERSION_MIN = (2, 10) ANSIBLE_VERSION_MIN = (2, 10)
ANSIBLE_VERSION_MAX = (2, 11) 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 Throw AnsibleError with a descriptive message in case of being loaded into
an unsupported Ansible release. an unsupported Ansible release.
""" """
v = ansible.__version__ v = ansible_mitogen.utils.ansible_version
if not isinstance(v, tuple):
v = tuple(distutils.version.LooseVersion(v).version)
if v[:2] < ANSIBLE_VERSION_MIN: if v[:2] < ANSIBLE_VERSION_MIN:
raise ansible.errors.AnsibleError( raise ansible.errors.AnsibleError(
OLD_VERSION_MSG % (v, ANSIBLE_VERSION_MIN) OLD_VERSION_MSG % (v, ANSIBLE_VERSION_MIN)

@ -53,6 +53,8 @@ import mitogen.utils
import ansible_mitogen.connection import ansible_mitogen.connection
import ansible_mitogen.planner import ansible_mitogen.planner
import ansible_mitogen.target import ansible_mitogen.target
import ansible_mitogen.utils
from ansible.module_utils._text import to_text from ansible.module_utils._text import to_text
try: try:
@ -226,7 +228,7 @@ class ActionModuleMixin(ansible.plugins.action.ActionBase):
with a pipelined call to :func:`ansible_mitogen.target.prune_tree`. with a pipelined call to :func:`ansible_mitogen.target.prune_tree`.
""" """
LOG.debug('_remove_tmp_path(%r)', tmp_path) 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 tmp_path = self._connection._shell.tmpdir # 06f73ad578d
if tmp_path is not None: if tmp_path is not None:
self._connection.get_chain().call_no_reply( 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): def _set_temp_file_args(self, module_args, wrap_async):
# Ansible>2.5 module_utils reuses the action's temporary directory if # Ansible>2.5 module_utils reuses the action's temporary directory if
# one exists. Older versions error if this key is present. # 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: if wrap_async:
# Sharing is not possible with async tasks, as in that case, # Sharing is not possible with async tasks, as in that case,
# the directory must outlive the action plug-in. # 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 # If _ansible_tmpdir is unset, Ansible>2.6 module_utils will use
# _ansible_remote_tmp as the location to create the module's temporary # _ansible_remote_tmp as the location to create the module's temporary
# directory. Older versions error if this key is present. # 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'] = ( module_args['_ansible_remote_tmp'] = (
self._connection.get_good_temp_dir() 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 # Built-in actions expected tmpdir to be cleaned up automatically
# on _execute_module(). # on _execute_module().
self._remove_tmp_path(tmp) self._remove_tmp_path(tmp)

@ -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

@ -5,14 +5,22 @@
import ansible import ansible
from ansible.module_utils.basic import AnsibleModule 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 from ansible.module_utils import distro
else: else:
distro = None distro = None
def main(): def main():
module = AnsibleModule(argument_spec={}) module = AnsibleModule(argument_spec={})
if ansible.__version__ > '2.8': if ansible_version[:2] >= (2, 8):
module.exit_json(info=distro.info()) module.exit_json(info=distro.info())
else: else:
module.exit_json(info={'id': None}) module.exit_json(info={'id': None})

Loading…
Cancel
Save