jinja string match

27,656 views
Skip to first unread message

Spiros Ioannou

unread,
Apr 29, 2013, 12:18:07 PM4/29/13
to salt-...@googlegroups.com
Hi,
is there a way in jinja to do string matching?
I would like to do something like:

{% if 'string' in grains['key'] %}
   xxxx
{% else %}
   yyyy
{% endif %}

Mrten

unread,
Apr 29, 2013, 3:07:48 PM4/29/13
to salt-...@googlegroups.com
http://jinja.pocoo.org/docs/templates/#list-of-builtin-filters

Short story: you can't [1]. Jinja isn't suitable for anything but the
very simplest expressions. The worst part is that you sometimes find
jinja 1.x docs with google that say you can do some things but then it
won't work if you try.

For more expressiveness you really should check out mako:

http://docs.makotemplates.org/en/latest/syntax.html

(I usually end up writing python-in-mako)

M.

[1] I'd love to be proven wrong though!

Mina Naguib

unread,
Apr 29, 2013, 3:34:56 PM4/29/13
to salt-...@googlegroups.com
I agree that jinja's often limiting, however unless I'm missing something, Spiros's example actually works out of the box…

In a test pillar:
{% if 'node3' in grains["id"] %}
test: yayaya matches node3
{% else %}
test: nonono
{% endif %}

Then:

# salt '*' pillar.get test
cm-mina-master-gentoo:
    nonono
cm-mina-node4-centos:
    nonono
cm-mina-node2-gentoo:
    nonono
cm-mina-node3-gentoo:
    yayaya matches node3
cm-mina-node1-gentoo:
    nonono



Mrten

unread,
Apr 29, 2013, 5:47:38 PM4/29/13
to salt-...@googlegroups.com
On 29/4/2013 21:34 , Mina Naguib wrote:

> I agree that jinja's often limiting, however unless I'm missing
> something, Spiros's example actually works out of the box�

Well shiver me timbers! That's what you get for just reading
documentation instead of trying stuff out ;)

Thank you for pointing out my error; I sure hope I can use this some time!

M.
Reply all
Reply to author
Forward
0 new messages