Search code examples
salt-project

accessinig current pillar items in a loop from another template?


I have snippet like this in the init.sls:

{% for server, args in pillar.get('servers', {}).items() %}

software-server@{{ server }}
    service.running:
        - enable: true
        - require:
            - pkg: software_pkgs
        - watch:
            - file: software_config

/etc/software/{{server}}.json:
    file.managed:
        - source: salt://software/files/config.json.j2
        - template: jinja

{% endfor %}

config.json.j2:

{
  listen: {{server}}:{{listen_addr}}
}

and in the pillar:

software.servers:
  server1:
    listen_addr:10.0.0.1
  server2:
    listen_addr:127.0.01

in each of the {{server}}.json the listen_addr is different. I don't know if saltstack has something like a scope for current loop, or is there a workaround for this.


Solution

  • You probably need to use context or defaults options in file.managed:

    file.managed

    In your example it would like like :

    /etc/software/{{server}}.json:
    file.managed:
        - source: salt://software/files/config.json.j2
        - template: jinja
        - context:
          server: {{ server }}
          listen_addr: {{ server['listen_addr'] }}