0

Background: I need to make an XML file like this:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="BaseOU" value="DC=myplace,DC=wan" />
    <add key="OurOU" value="OU=Users,DC=myplace,DC=wan" />
    <add key="EmailServer" value"email.server.com" />
    <add key="EmailRecipient" value"monitor@email.com" />
  </appSettings>
</configuration>

I would like to build this file dynamically so we can simply add new variables to pillar without having to change the jinja template for the managed file. Can I loop though pillar key/value pairs without knowing the keys?

For example, let's say my pillar.sls looks like this:

scriptvars:
  BaseOU: DC=myplace,DC=wan
  OurOU: OU=Users,DC=myplace,DC=wan
  EmailServer: email.server.com
  EmailRecipient: monitor@email.com

and I make a state that manages the file C:\script-config.xml, and the source is a jinja template similar to:

<?xml version="1.0"?>
<configuration>
  <appSettings>
  {% for unknownPillarKey in pillar.get['scriptvars'] %}
    <add key="unknownPillarKey" value="{{ pillar['unknownPillarKey'] }}" />
  {% endfor %}
  </appSettings>
</configuration>

Question: How do I add a new entry to scriptvars in my pillar.sls and have the for loop in the jinja template pick it up (when I apply the state - this isn't a question about beacons/reactors) and add it to the managed file? I think the issue is my jinja for loop or syntax but I can't find any info online. Many thanks for any insight.

static
  • 141
  • 7

1 Answers1

0

I found an answer, and it was a lot easier than I expected (unfortunately, searching online didn't help much because pillar pretty well always discusses keys and values).

It is as simple as the following:

<?xml version="1.0"?>
<configuration>
  <appSettings>
  {% for key, value in pillar['scriptvars'].items() %}
    <add key="{{ key }}" value="{{ value }}" />
  {% endfor %}
  </appSettings>
</configuration>

Important note: .items() is for the python 3 version of salt. If you are using the python 2 version, you should upgrade - it's 2019.

Thanks to these for pointing me in the proper direction:

https://stackoverflow.com/questions/31854153/how-to-get-key-value-in-salt

How to create salt from pillar using saltstack?

static
  • 141
  • 7