Search code examples
jinja2salt-project

Block within block in Jinja2


I am trying to get some variable from Pillar and passing a variable as name of key to get from Pillar but something seems to be missing. I tried a few combinations but none seem to work.

In first line - I am getting key - house_name and then in second line I want to dynamically get the appropriate value for that key from Pillar. Part of key is static (homes:list) and part dynamic (house_name)

{% for house_name in event_data.house_list|list %}
{% set home_def = salt['pillar.get']('homes:list:{{ house_name }}') %}
...
{% endfor %}

When I hardcode the house_name in second line - things work fine - which means something in rendering of the key with namespace is wrong. A few combinations which I tried but don't work of second line:

{% set home_def = salt['pillar.get']('homes:list:'{{ house_name }}) %}
{% set home_def = salt['pillar.get']("homes:list:{{ house_name }}") %}

Solution

  • you don't need use {{}} brackets in set statement

    {% for house_name in event_data.house_list|list %}
    {% set home_def = salt['pillar.get']('homes:list:{}'.format(house_name)) %}
    ...
    {% endfor %}