Search code examples
yamlsalt-project

saltstack, multi-line pillar items interpolated into template


I have a pillar that looks like this:

inline_blocks:
  the_seven: |
    dog cat horse cow
    ardvaark beatle snail

which I then want to insert into a file

{% set inline_block = pillar['inline_blocks'].get(val, '') %}
/etc/animals.conf:
  file.managed:
    - source: salt://farm/animals.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
        extras: {{ inline_block }}

and then in animals.conf,

{{ extras }}

I expect that if the key val is in inline_blocks, then its value will be interpolated in. If it's not, an empty string will be interpolated in.

Indeed, that's what happens if I write the defaults statement explicitly:

    - defaults:
        extras: |
          dog cat horse cow
          ardvaark beatle snail

but as written above, I get the error could not find expected ':'.

As a reality check, pillar.items happily retrieves the pillar entry, so (1) the pillar entry can be retrieved, and (2) the value can be interpolated, but (X) the multi-line value in the .sls file is causing problems.

Any pointers what the right syntax is to do this?


Solution

  • This issue was discussed in a bug as well, but for the content parameter. It seems to apply to multi-line YAML blocks passed as defaults or context also.

    Since you are using a Jinja template file as a source, we can easily fetch pillar data from template itself (as one of the comments suggests in above link).

    Considering pillar as:

    inline_blocks:
      val: |
        dog cat horse cow
        ardvaark beatle snail
    

    Then the animals.conf.j2 template as:

    {{ salt.pillar.get('inline_blocks:val', default="foo") }}
    

    Note: If this pillar data is assured to be always defined, we might even use pillar['inline_blocks']['val'] in the template.

    Rendered with a state like:

    create-animals-conf:
      file.managed:
        - name: /tmp/animals.conf
        - source: salt://animals.conf.j2
        - mode: 0664
        - template: jinja
    

    Should yield the template as you expect:

    $ cat /tmp/animals.conf
    dog cat horse cow
    ardvaark beatle snail