Setting conditional variables in map.jinja

314 views
Skip to first unread message

HRX

unread,
Mar 15, 2017, 12:26:30 PM3/15/17
to Salt-users
Hi,
Saltstack newbie, at my wit's end.

Trying to set variables in map.jinja based on values received from pillar data:

map.jinja:

{% set vars = {
  {% if salt['pillar.get']('ec2:tags:application') == 'jboss' %}
  'admingroup': 'jbossadmins'
  {% end if %}
}
%}

Then call this from a state file

...
{% set admin = vars.admingroup %}

And I will use {{ admin }} where I need to.

This produces an error with:

Rendering SLS failed:  Jinja syntax error: unexpected '%'

 {% if salt['pillar.get']('ec2:tags:application') == 'jboss' %}  <======================


I could not figure out what is wrong. 
What is the recommended approach for setting variables based on values obtained from pillars?

Thank you.


HRX

unread,
Mar 15, 2017, 12:58:08 PM3/15/17
to Salt-users
I figured out the issue, I was using "--local" to test the setup locally but my pillar was not local.
So I created a local copy for pillar and now this works.
But for the best practice question, I was told I can not call pillar.get from inside a pillar file, where should we setup variables based on values obtained from pillars?

Thanks

Seth House

unread,
Mar 15, 2017, 1:01:12 PM3/15/17
to salt users list
You don't need Jinja tags when you're already inside of a tag. And
once you're inside a tag, Jinja semantics are roughly equivalent to
Python. Here are two possible approaches:

{# option 1 #}
{% set vars = {
'admingroup': 'jbossadmins'
if salt['pillar.get']('ec2:tags:application') == 'jboss'
else 'defaultvalue',
} %}

{# option 2 #}
{% set vars = {} %}
{% if salt['pillar.get']('ec2:tags:application') == 'jboss' %}
{% do vars.update({'admingroup': 'jbossadmins'}) %}
{% endif %}
> --
> You received this message because you are subscribed to the Google Groups
> "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to salt-users+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/salt-users/2d1eda01-c0bf-4735-b896-014cb499bcc9%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Seth House

unread,
Mar 15, 2017, 1:04:02 PM3/15/17
to salt users list
On Wed, Mar 15, 2017 at 10:58 AM, HRX <huun...@gmail.com> wrote:
> But for the best practice question, I was told I can not call pillar.get
> from inside a pillar file, where should we setup variables based on values
> obtained from pillars?

Is the map file you're working on used in States or in Pillar? If it's
used in States then what you're doing is fine and even common.

It is true you cannot reference other Pillar values from within a
Pillar SLS file. Using Jinja imports is a quick way around that. See
the discussion below for examples of that.

https://groups.google.com/d/topic/salt-users/g_ox6gZb-q8/discussion

HRX

unread,
Mar 15, 2017, 1:28:33 PM3/15/17
to Salt-users, se...@eseth.com
Seth, thanks a lot! I used second option and it works very well as I am setting additional variables per pillar match.
Reply all
Reply to author
Forward
0 new messages