version_compare¶
Collection Note
This module is part of the ansible.builtin collection. To install the collection, use:
Added in version1.6.
Synopsis¶
- Compare version strings using various versioning schemes
Parameters¶
| Parameter | Defaults / Choices | Comments |
|---|---|---|
| _input string required |
Left hand version to compare | |
| operator string |
Default: eq Choices: ==, =, eq, <, lt, <=, le, >, gt, >=, ge, !=, <>, ne |
Comparison operator |
| strict boolean |
Whether to use strict version scheme. Mutually exclusive with O(version_type) | |
| version string required |
Right hand version to compare | |
| version_type string |
Default: loose Choices: loose, strict, semver, semantic, pep440 |
Version scheme to use for comparison. Mutually exclusive with O(strict). See C(notes) for descriptions on the version types. |
Notes¶
Note
- V(loose) - This type corresponds to the Python C(distutils.version.LooseVersion) class. All version formats are valid for this type. The rules for comparison are simple and predictable, but may not always give expected results.
- V(strict) - This type corresponds to the Python C(distutils.version.StrictVersion) class. A version number consists of two or three dot-separated numeric components, with an optional "pre-release" tag on the end. The pre-release tag consists of a single letter C(a) or C(b) followed by a number. If the numeric components of two version numbers are equal, then one with a pre-release tag will always be deemed earlier (lesser) than one without.
- V(semver)/V(semantic) - This type implements the L(Semantic Version,https://semver.org) scheme for version comparison.
- V(pep440) - This type implements the Python L(PEP-440,https://peps.python.org/pep-0440/) versioning rules for version comparison. Added in version 2.14.
Examples¶
- name: version test examples
assert:
that:
- "'1.0' is version_compare('1.0', '==')" # old name
- "'1.0' is version('1.0', '==')"
- "'1.0' is version('2.0', '!=')"
- "'1.0' is version('2.0', '<')"
- "'2.0' is version('1.0', '>')"
- "'1.0' is version('1.0', '<=')"
- "'1.0' is version('1.0', '>=')"
- "'1.0' is version_compare('1.0', '==', strict=true)" # old name
- "'1.0' is version('1.0', '==', strict=true)"
- "'1.0' is version('2.0', '!=', strict=true)"
- "'1.0' is version('2.0', '<', strict=true)"
- "'2.0' is version('1.0', '>', strict=true)"
- "'1.0' is version('1.0', '<=', strict=true)"
- "'1.0' is version('1.0', '>=', strict=true)"
- "'1.2.3' is version('2.0.0', 'lt', version_type='semver')"
- "'2.14.0rc1' is version('2.14.0', 'lt', version_type='pep440')"
Return Values¶
| Key | Data Type | Description | Returned |
|---|---|---|---|
| _value | boolean | Returns V(True) or V(False) depending on the outcome of the comparison. |
Authors¶
- Ansible Core