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