Philipp Kahr Philipp Kahr - 1 year ago 166
Python Question

Jinja2 Loops with Saltstack

I experience a issue with my jinja2 template when using for loops. I guess I'm just to dumb to get the right syntax.

{% for options in salt['pillar.get']('') %}
//trying to access a yaml list (posted below)`
{% for addjavavariables in options %}
//trying to get the lists out of the options
{{[0] }}
//trying to write every single line from my list
{{ addjavavariables }}:
- {{ addjavavariables }}
{% endfor %}
{% endfor %}

The YAML looks like this.:

- 'Xms1200M'
- 'Xmx1200M'
- 'XX:MaxDirectMemorySize=2G'
- 'XX:+UnlockDiagnosticVMOptions'
- 'XX:+UnsyncloadClass'
- 'XX:+LogVMOutput'
- 'XX:LogFile=../sonatype-work/nexus3/log/jvm.log'
- ''
- 'Dkaraf.home=.'
- 'Dkaraf.base=.'
- 'Dkaraf.etc=etc/karaf'
- 'Djava.util.logging.config.file=etc/karaf/'
- ''
- ''
- "Dkaraf.s'tartLocalConsole=false"
- 'Djava.util.prefs.userRoot=/home/nexus/.java'

Final file should looks like


my problem is, that I don't get anything into the file. It also won't go into the loop at all. Can anybody give me a hint, how I can put all the items in the list with a starting dash into the file?

Answer Source

The path separator for pillar.get is :, not ., so you should go with salt['pillar.get']('nexus:file:nexus:vmoptions').
But you could also simply use pillar['nexus']['file']['nexus']['vmoptions']

A weird thing is that you have 2 for loops but only 1 list to iterate over. Is there other keys in the nexus:file:nexus:vmoptions dict?

To get the required result, I would go with:

{% for addjavavariable in salt['pillar.get']('nexus:file:nexus:vmoptions:addjavavariables', []) %}
-{{ addjavavariable }}
{% endfor %}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download