How to use version_compare Jinja filter to only compare major and minor releases?

Hi!

I was a bit puzzled when trying to use version_compare Jinja filter to
check that the major and minor Ansible versions are equal to some
desired value.

Here is an example playbook:

- hosts: localhost
connection: local
gather_facts: false

vars:
- ansible_ver: "2.1.2"

tasks:
- name: Check that Ansible major and minor versions are the same
debug: msg="{{ ansible_ver | version_compare('2.1', '==') }}"
# returns 'false' but I would expect it to return 'true'

\- name: Check that Ansible major and minor versions are the same
  debug: msg="\{\{ ansible\_ver | version\_compare\('2\.1', '==',

strict=True) }}"
# returns 'false' as expected

(Note: In real playbooks, one would use ansible_version.full in place
of ansible_ver.)

Is this a bug or intended behavior?

If the second, how would one do comparisons that would only compare the
major and minor version, ignoring the patch level? Or achieve something
like Python's compatible release operator (~=) [1]?

Thanks and regards,
Tadej Janež

[1] https://www.python.org/dev/peps/pep-0440/#compatible-release

I would not expect “2.1.2”|version_compare(‘2.1’, ‘==’) to return true as they are not equal.

For what you want, you could use

ansible_ver | version_compare(‘2.1’, ‘>=’) and ansible_ver | version_compare(‘2.2’, ‘<’)

Note also that “~=” isn’t a valid operator for version_compare

https://docs.ansible.com/ansible/playbooks_tests.html#version-comparison

Also, I don’t think strict does what you think it means (it’s more likely to allow
non PEP-compliant versions to be compared, akin to distutils LooseVersion
vs StrictVersion)

Will,

I would not expect "2.1.2"|version_compare('2.1', '==') to return
true as they are not equal.

ok, agreed.

However, what about:

"2.1.0"|version_compare('2.1', '==')

I would expect that to return true, but it currently evaluates to
false.

It gets even more interesting. Setting 'strict=True':

"2.1.0"|version_compare('2.1', '==', strict=True)

returns true. Intuitively, one would think that using 'stric=True'
would return false since 2.1.0 and 2.1 are not strictly the same
version.

Is this really the desired behaviour?

FWIW, I'm using Ansible 2.2.0 RC2.

For what you want, you could use

ansible_ver | version_compare('2.1', '>=') and ansible_ver |
version_compare('2.2', '<')

Thanks for the tip!

Regards,
Tadej Janež