salt icon indicating copy to clipboard operation
salt copied to clipboard

Undefined jinja variable

Open gl1ch opened this issue 13 years ago • 51 comments

Hi, after upgrading to 0.17 I started getting an error "Undefined jinja variable; line 353 in template" when I try and set the following variable using grains. This worked before, did something change?

{%- set hostname = grains['id'] %}

snmp-package:
  pkg:
    - name: {{ pillar['packages']['snmp'] }}
    - installed

snmp-service:
  service:
    - name: {{ pillar['packages']['snmp-service'] }}
    - running
    - watch:
      - pkg: snmp-package
      - file: /etc/snmp/snmpd.conf
  file.managed:
    - name: /etc/snmp/snmpd.conf
    - user: root
    - group: root
    - mode: 600
    - source: salt://linux-server/snmp/etc/snmpd.jinja
    - template: jinja
    - require:
      - pkg: snmp-package

{% if 'postfix' in pillar['servers'][hostname]['roles'] %}
/etc/snmp/snmp_post_queue.sh:
  file.managed:
    - user: root
    - group: root
    - mode: 655
    - source: salt://linux-server/snmp/etc/snmp_post_queue.sh
    - require:
      - pkg: snmp-package
{% endif %}
am-vm-salt01:
    Data failed to compile:
----------
    Rendering SLS linux-server.snmp failed, render error: Undefined jinja variable; line 353 in template
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1929, in render_state
    rendered_sls=mods
  File "/usr/lib/python2.7/dist-packages/salt/template.py", line 69, in compile_template
    ret = render(input_data, env, sls, **render_kwargs)
  File "/usr/lib/python2.7/dist-packages/salt/renderers/jinja.py", line 42, in render
    tmp_data.get('data', 'Unknown render error in jinja renderer')
SaltRenderError: Undefined jinja variable; line 353 in template

gl1ch avatar Oct 04 '13 21:10 gl1ch

It's possible there is a grains issue (see #7624). What OS are you using?

basepi avatar Oct 04 '13 22:10 basepi

Currently using debian 7.1.0 on master and minion. Also it appears that "salt 'server*' grains.get id" is pulling the correct information so the module seems to be working.

gl1ch avatar Oct 04 '13 23:10 gl1ch

So is line 353 the grains['id'] line?

basepi avatar Oct 06 '13 17:10 basepi

No, the file doesnt have that many lines. To test I took my snmpd.jinja template down to the following two lines and it fails with the error above:

{%- set hostname = grains['id'] %}
agentAddress udp:127.0.0.1:161,udp:{{ pillar['servers'][hostname]['network_adapters']['eth0']['ip'] }}:161

but if I change my snmpd.jinja template to the following two lines it works:

{%- set hostname = grains['id'] %}
{{ hostname }}

gl1ch avatar Oct 07 '13 16:10 gl1ch

Also here is the error message from a debug output on the minion when only using the two lines above:

[CRITICAL] Rendering SLS linux-server.snmp failed, render error: Undefined jinja variable; line 353 in template Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1929, in render_state rendered_sls=mods File "/usr/lib/python2.7/dist-packages/salt/template.py", line 69, in compile_template ret = render(input_data, env, sls, **render_kwargs) File "/usr/lib/python2.7/dist-packages/salt/renderers/jinja.py", line 42, in render tmp_data.get('data', 'Unknown render error in jinja renderer')

gl1ch avatar Oct 08 '13 16:10 gl1ch

Was this happening on a previous version of salt or did it just start on 0.17?

basepi avatar Oct 09 '13 22:10 basepi

And if it worked on previous versions, from what version did you upgrade?

basepi avatar Oct 09 '13 22:10 basepi

This was not happening until upgrading to 0.17 as far as I know. I was using 0.16.4 before and I am still able to run these states on minions running 0.16.x (will double check the versions tomorrow).

gl1ch avatar Oct 10 '13 04:10 gl1ch

Same thing happened to me too after upgrading to 0.17. it doesn't happen with 0.16.4 if I downgrade it.

oba11 avatar Oct 10 '13 05:10 oba11

I think my problem could be related to this one:

After the update to 0.17 I get "Undefined jinja variable" on all the "file.managed" entries that try to use variables passed through the "defaults" or "context" fields. It worked on 0.16.4.

parruc avatar Oct 10 '13 08:10 parruc

I had the same problem when using pillars, e.g. {{pillar['mysql']['listen-addr']}}. Found out it was casued by the minion ID changing, from FQDN to short hostnames. Then the pillars where no longer accessible by that minion.

kpostrup avatar Oct 10 '13 12:10 kpostrup

In my case the minion can access the pillar data from the sls but stops working in "file.managed" statements that use defaults or context (not using grains there)

parruc avatar Oct 10 '13 13:10 parruc

Ah, now that @kpostrup posted, I think that might be the problem for most of you. There was a regression between 0.16.4 and 0.17.0 that made it so that /etc/hostname would be used to guess minion ID instead of socket.getfqdn(). So if your minion ID changed, then the targeting in your pillar may be broken, which would result in minions not having their pillar. Can anyone verify if this is their problem or not?

Note the minion IDs will revert back to socket.getfqdn() for 0.17.1, and we also will cache minion IDs so they don't change unexpectedly again.

basepi avatar Oct 11 '13 16:10 basepi

I dont think it is the hostname changing. To validate it I created a pillar for "hostname" and another for "hostname.domain" and the error message I still get the undefined variable.

gl1ch avatar Oct 18 '13 14:10 gl1ch

FWIW, I just ran into this issue and it turns out my problem was that I was importing a macro from a .sls file that used {{ pillar }} and I was not importing it with context. I moved the macro into another file for organizational purposes, but I'm sure adding with context to the import would've worked too.

borgstrom avatar Oct 20 '13 00:10 borgstrom

@gl1ch can you try the with context and @borgstrom mentioned? Curious if that's your problem.

basepi avatar Oct 21 '13 20:10 basepi

Sure, @basepi but I think I am confused as to how I would use the 'with context' in my state. Currently I am declaring the variable in my state file and using that same variable in that same state file. In the past I have used contexts to pass variables to other files but this is all in the same file. Am I missing something?

gl1ch avatar Oct 21 '13 22:10 gl1ch

Hehe, I also wasn't sure how to apply the with context that @borgstrom mentioned -- I haven't used the include syntax very extensively. And I can't find it in my cursory docs search. Can you elaborate, @borgstrom?

basepi avatar Oct 21 '13 23:10 basepi

with context wont apply in @gl1ch's case.

I was just chiming in because I got the exact same traceback (render error: Undefined jinja variable; line 353 in template) and found this ticket. This way if anyone ends up on this bug with searching and their problem is an import issue it might help them solve it.

Here's further reading on how with context works with jinja includes: http://jinja.pocoo.org/docs/templates/#import-visibility

Essentially, if you use pillar or any other builtin variable in a file that you include in another file you need to use with context or those builtin variables won't be available.

borgstrom avatar Oct 21 '13 23:10 borgstrom

Oh, gotcha. Thanks for the explanation.

@gl1ch Have you had a chance to test on the newly-released 0.17.1?

basepi avatar Oct 21 '13 23:10 basepi