Jinja Template Import

1,575 views
Skip to first unread message

Jacob Albretsen

unread,
Jul 10, 2012, 6:34:52 PM7/10/12
to salt-...@googlegroups.com
Hi Salt,

I have a init.sls file with a jinja macro defined in it. I include it in
spam.sls so Salt sees it, but if I do an import statement in spam.sls so jinja
sees it, it cannot find the location of init.sls. It looks in
/var/cache/salt/files. I've tried a number ways to get it to see the correct
path with no luck. Can someone bonk me on the head and tell me what I'm
missing?

Thanks,

- Jake

Thomas S Hatch

unread,
Jul 10, 2012, 7:56:39 PM7/10/12
to salt-...@googlegroups.com
Hi Jake,

What do your sls files look like? I think you need to use the jinja import to make them work

Jacob Albretsen

unread,
Jul 11, 2012, 1:56:46 PM7/11/12
to salt-...@googlegroups.com
On Tuesday, July 10, 2012 05:56:39 PM Thomas S Hatch wrote:
> Hi Jake,
>
> What do your sls files look like? I think you need to use the jinja import
> to make them work

in init.sls, amoung other things, I have this:

{% macro emailforward(user,address) %}
{{ user }}_forward:
file.managed:
- user: {{ user }}
- group: {{ user }}
- mode: '0400'
- name: /home/{{ user }}/.forward
- template: jinja
- source: salt://users/files/forward
- require:
- user: {{ user }}
- defaults:
email: "{{ address }}"
{% endmacro %}

Then I have sisko.sls for The Sisko user. In there I have this:

{% import 'init.sls' as user %}
{{ user.emailforward("sisko","capt...@deepspace9.com") }}

When I try to run it, I get this:

Data failed to compile:
----------
Rendering SLS users.sisko failed, render error:
Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/utils/templates.py", line 91, in
jinja
data = template.render(**passthrough)
File "/usr/lib64/python2.6/site-packages/jinja2/environment.py", line 669,
in render
return self.environment.handle_exception(exc_info, True)
File "/var/cache/salt/files/test/users/sisko.sls", line 48, in top-level
template code
{% import 'init.sls' as user %}
File "/usr/lib/python2.6/site-packages/salt/utils/jinja.py", line 75, in
get_source
with open(filepath, 'rb') as f:
IOError: [Errno 2] No such file or directory:
'/var/cache/salt/files/test/init.sls'

And as I write, this, I realize that it needs to be this:

{% import 'users/init.sls' as user %}
{{ user.emailforward("sisko","capt...@deepspace9.com") }}

But this spits out another error:

Data failed to compile:
----------
Rendering SLS users.sisko failed, render error:
Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/utils/templates.py", line 91, in
jinja
data = template.render(**passthrough)
File "/usr/lib64/python2.6/site-packages/jinja2/environment.py", line 669,
in render
return self.environment.handle_exception(exc_info, True)
File "/var/cache/salt/files/test/users/sisko.sls", line 48, in top-level
template code
{% import 'users/init.sls' as user %}
File "/usr/lib64/python2.6/site-packages/jinja2/environment.py", line 729,
in module
self._module = rv = self.make_module()
File "/usr/lib64/python2.6/site-packages/jinja2/environment.py", line 713,
in make_module
return TemplateModule(self, self.new_context(vars, shared, locals))
File "/usr/lib64/python2.6/site-packages/jinja2/environment.py", line 769,
in __init__
self._body_stream = list(template.root_render_func(context))
File "/var/cache/salt/files/test/users/init.sls", line 23, in top-level
template code
{{ rootconfig("bash_profile") }}
File "/var/cache/salt/files/test/users/init.sls", line 19, in template
- source: salt://users/files/root/root.{{ name }}.CentOS.{{
grains['osrelease'][0] }}
File "/usr/lib64/python2.6/site-packages/jinja2/environment.py", line 331,
in getitem
return obj[argument]
UndefinedError: 'grains' is undefined

So also in this init.sls I have lines like:

- source: salt://users/files/root/root.{{ name }}.CentOS.{{ grains['osrelease']
[0] }}

I use this approach to easily differentiate files between CentOS 5 and CentOS 6,
but it appears in the import grains ends up being undefined.

My initial thought is to maybe just put the macro (and other macros I may need
to import) in another SLS file.

Thanks,

- Jake

Alec Koumjian

unread,
Jul 11, 2012, 2:06:42 PM7/11/12
to salt-...@googlegroups.com
Since init.sls is not a python module, I think what you actually want is to use the include declaration:

Jacob Albretsen

unread,
Jul 11, 2012, 2:19:48 PM7/11/12
to salt-...@googlegroups.com
On Wednesday, July 11, 2012 11:06:42 AM Alec Koumjian wrote:
> Since init.sls is not a python module, I think what you actually want is to
> use the include declaration:
>
> http://docs.saltstack.org/en/latest/ref/states/highstate.html#term-include-d
> eclaration

It is already included in sisko.sls for other items in my init.sls files.

Alec Koumjian

unread,
Jul 11, 2012, 2:39:35 PM7/11/12
to salt-...@googlegroups.com
So then just remove the import statement and your macro should be available, I think. Though I have not tested this.

Jacob Albretsen

unread,
Jul 11, 2012, 2:45:25 PM7/11/12
to salt-...@googlegroups.com
On Wednesday, July 11, 2012 11:39:35 AM Alec Koumjian wrote:
> So then just remove the import statement and your macro should be
> available, I think. Though I have not tested this.

Nope it does not work. That's what started the whole deal of me figuring out
jinja template inheritance.


Jacob Albretsen

unread,
Jul 11, 2012, 2:47:39 PM7/11/12
to salt-...@googlegroups.com
On Wednesday, July 11, 2012 11:56:46 AM Jacob Albretsen wrote:

> My initial thought is to maybe just put the macro (and other macros I may
> need to import) in another SLS file.

I can confirm that this works. I just made a macro.sls file with just the email
forward macro and imported that. Still, I don't know of losing knowledge of
grains is a bug or something I'm missing.

Dan Colish

unread,
Jul 11, 2012, 2:54:47 PM7/11/12
to salt-...@googlegroups.com
You need to bring context in during an import if you want jinja to use it. Check out the jinja docs; http://jinja.pocoo.org/docs/templates/#import-context-behavior

Jacob Albretsen

unread,
Jul 11, 2012, 3:08:12 PM7/11/12
to salt-...@googlegroups.com
On Wednesday, July 11, 2012 11:54:47 AM Dan Colish wrote:
> You need to bring context in during an import if you want jinja to use it.
> Check out the jinja
> docs; http://jinja.pocoo.org/docs/templates/#import-context-behavior

BINGO! Thanks!

Reply all
Reply to author
Forward
0 new messages