template-formula/TEMPLATE/map.jinja
Daniel Dehennin 42e19322c9 feat(map): update to v5 map.jinja
The v5 `map.jinja` is a generic and configurable system to load
configuration values, exposed as the `mapdata` variable, from
different places:

- YAML files and templates from the fileserver for non-secret data
- pillars or SDB are preferred for secret data
- grains or `config.get`

The `map.jinja` optional sources are configured with compound targeting like
syntax `[<TYPE>[:<OPTION>[:<DELIMITER>]]@]<KEY>` with the following
default ordered sources:

- `Y:G@osarch`: YAML file and Jinja template named after `osarch` grain
- `Y:G@os_family`: YAML file and Jinja template named after `os_family` grain
- `Y:G@os` YAML file and Jinja template named after `os` grain
- `Y:G@osfinger` YAML file and Jinja template named after `osfinger` grain
- `C@{{ tplroot ~ ':lookup' }}`: dict retrieved with `salt["config.get"]`
- `C@{{ tplroot }}`: dict retrieved with `salt["config.get"]`
- `Y:G@id`: YAML file and Jinja template named after `osarch` grain

This is done by two new libraries:

- `libmatchers.jinja` provides the `parse_matchers` macro to parse
  strings looking like compound matchers, for example `Y:G@osarch`

- `libmapstack.jinja` provides the `mapstack` macro to load
  configuration from different sources described by matchers

Post-processing of `mapdata` variable can be done in a `parameters/post-map.jinja`.

The v5 `map.jinja` is documented in `docs/map.jinja.rst`.

BREAKING CHANGE: `map.jinja` now exports a generic `mapdata` variable

BREAKING CHANGE: The per grain parameter values are now under `TEMPLATE/parameters/`
2021-02-16 11:10:09 +01:00

66 lines
1.9 KiB
Django/Jinja

# -*- coding: utf-8 -*-
# vim: ft=jinja
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split("/")[0] %}
{%- from tplroot ~ "/libmapstack.jinja" import mapstack %}
{#- Where to lookup parameters source files #}
{%- set formula_param_dir = tplroot ~ "/parameters" %}
{#- List of sources to lookup for parameters #}
{#- Fallback to previously used grains plus minion `id` #}
{%- set map_sources = [
"Y:G@osarch",
"Y:G@os_family",
"Y:G@os",
"Y:G@osfinger",
"C@" ~ tplroot ~ ":lookup",
"C@" ~ tplroot,
"Y:G@id",
] %}
{%- set _map_settings = mapstack(
matchers=["map_jinja.yaml"],
defaults={
"values": {"sources": map_sources}
},
log_prefix="map.jinja configuration: ",
)
| load_yaml %}
{%- set map_sources = _map_settings | traverse("values:sources") %}
{%- do salt["log.debug"](
"map.jinja: load parameters from sources:\n"
~ map_sources
| yaml(False)
) %}
{#- Load formula parameters values #}
{%- set _formula_matchers = ["defaults.yaml"] + map_sources %}
{%- set _formula_settings = mapstack(
matchers=_formula_matchers,
dirs=[formula_param_dir],
defaults={
"values": {},
"merge_strategy": salt["config.get"](tplroot ~ ":strategy", None),
"merge_lists": salt["config.get"](tplroot ~ ":merge_lists", False),
},
log_prefix="map.jinja: ",
)
| load_yaml %}
{#- Make sure to track `map.jinja` configuration with `_mapdata` #}
{%- do _formula_settings["values"].update(
{
"map_jinja": _map_settings["values"]
}
) %}
{%- 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 %}