Test if a grain is set

4,835 views
Skip to first unread message

Sebastien Wains

unread,
Oct 23, 2013, 2:42:38 PM10/23/13
to salt-...@googlegroups.com
Hi,

I figured how in Python I can test for variable existence with something like this :

try:
  x
except NameError:
  print "x is not set

(at least, that's what is said to be the most elegant way on stackoverflow).

Should I go with this in a state or there's a oneliner alternative ?

Thanks

Colton Myers

unread,
Oct 23, 2013, 2:51:46 PM10/23/13
to salt-...@googlegroups.com
An easy way for grains is to use the salt `grains.get` function.  It allows you to fetch a grain if it's there, or provide a default if it's not.  So something like `{{ salt['grains.get']('mygrain', None) }}` would return the contents of the grain if it existed, or None if it didn't.  You could then use a conditional to check for the None value.

--
Colton Myers


--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Sebastien Wains

unread,
Oct 23, 2013, 4:21:15 PM10/23/13
to salt-...@googlegroups.com
Always resourceful Colton :-)

Thanks !

Colton Myers

unread,
Oct 23, 2013, 6:27:18 PM10/23/13
to salt-...@googlegroups.com
No problem!  Glad I could help.

--
Colton Myers

Mrten

unread,
Oct 23, 2013, 7:27:46 PM10/23/13
to salt-...@googlegroups.com
On 23/10/2013 20:51 , Colton Myers wrote:
> An easy way for grains is to use the salt `grains.get` function. It
> allows you to fetch a grain if it's there, or provide a default if it's
> not. So something like `{{ salt['grains.get']('mygrain', None) }}`
> would return the contents of the grain if it existed, or None if it
> didn't. You could then use a conditional to check for the None value.

Or do

{{ grains['mygrain'] | default (None) }}
or
{{ grains.mygrain | default (None) }}

as long as you're inside jinja.

The latter one will confuse you if you go back to python or use mako :)

M.


Colton Myers

unread,
Oct 24, 2013, 11:29:17 AM10/24/13
to salt-...@googlegroups.com
Wow, I totally didn't know about that pipe syntax, or that you could access the grains that easily (second example).  I learned something new today!  =)

--
Colton Myers


Sebastien Wains

unread,
Oct 24, 2013, 3:01:48 PM10/24/13
to salt-...@googlegroups.com
I actually prefer that way :-) 

It makes more sense to me, and easier on the eye.

Thanks to both of you ;-)

Sebastien Wains

unread,
Oct 25, 2013, 4:49:45 PM10/25/13
to salt-...@googlegroups.com
More questions.. :-)
In a jinja template, how would I get around this if the COMPANY_ENV grain didn't exist ?

{% if grains['COMPANY_ENV'] == 'PRD' %}
...
{% else %}
...
{% endif %}

Colton Myers

unread,
Oct 25, 2013, 7:01:33 PM10/25/13
to salt-...@googlegroups.com
Why not use the same default syntax?  Just use grains.get or the other syntax and compare with None (or whatever default you're using).

I guess I'm confused why this is a different question?  I may be missing something.

--
Colton Myers

Shantanu Bhattacharyya

unread,
Oct 26, 2013, 8:48:43 AM10/26/13
to salt-...@googlegroups.com
@Sebastien Wains: I ran into the same thing. Jinja does support the "in" operator (it works like it would work in python) so your conditional could look something like this as well {% if "COMPANY_ENV" in grains and grains["COMPANY_ENV"] == "PRD" %}. It is functionally equivalent to what basepi is suggesting although I'd argue easier to read.

@basepi: The problem is the jinja examples in the salt docs are too few and too elementary for actual use. In training Seth pointed us to the salt source which uses the ".get(), None" approach but I haven't seen anything like it in the docs or the jinja tutorial section in the walkthrough. Nor are there docs with nested conditionals, etc... I don't even think I saw compound conditionals -- A or B; A and B. Real world examples with some complexity would be a very helpful addition to the docs.

The nuances of yaml and jinja aren't immediately obvious to at least some of us unfortunately.

Mrten

unread,
Oct 26, 2013, 5:39:51 PM10/26/13
to salt-...@googlegroups.com
On 26/10/2013 14:48 , Shantanu Bhattacharyya wrote:

> The nuances of yaml and jinja aren't immediately obvious to at least
> some of us unfortunately.

These links should indeed definitely be in the docs:

Jinja: http://jinja.pocoo.org/docs/templates/
YAML: http://yaml.org/spec/1.2/spec.html
mako: http://docs.makotemplates.org/en/latest/

This one is very useful too:

yaml parser: http://yaml-online-parser.appspot.com/



There is a *lot* more possible then the docs currently hint at.

M.




Sebastien Wains

unread,
Oct 28, 2013, 5:03:08 AM10/28/13
to salt-...@googlegroups.com
Hmm sorry, it indeed works with {% if grains['COMPANY_ENV'] | default (None) == 'y' %}
I shouldn't be working so late :-)

More docs regarding Jinja and Yaml (and possibly mako, but I stick to the default) would be helpful.. I remember having to dig deep to find how to pass multiline arguments in YAML, it was buried somewhere in the doc.

Thanks guys

Colton Myers

unread,
Oct 28, 2013, 4:56:39 PM10/28/13
to salt-...@googlegroups.com
Thanks for the input!  We definitely need to get those links in -- I agree, our jinja documentation is severely lacking in complex examples.


--
Colton Myers
Reply all
Reply to author
Forward
0 new messages