saltstack-formulas / postfix-formula

http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html
Other
25 stars 130 forks source link

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

Closed Martin-Barr closed 2 years ago

Martin-Barr commented 2 years ago

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:


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 commented 2 years ago

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

AiYoriAoshi commented 2 years ago

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 commented 1 year ago

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 commented 1 year ago

Just in case anyone else stumbles across this same error, the fix mentioned above is found in this commit: https://github.com/saltstack-formulas/postfix-formula/commit/c432ae23dcc2b932a4431df952e3a04dbda85fd0

In postfix/files/mapping.j2

change:

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

to:

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