saltstack-formulas / template-formula

SaltStack formula template filled with dummy content

Home Page:http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

or {} changes don't work for me

gunix-de opened this issue ยท comments

The changes for default empty dictionary don't work for me,...

{%- import_yaml topdir ~ "/defaults.yaml" or {} as default_settings %}
{%- import_yaml topdir ~ "/osfamilymap.yaml" or {} as osfamilymap %}
{%- import_yaml topdir ~ "/osmap.yaml" or {} as osmap %}
{%- import_yaml topdir ~ "/osfingermap.yaml" or {} as osfingermap %}

do I need a special version of a component?

@gunix-de We stopped using topdir some time ago. We now use:

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{#- Start imports as #}
{%- import_yaml tplroot ~ "/defaults.yaml" or {} as default_settings %}
{%- import_yaml tplroot ~ "/osfamilymap.yaml" or {} as osfamilymap %}
{%- import_yaml tplroot ~ "/osmap.yaml" or {} as osmap %}
{%- import_yaml tplroot ~ "/osfingermap.yaml" or {} as osfingermap %}

Yes, you are right,... but what I mean is:
" or {} "
is not working for me,...

@gunix-de The only purpose of or {} is if someone wants to remove that file entirely. Otherwise, you'll need to supply values (or the empty dictionary) in the file itself.

yes, got it - but it does not work. If file is missing, the variables f.e. osfamilymap is None , not as expected {}

@gunix-de If you can point me to an actual example, it will be easier to comment.

@myii if the file is missing it breaks with error :

local:
   Data failed to compile:
----------
   Rendering SLS 'base:rkhunter.config.file' failed: Jinja error: rkhunter/osmap.yaml
/tmp/kitchen/var/cache/salt/minion/files/base/rkhunter/map.jinja(9):
---
[...]
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split('/')[0] %}
{#- Start imports as #}
{%- import_yaml tplroot ~ "/defaults.yaml" or {} as default_settings %}
{%- import_yaml tplroot ~ "/osfamilymap.yaml" or {} as osfamilymap %}
{%- import_yaml tplroot ~ "/osmap.yaml" or {} as osmap %}    <======================
{%- import_yaml tplroot ~ "/osfingermap.yaml" or {} as osfingermap %}

{%- set defaults = salt['grains.filter_by'](default_settings,
 default='rkhunter',
 merge=salt['grains.filter_by'](osfamilymap, grain='os_family',
[...]
---
Traceback (most recent call last):
 File "/usr/lib/python3/dist-packages/salt/utils/templates.py", line 392, in render_jinja_tmpl
   output = template.render(**decoded_context)
 File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 1008, in render
   return self.environment.handle_exception(exc_info, True)
 File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 780, in handle_exception
   reraise(exc_type, exc_value, tb)
 File "/usr/lib/python3/dist-packages/jinja2/_compat.py", line 37, in reraise
   raise value.with_traceback(tb)
 File "<template>", line 7, in top-level template code
 File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 1073, in make_module
   return TemplateModule(self, self.new_context(vars, shared, locals))
 File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 1152, in __init__
   body_stream = list(template.root_render_func(context))
 File "/tmp/kitchen/var/cache/salt/minion/files/base/rkhunter/map.jinja", line 9, in top-level template code
   {%- import_yaml tplroot ~ "/osmap.yaml" or {} as osmap %}
 File "/usr/lib/python3/dist-packages/salt/utils/jinja.py", line 171, in get_source
   raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: rkhunter/osmap.yaml

In merged PR #60, which was tested, the comments state:

# This file can be safely removed if you do not need to provide defaults via
# the `os_family` grain.
# This file can be safely removed if you do not need to provide defaults via
# the `os_finger` grain.
# This file can be safely removed if you do not need to provide defaults via
# the `os` grain.

I've also confirmed this at my end by moving the files and letting the or {} run. But perhaps there are some other reasons why it is failing in different setups. This will need further investigation. @daks Perhaps we can test this in Travis (with the files removed).

@n-rodriguez As discussed in the Slack/IRC/Matrix room, what you've mentioned is a context issue and not due to or {}.

So I've done further testing and the main minion I test on actually works with or {} for some reason. But I can't reproduce that with other minions so I'm confirming this issue.

However, it's not that or {} is failing -- it's that it's not making any difference at all. If the template is missing, it's missing. So we can remove the or {} but we still have the problem of how to deal with missing templates.

I have a long-winded solution, where we check for the existence of each file before importing it. I'm not sure it's worth the effort. The best resolution I can think of is to simply have an empty dict in the files that aren't required. And then the comments need to be updated in each file here.

Ultimately, thanks for reporting this issue @gunix-de -- sorry I can't find you a better solution. Empty dicts will have to do, at least for the time being.

The linked PR #94 will close this issue once merged. Thanks again @gunix-de.

๐ŸŽ‰ This issue has been resolved in version 2.0.3 ๐ŸŽ‰

The release is available on GitHub release

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€