Search code examples
jinja2template-enginesalt-project

saltstack jinja2 nested template inheritance


I'm having some issues with nested template inheritance in saltstack.

I'm expecting create_mon to pass the string 'ceph_mon_config' to create, which then pulls the values of cpu, mem, etc. in the 'ceph_mon_config pillar to prepare, which does the heavy lifting. As it stands now when the renderer reaches:

{% block cpu %}{{ pillar['{% block pillar_id %}{% endblock pillar_id %}']['cpu'] }}{% endblock cpu %}

It looks like it is interpreting it literally:

Rendering SLS 'base:/apps/ceph/create-mon' failed: Jinja variable 'salt.pillar object' has no attribute '{% block pillar_id %}{% endblock pillar_id %}'

Any ideas?

Thanks!

create_mon.sls

{% extends "states/system/vm/create.sls" %}

{% block pillar_id %}ceph_mon_config{% endblock pillar_id %}

virsh create /kvmfs/vms/{{ pillar['ceph_mon_config']['hostname'] }}/config.xml:
  cmd.run:
    - requires:
      - sls: /states/system/vm/create

create.sls

{% extends "states/system/vm/prepare.sls" %}

{% block cpu %}{{ pillar['{% block pillar_id %}{% endblock pillar_id %}']['cpu'] }}{% endblock cpu %}
{% block mem %}{{ pillar['ceph_mon_config']['mem'] }}{% endblock mem %}
{% block network %}{{ pillar['ceph_mon_config']['network'] }}{% endblock network %}
{% block os %}{{ pillar['ceph_mon_config']['os'] }}{% endblock os %}
{% block hostname %}{{ pillar['ceph_mon_config']['hostname'] }}{% endblock hostname %}
{% block disk %}{{ pillar['ceph_mon_config']['disk'] }}{% endblock disk %}

prepare.sls

/kvmfs/vms/{% block hostname %}{% endblock hostname %}/config.xml:
  file.managed:
    - source: https://git.cybbh.space/vta/saltstack/raw/master/apps/virsh/files/common.xml
    - makedirs: True
    - template: jinja
    - skip_verify: True
    - defaults:
        name: {{ self.hostname() }}
        mem: {% block mem %}{% endblock mem %}
        cpu: {% block cpu %}{% endblock cpu %}
        network: {% block network %}{% endblock network %}

/kvmfs/vms/{{ self.hostname() }}/disk0.qcow2:
  file.copy:
    - source: /kvmfs/images/{% block os %}{% endblock os %}-latest

qemu-img resize /kvmfs/vms/{{ self.hostname() }}/disk0.qcow2 {% block disk %}{% endblock disk %}:
  cmd.run:
    - requires:
      - /kvmfs/vms/{{ self.hostname() }}/disk0.qcow2

/kvmfs/vms/{{ self.hostname () }}/data/meta-data:
  file.managed:
    - source: https://git.cybbh.space/vta/saltstack/raw/master/apps/virsh/files/common.metadata
    - makedirs: True
    - template: jinja
    - skip_verify: True
    - defaults:
        hostname: {{ self.hostname() }}

/kvmfs/vms/{{ self.hostname () }}/data/user-data:
  file.managed:
    - source: https://git.cybbh.space/vta/saltstack/raw/master/apps/virsh/files/common.userdata
    - makedirs: True
    - skip_verify: True

genisoimage -o /kvmfs/vms/{{ self.hostname () }}/config.iso -V cidata -r -J /kvmfs/vms/{{ self.hostname () }}/data/meta-data /kvmfs/vms/{{ self.hostname () }}/data/user-data:
  cmd.run:    
    - requires:
      - /kvmfs/vms/{{ self.hostname () }}/data/metadata
      - /kvmfs/vms/{{ self.hostname () }}/data/userdata

Solution

  • Jinja doesn't allow nested evaluations, but variables are also passed from template to template when using 'extends', not just blocks.

    create_mon

    {% set pillar_id = 'ceph_mon_config' %}
    

    create

    ...
    {% extends "states/system/vm/prepare.sls" %}
    
    {% block cpu %}{{ pillar[pillar_id]['cpu'] }}{% endblock cpu %}
    ...
    

    This works as expected.