Jinja variable question

2,101 views
Skip to first unread message

Jon Langemak

unread,
Apr 16, 2015, 11:07:06 AM4/16/15
to salt-...@googlegroups.com
Im sure this is obvious, but I cant figure out what Im doing wrong.  I have a for loop in a template config file that looks like this...

{% set machinelist = "empty" -%}
{% for minion in salt['pillar.get']('kube_minions') -%}
  {% set machinelist = machinelist + minion -%}
{% endfor %}
{{machinelist}}

The output of this is...

empty

When I move the variable inside the for loop, I get all of the data that I want...

{% set machinelist = "empty" -%}
{% for minion in salt['pillar.get']('kube_minions') -%}
  {% set machinelist = machinelist + minion -%}
  {{machinelist}}
{% endfor %}

emptykubminion2
emptykubminion2kubminion3
emptykubminion2kubminion3kubmasta
emptykubminion2kubminion3kubmastakubminion1
emptykubminion2kubminion3kubmastakubminion1kubminion4

I found this -> http://stackoverflow.com/questions/17925674/jinja2-local-global-variable which seems to be my exact issue but I dont know what a 'view function' is.

Any thoughts?  Seems like this is a local/global variable issue but I don't understand how a variable defined outside the for loops can be used in the loop but then become inaccessible outside the loop.

Thanks!

Loren Gordon

unread,
Apr 16, 2015, 11:19:30 AM4/16/15
to salt-...@googlegroups.com
Yes, there's a locality of reference issue with jinja and for loops. You're actually setting a new variable inside the for loop, not updating the variable set outside the for loop.

Try this:

{% set machinelist = [] -%}

{% for minion in salt['pillar.get']('kube_minions') -%}

   
{% do machinelist.append(minnion) -%}
   
{{machinelist}}
{% endfor %}


-Loren

Loren Gordon

unread,
Apr 16, 2015, 11:21:23 AM4/16/15
to salt-...@googlegroups.com
Bother.

What I meant to send:

{% set machinelist = [] -%}
{% for minion in salt['pillar.get']('kube_minions') -%}

   
{% do machinelist.append(minion) -%}
{% endfor %}
{{machinelist}}

-Loren

Jon Langemak

unread,
Apr 16, 2015, 1:07:16 PM4/16/15
to salt-...@googlegroups.com
Thanks Loren!

When I use that code, I get this output...

['kubminion2', 'kubminion3', 'kubmasta', 'kubminion1', 'kubminion4']

Is there a way to do this without having this in an array like format with the single quotes around each item?

Martin Höfling

unread,
Apr 16, 2015, 2:57:45 PM4/16/15
to salt-...@googlegroups.com
If you want somthing like

'kubminion2', 'kubminion3', 'kubmasta', 'kubminion1', 'kubminion4'


you can use the join filter

{{ machines |/join/(', ') }}

Best

Martin

Jon Langemak

unread,
Apr 16, 2015, 3:20:46 PM4/16/15
to salt-...@googlegroups.com
Hi Martin - How would that fit into the loop that I have?  I want to append values from the pillar and then do a IF loop to not add certain values.  

Loren Gordon

unread,
Apr 16, 2015, 3:56:16 PM4/16/15
to salt-...@googlegroups.com
There are probably many ways of getting there...if you happen to know in advance which machines you want to exclude, an inline if statement is pretty straightforward:

{% set excludelist = ['kubmasta', 'kubminion1'] -%}

{% set machinelist = [] -%}
{% for minion in salt['pillar.get']('kube_minions', {}) -%}
   
{% do machinelist.append(minion) if minion not in excludelist -%}
{% endfor %}
{{ machinelist | join(',') }}

-Loren

Jon Langemak

unread,
Apr 16, 2015, 8:27:09 PM4/16/15
to salt-...@googlegroups.com
Thank you so much!  Just starting out with SaltStack and Jinja and these examples are hugely helpful!

Thanks!
Reply all
Reply to author
Forward
0 new messages