jinja errors

100 views
Skip to first unread message

natxo....@gmail.com

unread,
Nov 8, 2021, 9:32:28 AM11/8/21
to Salt-users
hi,

I want to conditionally import a yaml file if the minion has rhel7, and another one if the minion is rhel8.

This is what works without but no distinction between major os versions (so not what I want):

{%- from salt['pillar.get']('repository_file', tpldir ~ '/repositories_rhel7.jinja') import repositories %}

So the minion looks at the file and imports it and it works.

I want something like this:
{%- if grains['osmajorrelease'] == '7' %}
{%- from salt['pillar.get']('repository_file', tpldir ~ '/repositories_rhel7.jinja') import repositories %}
{%- elif grains['osmajorrelease'] == '8' %}
{%- from salt['pillar.get']('repository_file', tpldir ~ '/repositories_rhel8.jinja') import repositories %}
{%- endif %}

but then:

[ERROR   ] Rendering exception occurred
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/salt/utils/templates.py", line 502, in render_jinja_tmpl
    output = template.render(**decoded_context)
  File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/lib/python3.6/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 12, in top-level template code
  File "/usr/lib/python3.6/site-packages/jinja2/sandbox.py", line 407, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'repositories' is undefined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/salt/utils/templates.py", line 261, in render_tmpl
    output = render_str(tmplstr, context, tmplpath)
  File "/usr/lib/python3.6/site-packages/salt/utils/templates.py", line 509, in render_jinja_tmpl
    raise SaltRenderError("Jinja variable {}{}".format(exc, out), buf=tmplstr)
salt.exceptions.SaltRenderError: Jinja variable 'repositories' is undefined
[CRITICAL] Rendering SLS 'username:redhat.localyumrepo' failed: Jinja variable 'repositories' is undefined
local:
    Data failed to compile:
----------
    Rendering SLS 'dev_username:redhat.localyumrepo' failed: Jinja variable 'repositories' is undefined

What am I doing wrong? I understand the error, the variable is not defined, but why?

Thanks

Phipps, Thomas

unread,
Nov 8, 2021, 7:05:06 PM11/8/21
to salt-...@googlegroups.com
because you are assuming strings when the returned value is int.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/32abbcdc-390b-4695-8ce2-e15dd5a35ab9n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages