Skip to content

Commit d9b8d50

Browse files
committed
Fix ansible.__version__ comparisons with multi-digit components
Ansible 2.8 is older than Ansible 2.10, but `'2.8' < '2.10' == False`
1 parent 465ac8a commit d9b8d50

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

ansible_mitogen/loaders.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"""
3232

3333
from __future__ import absolute_import
34-
import distutils.version
34+
35+
import ansible_mitogen.utils
3536

3637
__all__ = [
3738
'action_loader',
@@ -42,7 +43,6 @@
4243
'strategy_loader',
4344
]
4445

45-
import ansible
4646

4747
ANSIBLE_VERSION_MIN = (2, 10)
4848
ANSIBLE_VERSION_MAX = (2, 11)
@@ -68,10 +68,7 @@ def assert_supported_release():
6868
Throw AnsibleError with a descriptive message in case of being loaded into
6969
an unsupported Ansible release.
7070
"""
71-
v = ansible.__version__
72-
if not isinstance(v, tuple):
73-
v = tuple(distutils.version.LooseVersion(v).version)
74-
71+
v = ansible_mitogen.utils.ansible_version
7572
if v[:2] < ANSIBLE_VERSION_MIN:
7673
raise ansible.errors.AnsibleError(
7774
OLD_VERSION_MSG % (v, ANSIBLE_VERSION_MIN)

ansible_mitogen/mixins.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import ansible_mitogen.connection
5454
import ansible_mitogen.planner
5555
import ansible_mitogen.target
56+
import ansible_mitogen.utils
57+
5658
from ansible.module_utils._text import to_text
5759

5860
try:
@@ -226,7 +228,7 @@ def _remove_tmp_path(self, tmp_path):
226228
with a pipelined call to :func:`ansible_mitogen.target.prune_tree`.
227229
"""
228230
LOG.debug('_remove_tmp_path(%r)', tmp_path)
229-
if tmp_path is None and ansible.__version__ > '2.6':
231+
if tmp_path is None and ansible_mitogen.utils.ansible_version[:2] >= (2, 6):
230232
tmp_path = self._connection._shell.tmpdir # 06f73ad578d
231233
if tmp_path is not None:
232234
self._connection.get_chain().call_no_reply(
@@ -335,7 +337,7 @@ def get_task_timeout_secs(self):
335337
def _set_temp_file_args(self, module_args, wrap_async):
336338
# Ansible>2.5 module_utils reuses the action's temporary directory if
337339
# one exists. Older versions error if this key is present.
338-
if ansible.__version__ > '2.5':
340+
if ansible_mitogen.utils.ansible_version[:2] >= (2, 5):
339341
if wrap_async:
340342
# Sharing is not possible with async tasks, as in that case,
341343
# the directory must outlive the action plug-in.
@@ -346,7 +348,7 @@ def _set_temp_file_args(self, module_args, wrap_async):
346348
# If _ansible_tmpdir is unset, Ansible>2.6 module_utils will use
347349
# _ansible_remote_tmp as the location to create the module's temporary
348350
# directory. Older versions error if this key is present.
349-
if ansible.__version__ > '2.6':
351+
if ansible_mitogen.utils.ansible_version[:2] >= (2, 6):
350352
module_args['_ansible_remote_tmp'] = (
351353
self._connection.get_good_temp_dir()
352354
)
@@ -393,7 +395,7 @@ def _execute_module(self, module_name=None, module_args=None, tmp=None,
393395
)
394396
)
395397

396-
if tmp and ansible.__version__ < '2.5' and delete_remote_tmp:
398+
if tmp and delete_remote_tmp and ansible_mitogen.utils.ansible_version[:2] < (2, 5):
397399
# Built-in actions expected tmpdir to be cleaned up automatically
398400
# on _execute_module().
399401
self._remove_tmp_path(tmp)

ansible_mitogen/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from __future__ import absolute_import
2+
3+
import distutils.version
4+
5+
import ansible
6+
7+
__all__ = [
8+
'ansible_version',
9+
]
10+
11+
ansible_version = tuple(distutils.version.LooseVersion(ansible.__version__).version)
12+
del distutils
13+
del ansible

tests/ansible/lib/modules/custom_python_uses_distro.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@
55
import ansible
66
from ansible.module_utils.basic import AnsibleModule
77

8-
if ansible.__version__ > '2.8':
8+
def try_int(s):
9+
try:
10+
return int(s, 10)
11+
except ValueError:
12+
return s
13+
14+
ansible_version = tuple(try_int(s) for s in ansible.__version__.split('.'))
15+
16+
if ansible_version[:2] >= (2, 8):
917
from ansible.module_utils import distro
1018
else:
1119
distro = None
1220

1321
def main():
1422
module = AnsibleModule(argument_spec={})
15-
if ansible.__version__ > '2.8':
23+
if ansible_version[:2] >= (2, 8):
1624
module.exit_json(info=distro.info())
1725
else:
1826
module.exit_json(info={'id': None})

0 commit comments

Comments
 (0)