You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: docs/map.jinja.rst
+49-11Lines changed: 49 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -93,7 +93,7 @@ We create a configuration for the DNS domain ``example.net`` in ``/srv/salt/TEMP
93
93
config: /etc/template-formula-example-net.conf
94
94
...
95
95
96
-
We create another configuration for the DNS domain ``example.com`` in ``/srv/salt/TEMPLATE/parameters/dns:domain/example.com.yaml``:
96
+
We create another configuration for the DNS domain ``example.com`` in the Jinja YAML template ``/srv/salt/TEMPLATE/parameters/dns:domain/example.com.yaml.jinja``:
97
97
98
98
.. code-block:: yaml
99
99
@@ -263,15 +263,21 @@ Here is a valid example:
263
263
config: '/path/to/a/configuration/file'
264
264
...
265
265
266
-
You can use `Jinja`_ as with any SLS files:
267
266
268
-
.. code-block:: yaml
267
+
Using Jinja2 YAML template
268
+
``````````````````````````
269
+
270
+
You can provide a Jinja2 YAML template file with a name suffixed with ``.yaml.jinja``, it must produce a YAML file conform to the `Format of configuration YAML files`_, for example:
271
+
272
+
.. code-block:: jinja2
269
273
270
274
---
271
275
strategy: 'overwrite'
272
276
merge_lists: 'true'
273
277
values:
274
-
output_dir: /tmp/{{ grains['id'] }}
278
+
{%- if grains["os"] == "Debian" %}
279
+
output_dir: /tmp/{{ grains["id"] }}
280
+
{%- endif %}
275
281
...
276
282
277
283
@@ -285,13 +291,24 @@ The ``map.jinja`` file aggregates configuration values from several sources:
285
291
- `grains`_
286
292
- configuration gathered with `salt['config.get']`_
287
293
294
+
For the values loaded from YAML files, ``map.jinja`` will automatically try to load a Jinja2 template with the same name as the YAML file with the addition of the ``.jinja`` extension, for example ``foo/bar/quux.yaml.jinja``.
295
+
296
+
After loading values from all sources, it will try to include the ``salt://parameters/post-map.jinja`` Jinja file if it exists which can post-process the ``mapdata`` variable.
297
+
288
298
Configuring ``map.jinja`` sources
289
299
`````````````````````````````````
290
300
291
301
The ``map.jinja`` file uses several sources where to lookup parameter values. The list of sources can be configured in two places:
292
302
293
-
1. a global ``salt://parameters/map_jinja.yaml``
294
-
2. a per formula ``salt://{{ tplroot }}/parameters/map_jinja.yaml``, it overrides the global configuration
303
+
1. globally
304
+
305
+
1. with a plain YAML file ``salt://parameters/map_jinja.yaml``
306
+
2. with a Jinja2 YAML template file ``salt://parameters/map_jinja.yaml.jinja``
307
+
308
+
2. per formula
309
+
310
+
1. with a plain YAML file ``salt://{{ tplroot }}/parameters/map_jinja.yaml``
311
+
2. with a Jinja2 YAML template file ``salt://{{ tplroot }}/parameters/map_jinja.yaml.jinja``
295
312
296
313
.. note::
297
314
@@ -318,7 +335,10 @@ Finally, the ``<KEY>`` describes what to lookup to either build the YAML filenam
318
335
319
336
.. note::
320
337
321
-
For the YAML type, if the ``<KEY>`` can't be looked up, then it's used a literal string path to a YAML file, for example: ``any/path/can/be/used/here.yaml`` will result in the loading of ``salt://{{ tplroot }}/parameters/any/path/can/be/used/here.yaml`` if it exists.
338
+
For the YAML type:
339
+
340
+
- if the ``<KEY>`` can't be looked up, then it's used a literal string path to a YAML file, for example: ``any/path/can/be/used/here.yaml`` will result in the loading of ``salt://{{ tplroot }}/parameters/any/path/can/be/used/here.yaml`` if it exists
341
+
- ``map.jinja`` will automatically try to load a Jinja2 template, after the corresponding YAML file, with the same name as the YAML file extended with the ``.jinja`` extension, for example ``any/path/can/be/used/here.yaml.jinja``
322
342
323
343
The built-in ``map.jinja`` sources are:
324
344
@@ -332,19 +352,24 @@ The built-in ``map.jinja`` sources are:
332
352
- "C@{{ tplroot }}"
333
353
- "Y:G@id"
334
354
335
-
This is strictly equivalent to the following ``map_jinja.yaml`` using `Jinja`_:
355
+
This is strictly equivalent to the following ``map_jinja.yaml.jinja``:
@@ -356,14 +381,21 @@ For each configuration source defined, ``map.jinja`` will:
356
381
357
382
- for YAML file sources
358
383
359
-
- if the ``<KEY>`` can be looked up, load values from the YAML file named ``salt://{{ tplroot }}/paramaters/<KEY>/{{ salt['<QUERY_METHOD>']('<KEY>') }}.yaml`` if it exists
360
-
- otherwise, load the YAML file named ``salt://{{ tplroot }}/parameters/<KEY>.yaml`` if it exists
384
+
- if the ``<KEY>`` can be looked up:
385
+
386
+
- load values from the YAML file named ``salt://{{ tplroot }}/paramaters/<KEY>/{{ salt['<QUERY_METHOD>']('<KEY>') }}.yaml`` if it exists
387
+
- load values from the Jinja2 YAML template file named ``salt://{{ tplroot }}/paramaters/<KEY>/{{ salt['<QUERY_METHOD>']('<KEY>') }}.yaml.jinja`` if it exists
388
+
389
+
- otherwise:
390
+
391
+
- load the YAML file named ``salt://{{ tplroot }}/parameters/<KEY>.yaml`` if it exists
392
+
- load the Jinja2 YAML template file named ``salt://{{ tplroot }}/parameters/<KEY>.yaml.jinja`` if it exists
361
393
362
394
- for ``C``, ``G`` or ``I`` source type, lookup the value of ``salt['<QUERY_METHOD>']('<KEY>')``
363
395
364
396
#. merge the loaded values with the previous ones using `salt.slsutil.merge`_
365
397
366
-
There will be no error if a YAML file does not exists, they are all optional.
398
+
There will be no error if a YAML or Jinja2 file does not exists, they are all optional.
367
399
368
400
369
401
Configuration values from ``salt['config.get']``
@@ -383,13 +415,19 @@ Global view of the order of preferences
383
415
To summarise, here is a complete example of the load order of formula configuration values for an ``AMD64`` ``Ubuntu 18.04`` minion named ``minion1.example.net`` for the ``libvirt`` formula:
Remember that the order is important, for example, the value of ``key1:subkey1`` loaded from ``parameters/os_family/Debian.yaml`` is overridden by a value loaded from ``parameters/id/minion1.example.net.yaml``.
0 commit comments