You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description
I instantiate two salt caller clients in the same python environment and it looks like the cached utils.jinja file_client is reused between them when resolving jinja imports.
This cause the second minion to fail when trying to resolve jinja imports. It seems like the file_client of the first salt minion is used and it validates that the imported file already exists in the first minion cachedir (and does not download the file to the second minion cache). The second minion will then produce an error because the file is not downloaded to the second minion cachedir. This also fail on the master branch.
I am trying to run salt states with test=true mode in order to validate salt/jinja file rendering before deploying. Each client is instantiated with all the 'cache' related config setup to point to a per minion directory.
git clone git@github.com:jmarais/salt-utils-jinja-cache-issue.git
cd salt-utils-jinja-cache-issue
make run
result:
end of log output
2022-08-22 10:31:51,732 salt.utils.templates DEBUG:Jinja Error
2022-08-22 10:31:51,732 salt.utils.templates DEBUG:Exception:
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/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "<template>", line 1, in top-level template code
File "/usr/lib/python3.6/site-packages/salt/utils/jinja.py", line 237, in get_source
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: jinja_imported.j2
2022-08-22 10:31:51,733 salt.utils.templates DEBUG:Out:
2022-08-22 10:31:51,733 salt.utils.templates DEBUG:Line: 1
2022-08-22 10:31:51,733 salt.utils.templates DEBUG:TmplStr: {%- from 'jinja_imported.j2' import foo -%}
what is the value: {{ foo.bar }}
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/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "<template>", line 1, in top-level template code
File "/usr/lib/python3.6/site-packages/salt/utils/jinja.py", line 237, in get_source
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: jinja_imported.j2
2022-08-22 10:31:51,733 salt.utils.templates DEBUG:TraceStr: 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/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "<template>", line 1, in top-level template code
File "/usr/lib/python3.6/site-packages/salt/utils/jinja.py", line 237, in get_source
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: jinja_imported.j2
2022-08-22 10:31:51,733 salt.utils.templates 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/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "<template>", line 1, in top-level template code
File "/usr/lib/python3.6/site-packages/salt/utils/jinja.py", line 237, in get_source
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: jinja_imported.j2
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 548, in render_jinja_tmpl
"Jinja error: {}{}".format(exc, out), line, tmplstr, trace=tracestr
salt.exceptions.SaltRenderError: Jinja error: jinja_imported.j2
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/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "<template>", line 1, in top-level template code
File "/usr/lib/python3.6/site-packages/salt/utils/jinja.py", line 237, in get_source
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: jinja_imported.j2
; line 1
---
{%- from 'jinja_imported.j2' import foo -%} <======================
what is the value: {{ foo.bar }}
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)
[...]
---
2022-08-22 10:31:51,733 salt.state DEBUG:An exception occurred in this state: Jinja error: jinja_imported.j2
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/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "<template>", line 1, in top-level template code
File "/usr/lib/python3.6/site-packages/salt/utils/jinja.py", line 237, in get_source
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: jinja_imported.j2
; line 1
---
{%- from 'jinja_imported.j2' import foo -%} <======================
what is the value: {{ foo.bar }}
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)
[...]
---
2022-08-22 10:31:51,744 salt.state ERROR: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/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "<template>", line 1, in top-level template code
File "/usr/lib/python3.6/site-packages/salt/utils/jinja.py", line 237, in get_source
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: jinja_imported.j2
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 548, in render_jinja_tmpl
"Jinja error: {}{}".format(exc, out), line, tmplstr, trace=tracestr
salt.exceptions.SaltRenderError: Jinja error: jinja_imported.j2
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/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "<template>", line 1, in top-level template code
File "/usr/lib/python3.6/site-packages/salt/utils/jinja.py", line 237, in get_source
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: jinja_imported.j2
This result can also be observed when looking at the resulting files in each minion cache directory:
note the missing minion_cache/rock/cache/files/base/jinja_imported.j2 file for the 'rock' minion.
Expected behavior
I expect that each minion will be able to download template files to their own cache.
Versions Report
salt --versions-report
Salt Version:
Salt: 3004.2Dependency Versions:
cffi: Not Installedcherrypy: Not Installeddateutil: Not Installeddocker-py: Not Installedgitdb: Not Installedgitpython: Not InstalledJinja2: 3.1.2libgit2: Not InstalledM2Crypto: Not InstalledMako: Not Installedmsgpack: 1.0.4msgpack-pure: Not Installedmysql-python: Not Installedpycparser: Not Installedpycrypto: Not Installedpycryptodome: 3.15.0pygit2: Not InstalledPython: 3.8.12 (default, Sep 21 2021, 00:10:52)python-gnupg: Not InstalledPyYAML: 6.0PyZMQ: 21.0.2smmap: Not Installedtimelib: Not InstalledTornado: 4.5.3ZMQ: 4.3.3System Versions:
dist: centos 8locale: utf-8machine: x86_64release: 4.18.0-373.el8.x86_64system: Linuxversion: CentOS Stream 8
Hi there! Welcome to the Salt Community! Thank you for making your first contribution. We have a lengthy process for issues and PRs. Someone from the Core Team will follow up as soon as possible. In the meantime, here’s some information that may help as you continue your Salt journey.
Please be sure to review our Code of Conduct. Also, check out some of our community resources including:
There are lots of ways to get involved in our community. Every month, there are around a dozen opportunities to meet with other contributors and the Salt Core team and collaborate in real time. The best way to keep track is by subscribing to the Salt Community Events Calendar.
If you have additional questions, email us at saltproject@vmware.com. We’re glad you’ve joined our community and look forward to doing awesome things with you!
jmarais
changed the title
[BUG] file client cache issue when instantiating two salt callers with different 'cachedir's
[BUG] utils/jinja file client cache issue when instantiating two salt callers with different 'cachedir's
Sep 5, 2022
Description
I instantiate two salt caller clients in the same python environment and it looks like the cached utils.jinja file_client is reused between them when resolving jinja imports.
This cause the second minion to fail when trying to resolve jinja imports. It seems like the file_client of the first salt minion is used and it validates that the imported file already exists in the first minion cachedir (and does not download the file to the second minion cache). The second minion will then produce an error because the file is not downloaded to the second minion cachedir. This also fail on the master branch.
I am trying to run salt states with
test=true
mode in order to validate salt/jinja file rendering before deploying. Each client is instantiated with all the 'cache' related config setup to point to a per minion directory.Issue traced down to the reuse here:
https://github.com/saltstack/salt/blob/master/salt/utils/jinja.py#L106-L116
Does adding:
to the if check there address the issue correctly?
Setup
Here is a repo with the example bug:
https://github.com/jmarais/salt-utils-jinja-cache-issue
It comes down to trying to file.manage a first jinja fail with an import:
jinja_main:
jinja_imported:
Steps to Reproduce the behavior
result:
end of log output
This result can also be observed when looking at the resulting files in each minion cache directory:
find minion_cache result
note the missing
minion_cache/rock/cache/files/base/jinja_imported.j2
file for the 'rock' minion.Expected behavior
I expect that each minion will be able to download template files to their own cache.
Versions Report
salt --versions-report
salt library __version__ number
Additional context
I am not sure if this is a bug or intended behaviour?
The text was updated successfully, but these errors were encountered: