9

I am using jinja2 template to install/upgrade packages.

The logic was setting a variable for current installed version and compare it with the available version. It was working fine but once we passed in to 10.x, comparison quit working.

Is it possible to cast the variable so it can correctly identify 10.9.8 is greater than 9.8.7?

Thanks

current_version=['9.8.7']

{% if current_version < '10.9.8' %}

BBDG
  • 365
  • 4
  • 11
  • I guess I can try to remove the dots and cast them to integer and then compare. How would one go about deleting dots in jinja2? – BBDG Sep 20 '17 at 14:21
  • I ended up using != to compare installed version string to the version I want to have installed on the server. – BBDG Sep 20 '17 at 19:45
  • Possible duplicate of [How to compare version strings in salt sls files](https://stackoverflow.com/questions/45701907/how-to-compare-version-strings-in-salt-sls-files) – ceving Sep 21 '17 at 08:52

5 Answers5

17

There's a special test version_compare:

{% if current_version | version_compare('10.9.8', '<') %}

current_version should be string (it is a list in your example).

MiSHuTka
  • 1,191
  • 1
  • 12
  • 20
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
2

Using plain jinja2, without ansible or other extensions:

{% if my_version.split('.') | map('int') | list >= [10, 9, 8]  %}

By converting each element to int, you ensure it won't compare lexicographically.

Kira
  • 463
  • 4
  • 12
1

With Jinja2

Using split and because of how sequence comparison work, the following should do just fine:

{% if current_version.split('.') | map('int') < '10.9.8'.split('.') | map('int') %}

Test:

Split: {{ current_version.split('.') }}
Split + cast: {{ current_version.split('.') | map('int') }}
---
Is {{ current_version.split('.') | map('int') }} < {{ '10.9.8'.split('.') | map('int') }}?
{% if current_version.split('.') | map('int') < '10.9.8'.split('.') | map('int') %}
Yes
{% endif %}

Which, with current_version: "9.8.7" gives:

Split: ['9', '8', '7']
Split + cast: [9, 8, 7]
---
Is [9, 8, 7] < [10, 9, 8]?
Yes
cglacet
  • 8,873
  • 4
  • 45
  • 60
  • This solution fails the exactly same way as OP's. `['1','2'] < ['1','10'] == False` – Kira Apr 16 '21 at 11:32
  • Ah, good catch, you are right. It missed the `int` cast, now it should work. Let me know if there's another problem. – cglacet Apr 16 '21 at 22:17
0

In saltstack you can use pkg.version_cmp

See my reply here: How to compare version strings in salt sls files

ProT-0-TypE
  • 283
  • 2
  • 9
0

@cglacet answer was useful and worked when generating in Ansible, but it gave me an error when using the Jinja2 library directly in Python:

Exception has occurred: TypeError
'>=' not supported between instances of 'generator' and 'generator'

It seems like it prints the memory location and not actual values. I don't know what voodoo magic Ansible used to make it work, but adding a | list creates an actual list of integers and fixes the issue.

{% if current_version.split('.') | map('int') | list < '10.9.8'.split('.') | map('int') | list %}

Proof:

{% set sw_version = "7.5.2" %}
{{ sw_version.split('.') }}
{{ ['7', '5', '2'] | int }}
{{ sw_version.split('.') | map('int') }}

output

['7', '5', '2']
0
<generator object sync_do_map at 0x1223b6270>

Fix:

{% set sw_version = "7.5.2" %}

{% if sw_version.split('.') | map('int') | list < '10.9.8'.split('.') | map('int') | list %}
Smaller than to 10.9.8
{% endif %}
{% if sw_version.split('.') | map('int') | list >= '7.5.2'.split('.') | map('int') | list %}
Equal to 7.5.2
{% endif %}
{% if sw_version.split('.') | map('int') | list >= '6.7.5'.split('.') | map('int') | list %}
Bigger than 6.7.5
{% endif %}
{% if sw_version.split('.') | map('int') | list < '7.6.1'.split('.') | map('int') | list %}
Smaller than 7.6.1
{% endif %}
{% if sw_version.split('.') | map('int') | list < '7.5.10'.split('.') | map('int') | list %}
Smaller than 7.5.10
{% endif %}
Smaller than to 10.9.8


Equal to 7.5.2


Bigger than 6.7.5


Smaller than 7.6.1


Smaller than 7.5.10
arobbe
  • 1