'saltstack state file: how to access a list element in a salt grain via Jinja

I try to evaluate the value of a grains list in jinja but do not know how. The list entry I am looking for is the minor osversion in

grain:osrelease_info

salt-call -g |grep -C2 osrelease_info
    osrelease:
        15.99
    osrelease_info:
        - 15
        - 99

In a state-file I would like to evaluate the minior osrelease value in a jinja expression like this one:

{% if grains['osmajorrelease'] == '15' and grains['osrelease_info'][1] >= 99 %}
  ... 
{% endif %}

However the syntax I tried above to access index:1 of the osrelease_info list does not report an error but doesn't work either. So how can I access the list entry containing value "99" in Jinja?



Solution 1:[1]

I think I found the answer myself alltough it is a little strange. The problem was not the expression

{% ... and grains['osrelease_info'][1] >= 99 %}

but instead the string comparison before:

{% if grains['osmajorrelease'] == '15' ... %}

All I changed was to convert the string '15' to an int. So the following expression does what I originally wanted:

{% if grains['osmajorrelease'] == 15 and grains['osrelease_info'][1] >= 99 %}
  ... 
{% endif %}

This is weird since I use exactly the same comparision (grains['osmajorrelease'] == '15') in other state files with success.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rainer042