error rendering top pillar

250 views
Skip to first unread message

natxo....@gmail.com

unread,
Feb 21, 2022, 3:10:55 PM2/21/22
to Salt-users
hi,

we have this in our top.sls pillar file, running ok in older salt versions (2.*):

{%- set pillar_root = salt['saltutil.wheel']('pillar_roots.list_roots')['return'][saltenv][0].keys()[0] %}
{{ saltenv }}:

  '*':
    - schedule
    - rootpwd

{%- if salt['file.file_exists']('{0}/directory/clients.sls'.format(pillar_root)) %}
  'hostgroup:serversxx':
    - match: grain
    - serversxx.serverxx-clients
{%- endif %}

#include minions.<minion name> if file exists
{%- if salt['file.file_exists']('{0}/minions/{1}.sls'.format(pillar_root, grains.get('id').replace('.','-'))) %}
  {{ grains.get('id') }}:
    - minions.{{ grains.get('id').replace('.','-') }}
{%- endif %}

After upgrading the salt master to 3.004 the minion fails (both in 2.x and 3.004):
jinja2.exceptions.UndefinedError: dict_keys object has no element 0

I try to take a peak at what is in the pillar of the corresponding minion, but salt-run pillar.show_top minion shows nothing

How can I troubleshoot this?

Thanks in advance.
Regard,
Natxo

natxo....@gmail.com

unread,
Feb 21, 2022, 3:49:26 PM2/21/22
to Salt-users
Apologies for the wall of text approaching.

a bit further:

# salt-run pillar.show_top minion -l debug
[DEBUG   ] pillarenv 'base' not found in the configured pillar environments

Indeed we have no base environment, and this minion has another saltenv defined; if I pass saltenv=goodenvname then I see the desired top output for this particular environment

If I use another saltenv pillar top.sls with the old code, then it fails with the same error:


[ERROR   ] Pillar rendering failed for minion minionname
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 25, in top-level template code
  File "/usr/lib/python3.6/site-packages/jinja2/sandbox.py", line 456, in call
    return __self.format_string(fmt, args, kwargs, __obj)
  File "/usr/lib/python3.6/site-packages/jinja2/sandbox.py", line 449, in format_string
    rv = formatter.vformat(s, args, kwargs)
  File "/usr/lib64/python3.6/string.py", line 194, in vformat
    result, _ = self._vformat(format_string, args, kwargs, used_args, 2)
  File "/usr/lib64/python3.6/string.py", line 247, in _vformat
    result.append(self.format_field(obj, format_spec))
  File "/usr/lib64/python3.6/string.py", line 264, in format_field
    return format(value, format_spec)

jinja2.exceptions.UndefinedError: dict_keys object has no element 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/salt/pillar/__init__.py", line 684, in get_tops
    _pillar_rend=True,
  File "/usr/lib/python3.6/site-packages/salt/template.py", line 99, in compile_template
    ret = render(input_data, saltenv, sls, **render_kwargs)
  File "/usr/lib/python3.6/site-packages/salt/loader/lazy.py", line 149, in __call__
    return self.loader.run(run_func, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/salt/loader/lazy.py", line 1201, in run
    return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/contextvars/__init__.py", line 38, in run
    return callable(*args, **kwargs)
  File "/usr/lib/python3.6/site-packages/salt/loader/lazy.py", line 1216, in _run_as
    return _func_or_method(*args, **kwargs)
  File "/usr/lib/python3.6/site-packages/salt/renderers/jinja.py", line 75, in render
    **kws
  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 dict_keys object has no element 0
[DEBUG   ] Sending event: tag = salt/run/20220221203727857405/progress; data = {'data': ['Rendering Primary Top file failed, render error:\nJinja variable dict_keys object has no element 0'], 'outputter': 'nested', '_stamp': '2022-02-21T20:37:30.375981'}
[DEBUG   ] LazyLoaded nested.output
event:
    ----------
    _stamp:
        2022-02-21T20:37:30.375981
    data:
        - Rendering Primary Top file failed, render error:
          Jinja variable dict_keys object has no element 0
    outputter:
        nested
suffix:
    progress
[DEBUG   ] LazyLoaded local_cache.prep_jid
[DEBUG   ] Sending event: tag = salt/run/20220221203727857405/ret; data = {'fun': 'runner.pillar.show_top', 'jid': '20220221203727857405', 'user': 'sudo_xxxx@sddl', 'fun_args': ['minionname', {'saltenv': 'othername'}], '_stamp': '2022-02-21T20:37:30.385273', 'return': ['Rendering Primary Top file failed, render error:\nJinja variable dict_keys object has no element 0'], 'success': True}
[DEBUG   ] LazyLoaded nested.output
- Rendering Primary Top file failed, render error:
  Jinja variable dict_keys object has no element 0
[INFO    ] Runner completed: 20220221203727857405
[DEBUG   ] Closing IPCMessageClient instance
[DEBUG   ] Runner return: ['Rendering Primary Top file failed, render error:\nJinja variable dict_keys object has no element 0']

the master itself is set as minion with another saltenv name, not base.

So where is this saltenv coming from?

Phipps, Thomas

unread,
Feb 21, 2022, 4:08:18 PM2/21/22
to salt-...@googlegroups.com

so looking at the code the only index 0 item is in the line {%- set pillar_root = salt['saltutil.wheel']('pillar_roots.list_roots')['return'][saltenv][0].keys()[0] %} which means that the return from that has changed.

here are two other ways to do the exact same lookup.

{%- set pillar_root = salt['saltutil.wheel']('pillar_roots.list_roots')['return'][saltenv][0]| first %}
{%- set pillar_root = salt['saltutil.runner']('config.get',arg=['pillar_roots'])[saltenv][0] %}

as for your question about where base is coming from when you don’t set saltenv in salt-run. salt-run is purely a master function. minion configs are not set here. so it doesn’t see that the minion is set to saltenv. even if the masters minion config is set to something other than base it will still error with that for the salt-run command without a saltenv as the minion on the master is NOT a part of the master. it is just another minion that just happens to exist on the same server that the master does.


--
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/8e81e197-4d4c-40b4-8f0f-3e13950e797dn%40googlegroups.com.

natxo....@gmail.com

unread,
Feb 21, 2022, 4:29:40 PM2/21/22
to Salt-users
This does it indeed. I will retest tomorrow with salt minions still on 2.* so that the upgrade process goes smoothly.

Thanks!
Reply all
Reply to author
Forward
0 new messages