Jinja/Python String Conditionals in State Files?

1,021 views
Skip to first unread message

Robert Parker

unread,
Jul 16, 2013, 9:41:53 AM7/16/13
to salt-...@googlegroups.com
I have a set of more or less identical servers with very similar names and they are arbitrarily grouped into app clusters.  I need a way, based on arbitrary hostname with an ordinal on the end, to group them together and give them a unique config file within a salt state file.  

So, I have a group of 12 servers, named server1, server2, server3 [..] server12, etc.    Servers 1-4 are in cluster 1, 5-8 are in cluster 2, and 9-12 are in cluster 3.  What I want is something like this:

/etc/sysconfig/tomcat7:
   file.managed:
     - source:

     # Cluster 1
     {% if grains['host'].endswith ('1|2|3|4)' %}             
       - salt://files/config/tomcat7.conf.cluster1
     {% endif %}


     # Cluster 2
     {% if grains['host'].endswith ('3|4|5|6)' %}             
       - salt://files/config/tomcat7.conf.cluster2
     {% endif %}


     # Cluster 3
     {% if grains['host'].endswith ('7|8|9|10)' %}             
       - salt://files/config/tomcat7.conf.cluster3
     {% endif %}

     - user: tomcat
     - group: tomcat
     - mode: 644

However the "  {% if grains['host'].endswith ('x|y|z)' %} " statement doesn't seem to work.  What is the right syntax to do something like this?

Thanks,
Robert

Umberto Nicoletti

unread,
Jul 16, 2013, 11:22:00 AM7/16/13
to salt-...@googlegroups.com
Totally untested, but should/might work (with minor adjustments):

 {% if grains['host']|truncate(6,True,'') in range(1,4) %}      

truncate and | are respectively documented at:


hth,
Umberto

Robert Parker

unread,
Jul 16, 2013, 3:24:22 PM7/16/13
to salt-...@googlegroups.com
Hmm, this is nice but I need something more flexible that can work with hostnames of varyling lengths if need be.  is there no way to invoke a regexp within a jinja conditional?

Robert

Avi Marcus

unread,
Jul 16, 2013, 3:34:14 PM7/16/13
to salt-...@googlegroups.com
Perhaps: set a grain to specify which cluster the machine is in. That will let you cleanly use this information in more places.

-Avi

--
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.
 
 

Miguel

unread,
Jul 16, 2013, 3:38:22 PM7/16/13
to salt-...@googlegroups.com
Check this conversation out for how to get a helper set up to do what you want:

Mrten

unread,
Jul 16, 2013, 3:58:36 PM7/16/13
to salt-...@googlegroups.com
On 16/7/2013 21:24 , Robert Parker wrote:

> Hmm, this is nice but I need something more flexible that can work with
> hostnames of varyling lengths if need be. is there no way to invoke a
> regexp within a jinja conditional?

if you need to go there it is imho more effective to jump over to a mako
renderer which allows easy escapes into python, or even write a pure
python renderer.

jinja is only practical for simple logic (which makes sense, given
jinja's original goal).

M.

Ethan Erchinger

unread,
Jul 16, 2013, 4:27:09 PM7/16/13
to salt-...@googlegroups.com
{% set shortname = grains['id'].split('.')[0] %}
{% set cluster = shortname.rstrip('1234567890') %}
{% set hostid = shortname.split(cluster)[1]|int %}

{% if hostid in range(1,4) %}
Reply all
Reply to author
Forward
0 new messages