0
0
mirror of https://github.com/saltstack-formulas/salt-formula.git synced 2024-11-28 22:25:51 +01:00
salt-formula/salt/formulas.jinja
Matthew X. Economou 827ed47a25 Filter all calls to formulas_git_opt through load_yaml
Jinja macros are not actually functions.  The only thing they can return
is a string.  In order to return structured data, the callee must
serialize it, and the caller must deserialize it.  This state formula
uses YAML as the intermediary, hence the occurrence of both the
`|yaml` (callee) and `|load_yaml` (caller) filters in its code.

The post-render "mapping values are not allowed here" error in
*salt/formulas.sls* or the broken rendering of
*salt/files/master.d/f_defaults.conf* happens because invocations of the
`formulas_git_opt` macro in several Jinja `set` statements do not get
deserialized, resulting in the trailing newline followed by three dot
characters (`...`), which YAML uses to signal the end of a document.
Correcting these rendering errors requires adding the necessary
deserialization code at those locations (i.e., filtering the macro call
through `|load_yaml`).
2015-09-03 14:56:45 -04:00

50 lines
1.3 KiB
Django/Jinja

{% set defaults = {
'baseurl': 'https://github.com/saltstack-formulas',
'basedir': '/srv/formulas',
'update': False,
'options': {},
}
%}
{% set formulas = salt['pillar.get']('salt_formulas:list', {}) %}
{%- macro formulas_git_opt(env, opt) -%}
{%- set value = salt['pillar.get']('salt_formulas:git_opts:{0}:{1}'.format(env, opt),
salt['pillar.get']('salt_formulas:git_opts:default:{0}'.format(opt),
defaults[opt])) -%}
{{ value|yaml }}
{%- endmacro -%}
{%- macro formulas_roots(env) -%}
{%- set value = [] -%}
{%- for dir in formulas.get(env, []) -%}
{%- set basedir = formulas_git_opt(env, 'basedir')|load_yaml -%}
{%- do value.append('{0}/{1}'.format(basedir, dir)) -%}
{%- endfor -%}
{{ value|yaml }}
{%- endmacro -%}
{# Generate file_roots config merging standard salt config and list of
enabled formulas #}
{%- macro file_roots(input) -%}
{%- set processed_envs = [] -%}
file_roots:
{%- for name, roots in input|dictsort -%}
{%- do processed_envs.append(name) %}
{{ name }}:
{%- for dir in roots %}
- {{ dir }}
{%- endfor -%}
{%- for dir in formulas_roots(name)|load_yaml %}
- {{ dir }}
{%- endfor -%}
{%- endfor -%}
{%- for name in formulas -%}
{%- if name not in processed_envs %}
{{ name }}:
{%- for dir in formulas_roots(name)|load_yaml %}
- {{ dir }}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{%- endmacro -%}