Search code examples
salt-project

Saltstack: creating Java properties file from Jinja template with arbitrary values


Using Saltstack (as a complete newbie), I need to deliver a Java properties file on the target. There will be a template for this file (I presumed the best template format would be Jinja) and a collection of key/value pairs to populate it. So I could do something like this:

/some/file/on/target:
  file.managed:
    - source:
      - salt://template.jinja
    - template: jinja
    - defaults:
      key1: value1
      key2: value2

The problem is that the collection is variable (over time, and from one environment to another), and the salt formula doesn't seem the best place to put its members. The salt formulas will be maintained by dev ops staff, while the properties and their values in different environments will be maintained by developers.

Is there a good way to do this?


Solution

  • Figured it out:

    /some/file/on/target:
      file.managed:
        - source:
          - salt://template.jinja
        - template: jinja
        - defaults:
    {%- for key, value in pillar.get('default', {}).items() %}
          {{key}}: {{value}}
    {%- endfor %}
    

    Note the specific form of the opening tag, to suppress whitespace, otherwise it's not valid YAML. The default pillar contains e.g.:

    key1: value1
    key2: value2
    .. etc.
    

    Sorry to answer my own question. It really did take me that long to hit on the answer.