Search code examples
jinja2salt-project

Checking grains out of list with saltstack


I want to check if a specific version of a program is already installed. Therefore, I got a state file:

{% set rvs = ['1113','1278'] %}

{% for rv in rvs %}

{% if ('r{{ rv }}' not in grains.get('cat12', [])) %}

... install it ...

{% else %}

... do nothing ...

{% endif %}

{% endfor %}

In my grains I have:

cat12:
  - r1113

I would expect that '1278' is installed and list item '1113' triggers nothing, but even that is installed again...


Solution

  • There's no such syntax as using {{ and }} inside {% and %}. What's inside {% and %} is already Jinja. Just concatenate the string literal and the string variable with the ~ operator.

    {% if 'r' ~ rv not in grains.get('cat12', []) %}
    

    Or you can use the format filter:

    {% if 'r%s'|format(rv) not in grains.get('cat12', []) %}