# How to use version\_compare Jinja filter to only compare major and minor releases?

**URL:** <https://forum.ansible.com/t/how-to-use-version-compare-jinja-filter-to-only-compare-major-and-minor-releases/22880>\
**Category:** Ansible Developer\
**Created:** [October 25, 2016, 12:57pm UTC](https://forum.ansible.com/t/how-to-use-version-compare-jinja-filter-to-only-compare-major-and-minor-releases/22880 "2016-10-25T12:57:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tadej\_Janez](https://avatars.discourse-cdn.com/v4/letter/t/f475e1/32.png) [@Tadej\_Janez](https://forum.ansible.com/u/Tadej_Janez)\
**Post date:** [October 25, 2016, 12:57pm UTC](https://forum.ansible.com/t/how-to-use-version-compare-jinja-filter-to-only-compare-major-and-minor-releases/22880/1 "2016-10-25T12:57:57Z")

</div>

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=&quot;\{\{ ansible\_ver | version\_compare\(&#39;2\.1&#39;, &#39;==&#39;,

```

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](https://www.python.org/dev/peps/pep-0440/#compatible-release)

---

<div class="post-metadata">

**Author:** ![willthames](https://avatars.discourse-cdn.com/v4/letter/w/8dc957/32.png) [@willthames](https://forum.ansible.com/u/willthames)\
**Post date:** [October 25, 2016, 1:50pm UTC](https://forum.ansible.com/t/how-to-use-version-compare-jinja-filter-to-only-compare-major-and-minor-releases/22880/2 "2016-10-25T13:50:35Z")

</div>

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’, ‘\<’)

---

<div class="post-metadata">

**Author:** ![willthames](https://avatars.discourse-cdn.com/v4/letter/w/8dc957/32.png) [@willthames](https://forum.ansible.com/u/willthames)\
**Post date:** [October 25, 2016, 1:52pm UTC](https://forum.ansible.com/t/how-to-use-version-compare-jinja-filter-to-only-compare-major-and-minor-releases/22880/3 "2016-10-25T13:52:26Z")

</div>

Note also that “~=” isn’t a valid operator for version\_compare

[https://docs.ansible.com/ansible/playbooks\_tests.html#version-comparison](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)

---

<div class="post-metadata">

**Author:** ![Tadej\_Janez](https://avatars.discourse-cdn.com/v4/letter/t/f475e1/32.png) [@Tadej\_Janez](https://forum.ansible.com/u/Tadej_Janez)\
**Post date:** [October 25, 2016, 3:11pm UTC](https://forum.ansible.com/t/how-to-use-version-compare-jinja-filter-to-only-compare-major-and-minor-releases/22880/4 "2016-10-25T15:11:55Z")

</div>

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ž
