diff --git a/docs/map.jinja.rst b/docs/map.jinja.rst index 3a75e57..1868ed5 100644 --- a/docs/map.jinja.rst +++ b/docs/map.jinja.rst @@ -28,7 +28,7 @@ For formula users Quick start: configure per role and per DNS domain name values ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -We will see a quick setup to configure the ``TEMPLATE`` formula for different DNS domain name and several roles. +We will see a quick setup to configure the ``TEMPLATE`` formula for different DNS domain names and several roles. For this example, I'll define 2 kinds of `fileserver`_ sources: @@ -93,7 +93,7 @@ We create a configuration for the DNS domain ``example.net`` in ``/srv/salt/TEMP config: /etc/template-formula-example-net.conf ... -We create another configuration for the DNS domain ``example.com`` in ``/srv/salt/TEMPLATE/parameters/dns:domain/example.com.yaml``: +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``: .. code-block:: yaml @@ -149,29 +149,28 @@ We create the global parameters file ``/srv/salt/parameters/map_jinja.yaml``: --- values: - map_jinja: - sources: - # default values - - "Y:G@osarch" - - "Y:G@os_family" - - "Y:G@os" - - "Y:G@osfinger" - - "C@{{ tplroot ~ ':lookup' }}" - - "C@{{ tplroot }}" - - # Roles activate/deactivate things - # then thing are configured depending on environment - # So roles comes before `dns:domain`, `domain` and `id` - - "Y:C@roles" - - # DNS domain configured (DHCP or resolv.conf) - - "Y:G@dns:domain" - - # Based on minion ID - - "Y:G@domain" - - # default values - - "Y:G@id" + sources: + # default values + - "Y:G@osarch" + - "Y:G@os_family" + - "Y:G@os" + - "Y:G@osfinger" + - "C@{{ tplroot ~ ':lookup' }}" + - "C@{{ tplroot }}" + + # Roles activate/deactivate things + # then thing are configured depending on environment + # So roles comes before `dns:domain`, `domain` and `id` + - "Y:C@roles" + + # DNS domain configured (DHCP or resolv.conf) + - "Y:G@dns:domain" + + # Based on minion ID + - "Y:G@domain" + + # default values + - "Y:G@id" ... The syntax is explained later at `Sources of configuration values`_. @@ -196,7 +195,7 @@ For the clients: .. note:: - Since we used ``Y:C@roles``, ``map.jinja`` will do a ``salt['config.get']('roles')`` to retrieve the roles so you could use any other method to bind roles to minions (`pillars`_ or `SDB`_) but `grains`_ seems to be the prefered method. + Since we used ``Y:C@roles``, ``map.jinja`` will do a ``salt['config.get']('roles')`` to retrieve the roles so you could use any other method to bind roles to minions (`pillars`_ or `SDB`_) but `grains`_ seems to be the preferred method. Note for Microsoft Windows systems `````````````````````````````````` @@ -209,29 +208,28 @@ Modify ``/srv/salt/parameters/map_jinja.yaml`` to change the query for ``dns:dom --- values: - map_jinja: - sources: - # default values - - "Y:G@osarch" - - "Y:G@os_family" - - "Y:G@os" - - "Y:G@osfinger" - - "C@{{ tplroot ~ ':lookup' }}" - - "C@{{ tplroot }}" - - # Roles activate/deactivate things - # then thing are configured depending on environment - # So roles comes before `dns:domain`, `domain` and `id` - - "Y:C@roles" - - # DNS domain configured (DHCP or resolv.conf) - - "Y:G:!@dns!domain" - - # Based on minion ID - - "Y:G@domain" - - # default values - - "Y:G@id" + sources: + # default values + - "Y:G@osarch" + - "Y:G@os_family" + - "Y:G@os" + - "Y:G@osfinger" + - "C@{{ tplroot ~ ':lookup' }}" + - "C@{{ tplroot }}" + + # Roles activate/deactivate things + # then thing are configured depending on environment + # So roles comes before `dns:domain`, `domain` and `id` + - "Y:C@roles" + + # DNS domain configured (DHCP or resolv.conf) + - "Y:G:!@dns!domain" + + # Based on minion ID + - "Y:G@domain" + + # default values + - "Y:G@id" ... And then, rename the directory: @@ -265,29 +263,56 @@ Here is a valid example: config: '/path/to/a/configuration/file' ... -You can use `Jinja`_ as with any SLS files: -.. code-block:: yaml +Using Jinja2 YAML template +`````````````````````````` + +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: + +.. code-block:: jinja2 --- strategy: 'overwrite' merge_lists: 'true' values: - output_dir: /tmp/{{ grains['id'] }} + {%- if grains["os"] == "Debian" %} + output_dir: /tmp/{{ grains["id"] }} + {%- endif %} ... Sources of configuration values ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +The ``map.jinja`` file aggregates configuration values from several sources: + +- YAML files stored in the `fileserver`_ +- `pillars`_ +- `grains`_ +- configuration gathered with `salt['config.get']`_ + +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``. + +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. Configuring ``map.jinja`` sources ````````````````````````````````` -The ``map.jinja`` file uses several sources where to lookup parameter values. The list of sources can be modified by two files: +The ``map.jinja`` file uses several sources where to lookup parameter values. The list of sources can be configured in two places: + +1. globally + + 1. with a plain YAML file ``salt://parameters/map_jinja.yaml`` + 2. with a Jinja2 YAML template file ``salt://parameters/map_jinja.yaml.jinja`` + +2. per formula -1. a global ``salt://parameters/map_jinja.yaml`` -2. a per formula ``salt://{{ tplroot }}/parameters/map_jinja.yaml``, it overrides the global configuration + 1. with a plain YAML file ``salt://{{ tplroot }}/parameters/map_jinja.yaml`` + 2. with a Jinja2 YAML template file ``salt://{{ tplroot }}/parameters/map_jinja.yaml.jinja`` + +.. note:: + + The ``map.jinja`` configuration files must conform to the `format of configuration YAML files`_. Each source definition has the form ``[[: