0

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']('nexus.file.nexus.vmoptions') %}
//trying to access a yaml list (posted below)`
{% for addjavavariables in options %}
//trying to get the lists out of the options
  {{ nexus.file.nexus.vmoptions.addjavavariables[0] }}
//trying to write every single line from my list 
  {{ addjavavariables }}:
  - {{ addjavavariables }}
   {% endfor %} 
{% endfor %}

The YAML looks like this.:

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

Final file should looks like

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

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?

1 Answer 1

2

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 %}
2
  • Thank you very much! It worked well. I thought i had to iterate over every item in the list to get the item. I didn't know that i can go for addjavavariables and automatically get every item. I thought i always have to go for something like addjavavariables[index].value. I marked your answer as right. Commented Jul 27, 2017 at 12:29
  • 1
    The [] version will raise if anything is missing, while the single key with : will not.
    – OrangeDog
    Commented Feb 2, 2022 at 11:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.