Search code examples
for-loopjinja2salt-project

Using jinja variable as an argument in salt function used in a jinja for loop


I've been searching quite a lot but my newbiness prevent me from finding a solution that works ! Here is my problem (not that complexe, or so I thought) :

My pillar data :

server_access:
  ops: 
    - salt-dev
  ps:
    - client-salt.example.com
  rd:
  ops_trainees:

Then my jinja template excerpt :

{% set ldap_search_filter_additionnal = [] %}
{% for group in ['ops','ps','rd','ops_trainees'] %}
{% for servers in salt['pillar.get']('server_access:{{ group }}', {}) %}
{% if grains['fqdn'] == servers %}
{% set ldap_search_filter_additionnal = ldap_search_filter_additionnal.append('(isMemberOf="cn={{ group }},ou=test,dc=example,dc=com")') %}
{% endif %}
{% endfor %}
{% endfor %}

I am expecting to loop through the first 4 value, then the second (nested) loop to use each of these 4 value as an argument to the salt function, then if it matches the fqdn add some ldap search filters and so on ...

But it doesn't seems to be possible to use a jinja variable inside the salt function call, is it ?

If not what would you expert advise to get the expected behaviour ?

Bonus : Would you confirm that a jinja loop does not "break" when the if statement is not true ?

Thanks

UPDATE_1 :

After the first propositionof @Christophe Drevet-Droguet : I tried it but I couldn't make it work, or at least match my needs, but I did find something that "kind of" works, unless I only make it work for the first element, with the following try :

{%- set ldap_search_filter_additionnal = [] -%} 
{%- for server_group, server_name in pillar['server_access'].iteritems() -%} 
{%- if grains['fqdn'] == server_name[0] -%} 
{%- set testo = '(isMemberOf=\"cn='+server_group+',ou=interne,dc=example,dc=com\")' -%} 
{%- set ldap_search_filter_additionnal = ldap_search_filter_additionnal.append(testo) -%} 
{%- endif -%} 
{% endfor %}

I understand that I match (using the if statement) only the first element because of the "[0]" of "server_name[0]", so far i cannot do any better, so if you have another improvement to make works as I need, please :)

UPDATE_2 :

Ok I finally get the behaviour I was fighting for, but the code is somewhat awkward, so if anyone has tips to make it classier please just throw it down there !

Working solution in answer below.


Solution

  • Working solution :

    {%- set ldap_search_filter_additionnal = [] -%}
    {%- for serverGroup, serverName in pillar.get('server_access', {}).iteritems() -%}
    {%- for serverNameFull in serverName -%}
    {%- if serverNameFull == grains['fqdn'] -%}
    {%- set testo = '(isMemberOf=\"cn='+serverGroup+',ou=interne,dc=eloquant,dc=com\")' -%}
    {%- set ldap_search_filter_additionnal = ldap_search_filter_additionnal.append(testo) -%}
    {%- endif -%}
    {% endfor %}
    {% endfor %}
    

    The second nested loop is what saved me, but if I thought of this solution a long time ago, I could not accept it but now my head is burning I need something that just works, please improve this code, I usually go for clean, tidy and classy code, need help on this !