0

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 }}") %}

1 Answer 1

1

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 %}
5
  • That worked like a charm, thanks a ton. I realise that I am missing some basic syntax documentation - any pointers? Commented Dec 2, 2015 at 16:08
  • 1
    In Jinja2 the double curly braces are used as a print statement. If you access variables inside tags don’t put the braces around them. See variables section in documentation: jinja.pocoo.org/docs/dev/templates/#variables
    – r-m-n
    Commented Dec 2, 2015 at 16:28
  • how would it work if there are two dynamic values something like homes:list:{}:{} where the two parentheses is where I need to put my two variables Commented Dec 23, 2015 at 10:52
  • 'homes:list:{}:{}'.format(var1, var2). You can read more about format in python documentation: docs.python.org/2/library/string.html#format-examples
    – r-m-n
    Commented Dec 23, 2015 at 11:03
  • thanks a ton, that link is super helpful. Looks like time to wear some Python hats :) Commented Dec 25, 2015 at 6:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.