Skip to content

Commit

Permalink
feat(map): YAML files must not use any Jinja
Browse files Browse the repository at this point in the history
For each `.yaml` file that `map.jinja` try to load, a `.yaml.jinja`
Jinja template is tried right after to permit jinja manipulation of
values and circumvent yamllint errors when using Jinja in YAML files.

At the end of the load of YAML files and Jinja YAML templates,
`map.jinja` include an optional `post-map.jinja` for post-processing
the `mapdata` variable.
  • Loading branch information
baby-gnu committed Feb 10, 2021
1 parent 329e413 commit a76c078
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
22 changes: 15 additions & 7 deletions openvpn/libmapstack.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@
{%- set yaml_names = [yaml_names] %}
{%- endif %}

{#- Try to load a `.yaml.jinja` file for each `.yaml` file #}
{%- set all_yaml_names = [] %}
{%- for name in yaml_names %}
{%- set extension = name.rpartition(".")[2] %}
{%- if extension not in ["yaml", "jinja"] %}
{%- do all_yaml_names.extend([name ~ ".yaml", name ~ ".yaml.jinja"]) %}
{%- elif extension == "yaml" %}
{%- do all_yaml_names.extend([name, name ~ ".jinja"]) %}
{%- else %}
{%- do all_yaml_names.append(name) %}
{%- endif %}
{%- endfor %}

{#- `yaml_dirname` can be an empty string with literal path like `myconf.yaml` #}
{%- set yaml_dir = [
param_dir,
Expand All @@ -204,15 +217,10 @@
| select
| join("/") %}

{%- for yaml_name in yaml_names %}
{#- Make sure to have a `.yaml` extension #}
{#- Use `.rpartition` to strip last `.yaml` in `dir.yaml/file.yaml` #}
{%- for yaml_name in all_yaml_names %}
{%- set yaml_filename = [
yaml_dir.rstrip("/"),
yaml_name.rpartition(".yaml")
| reject("equalto", ".yaml")
| join
~ ".yaml"
yaml_name
]
| select
| join("/") %}
Expand Down
5 changes: 5 additions & 0 deletions openvpn/map.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

{#- Load formula parameters values #}
{%- set _formula_matchers = ["defaults.yaml"] + map_sources %}

{%- set _formula_settings = mapstack(
matchers=_formula_matchers,
dirs=[formula_param_dir],
Expand All @@ -59,3 +60,7 @@

{%- do salt["log.debug"]("map.jinja: save parameters in variable 'mapdata'") %}
{%- set mapdata = _formula_settings["values"] %}

{#- Per formula post-processing of `mapdata` if it exists #}
{%- do salt["log.debug"]("map.jinja: post-processing of 'mapdata'") %}
{%- include tplroot ~ "/post-map.jinja" ignore missing %}
3 changes: 1 addition & 2 deletions openvpn/parameters/defaults.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# yamllint disable-file
# -*- coding: utf-8 -*-
# vim: ft=yaml
---
Expand All @@ -14,7 +13,7 @@ values:
group: nobody
# None, will default to 'user'
log_user: ~
multi_services: {{ salt['grains.has_value']('systemd') }}
multi_services: false
pkgs: ['openvpn']
service: openvpn
service_function: running
Expand Down
15 changes: 15 additions & 0 deletions openvpn/parameters/defaults.yaml.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{#- -*- coding: utf-8 -*- #}
{#- vim: ft=jinja #}
{#-
Setup variables after loading `defaults.yaml`.
This jinja2 file must return a valid `map.jinja` YAML which will
be merged into `defaults.yaml`.
If you do not need to provide calculated defaults, you can remove
this file or provide at least an empty dict, e.g. values: {}
#}
---
values:
multi_services: {{ salt['grains.has_value']('systemd') }}
...

0 comments on commit a76c078

Please sign in to comment.