-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Can't use tpldir or slspath in template file #41195
Comments
When they said to use tpldir, did they mention where in salt that was documented? because i cannot find it in the documents at all :/ It might be just a jinja thing. Can you try using
I am looking into this further. Thanks, |
ahh, i see it referenced in |
I have tried
|
And about documentation, it isn't documented anywhere, only in code and in other issues. Mostly discussed here #4348. |
ok, let me take a look closer, because I see where it should be injecting it into the template environment in salt.utils.jinja. |
Thanks for reporting this. I have replicated the issue, and looking at the source this should definitely work. We attempt to add the tpldir stuff into the environment for jinja files here https://github.com/saltstack/salt/blob/2016.11/salt/utils/jinja.py#L106-L113 But it doesn't look like the jinja templates actually get run through there, instead they go through https://github.com/saltstack/salt/blob/develop/salt/utils/templates.py#L109-L129 And because the template is not an sls file, we never add the tpldir information to the template. So, even though the original commit that adds this wanted to add what you are requesting, it doesn't work. And from testing all the way back to that commit, it looks like it never worked. I am tagging this as a bug for our core team. Thanks for reporting. If you wanted to take a shot at fixing this, we would greatly appreciate it. Here are the jinja docs which should be how our cache loader is loaded and used for loading templates. http://jinja.pocoo.org/docs/2.9/api/#jinja2.BaseLoader.get_source Thanks, |
@gtmanfred thanks for analysis, I had the same assumption. But this assumption is best of my Python skills :) I will patiently wait for fix from Salt team. |
@gtmanfred I wonder if I was having the same problem when trying to use |
That would not surprise me at all.
…On Fri, May 12, 2017 at 5:29 AM, Loren Gordon ***@***.***> wrote:
@gtmanfred <https://github.com/gtmanfred> I wonder if I was having the
same problem when trying to use tpldir in a winrepo pkg definition?
#29063 <#29063>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#41195 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAssoWUVAgvXGY8lDNW9FNXG0Ra4AeB_ks5r5EKRgaJpZM4NYSEh>
.
|
Any progress here? |
Workaround described here: saltstack-formulas/mysql-formula#184 (comment) |
Where {#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{#- Start imports as #}
{%- import_yaml tplroot ~ "/defaults.yaml" as default_settings %}
{%- import_yaml tplroot ~ "/osfamilymap.yaml" as osfamilymap %}
{%- import_yaml tplroot ~ "/osmap.yaml" as osmap %}
{%- import_yaml tplroot ~ "/osfingermap.yaml" as osfingermap %} |
Working on making SaltStack Formulas portable, find the need for a common point of reference. Comparing the import of `map.jinja` with and without context: | `tpldata` | without context | with context | |-----------|--------------------|--------------------------| | `tplfile` | template/map.jinja | template/pkg/install.sls | | `tpldir` | template | template/pkg | | `tpldot` | template | template.pkg | | `tplroot` | template | template | With `tplroot`, it is possible to use a single point of reference for all paths, avoiding the various incantations currently used, such as: * `from tpldir ~ "/map.jinja" import ...` * `from salt.file.dirname(tpldir) ~ "/map.jinja" import ...` * `from salt['file.dirname'](tpldir) ~ "/map.jinja" import ...` In all of the above circumstances, this simply becomes: * `from tplroot ~ "/map.jinja" import ...` Additional benefits are gained when referencing by colon `:` for pillars or by period `.` for `include` statements or for any requisites. Related issues: saltstack#10838, saltstack#41195.
Working on making SaltStack Formulas portable, find the need for a common point of reference. Comparing the import of `map.jinja` with and without context: | `tpldata` | without context | with context | |-----------|--------------------|--------------------------| | `tplfile` | template/map.jinja | template/pkg/install.sls | | `tpldir` | template | template/pkg | | `tpldot` | template | template.pkg | | `tplroot` | template | template | With `tplroot`, it is possible to use a single point of reference for all paths, avoiding the various incantations currently used, such as: * `from tpldir ~ "/map.jinja" import ...` * `from salt.file.dirname(tpldir) ~ "/map.jinja" import ...` * `from salt['file.dirname'](tpldir) ~ "/map.jinja" import ...` In all of the above circumstances, this simply becomes: * `from tplroot ~ "/map.jinja" import ...` Additional benefits are gained when referencing by colon `:` for pillars or by period `.` for `include` statements or for any requisites. Related issues: saltstack#10838, saltstack#41195.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue. |
This is still present in |
Thank you for updating this issue. It is no longer marked as stale. |
@myii (salt-formulas org maintainer) informed me about a problem with the I'm not 100% familiar with the code (I just backported it to Salt Neon), but on a first glance it looks like the problem is caused by using the
(1) The above snippet gets saved into a tempfile (something like (2) The second part of the problem is that a (3) And the final issue is that these variables have different values depending on the presence of Below is a reproducible test case:
Below is how issues (2) and (3) mainfest themselves:
And below is the issue (1) with the new
I made an experimental patch to
However, the One potential workaround is to introduce the TLDR: salt-formulas need path-independent ways to reference CC: @terminalmage You wrote the original |
@max-arnold With regard to When I need to use these variables in a jinja template, I just pass them in the /etc/foo.conf:
file.managed:
- source: salt://apps/foo/foo.conf.jinja
- template: jinja
- context:
tpldir: {{ tpldir }} |
@terminalmage The discussion with @max-arnold started up again when I realised that the PR that was merged into Just quoting part of the description here:
We've been using this across the formulas now for some time using: {#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %} It's works with One problem was identified by @max-arnold above, though:
I suppose one way around this it to allow formula users to provide an override for What's your opinion about how this should proceed? Should |
@saltstack/docs-working-group |
@myii Sorry, I missed your question from several months ago. It seems that your |
Working on making SaltStack Formulas portable, find the need for a common point of reference. Comparing the import of `map.jinja` with and without context: | `tpldata` | without context | with context | |-----------|--------------------|--------------------------| | `tplfile` | template/map.jinja | template/pkg/install.sls | | `tpldir` | template | template/pkg | | `tpldot` | template | template.pkg | | `tplroot` | template | template | With `tplroot`, it is possible to use a single point of reference for all paths, avoiding the various incantations currently used, such as: * `from tpldir ~ "/map.jinja" import ...` * `from salt.file.dirname(tpldir) ~ "/map.jinja" import ...` * `from salt['file.dirname'](tpldir) ~ "/map.jinja" import ...` In all of the above circumstances, this simply becomes: * `from tplroot ~ "/map.jinja" import ...` Additional benefits are gained when referencing by colon `:` for pillars or by period `.` for `include` statements or for any requisites. Related issues: saltstack#10838, saltstack#41195.
Working on making SaltStack Formulas portable, find the need for a common point of reference. Comparing the import of `map.jinja` with and without context: | `tpldata` | without context | with context | |-----------|--------------------|--------------------------| | `tplfile` | template/map.jinja | template/pkg/install.sls | | `tpldir` | template | template/pkg | | `tpldot` | template | template.pkg | | `tplroot` | template | template | With `tplroot`, it is possible to use a single point of reference for all paths, avoiding the various incantations currently used, such as: * `from tpldir ~ "/map.jinja" import ...` * `from salt.file.dirname(tpldir) ~ "/map.jinja" import ...` * `from salt['file.dirname'](tpldir) ~ "/map.jinja" import ...` In all of the above circumstances, this simply becomes: * `from tplroot ~ "/map.jinja" import ...` Additional benefits are gained when referencing by colon `:` for pillars or by period `.` for `include` statements or for any requisites. Related issues: saltstack#10838, saltstack#41195.
For those of you who are watching this thread, can anyone tell me if the PR linked in this issue resolved this documentation issue or not? If so, I'll close it out. |
Hello @barbaricyawps
Sorry, which PR are you speaking about? So many things are linked to this. Regards. |
Can't use tpldir or slspath in template file
I want to load defaults with construction like this
Which work flawless in sls state file. But fails in template file. On #salt IRC channel I received a recommendation to use 'tpldir' variable instead, but it doesn't work as well.
Setup
init.sls
test.cfg.jinja
With setup like this I receive error:
Expected result is 'tpldir' will contain path to current template file.
Versions Report
The text was updated successfully, but these errors were encountered: