Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jinja variable odict_values object has no element 0 python2 to python3 error #125

Closed
Martin-Barr opened this issue Jul 6, 2022 · 4 comments
Labels

Comments

@Martin-Barr
Copy link

Your setup

Formula commit hash / release tag

[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = https://github.com/saltstack-formulas/postfix-formula
fetch = +refs/heads/:refs/remotes/origin/
[branch "master"]
remote = origin
merge = refs/heads/master

Versions reports (master & minion)

salt3004.1

Pillar / config used

Our postfix.sls update file

#!jinja|yaml|gpg
postfix:
config:
relayhost: '[email-smtp.eu-west-2.amazonaws.com]:587'
smtp_sasl_auth_enable: 'yes'
smtp_sasl_security_options: noanonymous
smtp_sasl_password_maps: hash:/etc/postfix/sasl_passwd
sender_canonical_maps: hash:/etc/postfix/canonical
smtp_use_tls: 'yes'
smtp_tls_security_level: encrypt
smtp_tls_note_starttls_offer: 'yes'
smtp_tls_CAfile: /etc/pki/tls/certs/ca-bundle.crt
mapping:
smtp_sasl_password_maps:
- '[email-smtp.eu-west-2.amazonaws.com]:25': xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- '[email-smtp.eu-west-2.amazonaws.com]:465': xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
- '[email-smtp.eu-west-2.amazonaws.com]:587': xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


Bug details

Describe the bug

In Python 2, calling the "values" method of a dictionary returned a list that then could be accessed using an index, but in Python 3 the "values" method returns a iterable object that can't be indexed.

Steps to reproduce the bug

Salt-3004.1 server
Salt-minion linux 7.9

github postfix formulas

Expected behaviour

You will get the same output as I did

      ID: postfix_smtp_sasl_password_maps
Function: file.managed
    Name: /etc/postfix/sasl_passwd
  Result: False
 Comment: An exception occurred in this state: 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 10, in top-level template code
            File "/usr/lib/python3.6/site-packages/jinja2/sandbox.py", line 462, in call
              return __context.call(__obj, *args, **kwargs)
            File "/usr/lib/python3.6/site-packages/jinja2/runtime.py", line 679, in _invoke
              rv = self._func(*arguments)
            File "<template>", line 2, in template
            File "/usr/lib/python3.6/site-packages/jinja2/tests.py", line 158, in test_iterable
              iter(value)
          jinja2.exceptions.UndefinedError: odict_values 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/state.py", line 2180, in call
              *cdata["args"], **cdata["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/loader/lazy.py", line 1249, in wrapper
              return f(*args, **kwargs)
            File "/usr/lib/python3.6/site-packages/salt/states/file.py", line 3026, in managed
              **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/modules/file.py", line 5409, in check_managed_changes
              **kwargs
            File "/usr/lib/python3.6/site-packages/salt/modules/file.py", line 4661, in get_managed
              **kwargs
            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 odict_values object has no element 0

Attempts to fix the bug

No attempts to fix yet

Additional context

N/A

@Martin-Barr Martin-Barr added the bug label Jul 6, 2022
@Martin-Barr
Copy link
Author

I found your solution in the code. I have now implemented and it is working.

@AiYoriAoshi
Copy link

I found your solution in the code. I have now implemented and it is working.

Can you elaborate on the solution? I'm not sophisticated in Python or Salt and just followed the existing configuration and pillar.example file and I'm totally lost

@jeffdyke
Copy link

jeffdyke commented Feb 2, 2023

I found your solution in the code. I have now implemented and it is working.

These types of answers are the dredge of github and all programming help area's society. If you happen upon this page, don't do this.

@D010101
Copy link

D010101 commented May 26, 2023

Just in case anyone else stumbles across this same error, the fix mentioned above is found in this commit:
c432ae2

In postfix/files/mapping.j2

change:

{{ format_value(item.keys()[0], item.values()[0]) }}

to:

{%-   set key, value = item.popitem() %}
{{ format_value(key, value) }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants