jinja2 nested template inheritance

739 views
Skip to first unread message

Chris Apsey

unread,
Dec 31, 2016, 11:18:03 PM12/31/16
to Salt-users

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!


pillar data

https://git.cybbh.space/vta/saltstack/raw/master/pillar/apps/ceph/vm/mon.sls


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

Nicholas Capo

unread,
Dec 31, 2016, 11:31:07 PM12/31/16
to Salt-users

I think at that point pillar_id is just a variable, so try it without the quotes or the jinja tags:

pillar[pillar_id]

I'm not quite sure that's correct since I'm not very familiar with template inheiritence.

Nicholas


--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Apsey

unread,
Dec 31, 2016, 11:43:03 PM12/31/16
to Salt-users
That's what I thought at first as well, but if I remove the jinja expression tags, it properly renders the inner block based on the child, but interprets the outer block data as a literal string:

                  -  <vcpu placement='static'>4</vcpu>  <-------- this is the proper value, so it shouldn't try to overwrite it with the below line
                  +  <vcpu placement='static'>pillar[ceph_mon_config]['cpu']</vcpu>

It seems that the core issue is that either the outer block is rendered, or the inner one is rendered, but not both.  I'm starting to think this might be a limitation of jinja, but I'm not quite sure yet.  Based on the jinja2 documentation it *seems* like nested rendering should be possible, but I may be misinterpreting it.

Thanks for your input!

Thomas Phipps

unread,
Jan 1, 2017, 4:26:18 AM1/1/17
to salt-...@googlegroups.com
you don't need the blocks...

jinja variables also pass from template to template... 

here is your templates reworked using variables instead of blocks. 

blocks are meant more for data to be displayed not for internal logic. 
which is how you were trying to use it. 

you can't have jinja within jinja.
which means if you are outputting something in a echo tag [the {{}} tags]
it really is just a shortcut for {%- echo <whatever> -%}


--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Thomas Phipps
Linux/Openstack/Logstash

Message has been deleted

Chris Apsey

unread,
Jan 1, 2017, 10:19:40 AM1/1/17
to Salt-users
I can't believe I didn't think to try that.. thank you!!!!!  

It works perfectly.
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
Message has been deleted
0 new messages