3 TOFS: A pattern for using SaltStack
4 ===================================
22 - daniel.dehennin@baby-gnu.org
26 - https://github.com/myii
29 All that follows is a proposal based on my experience with `SaltStack <http://www.saltstack.com/>`_. The good thing of a piece of software like this is that you can "bend it" to suit your needs in many possible ways, and this is one of them. All the recommendations and thoughts are given "as it is" with no warranty of any type.
31 .. contents:: **Table of Contents**
33 Usage of values in pillar vs templates in ``file_roots``
34 --------------------------------------------------------
36 Among other functions, the *master* (or *salt-master*) serves files to the *minions* (or *salt-minions*). The `file_roots <http://docs.saltstack.com/en/latest/ref/file_server/file_roots.html>`_ is the list of directories used in sequence to find a file when a minion requires it: the first match is served to the minion. Those files could be `state files <http://docs.saltstack.com/en/latest/topics/tutorials/starting_states.html>`_ or configuration templates, among others.
38 Using SaltStack is a simple and effective way to implement configuration management, but even in a `non-multitenant <http://en.wikipedia.org/wiki/Multitenancy>`_ scenario, it is not a good idea to generally access some data (e.g. the database password in our `Zabbix <http://www.zabbix.com/>`_ server configuration file or the private key of our `Nginx <http://nginx.org/en/>`_ TLS certificate).
40 To avoid this situation we can use the `pillar mechanism <http://docs.saltstack.com/en/latest/topics/pillar/>`_, which is designed to provide controlled access to data from the minions based on some selection rules. As pillar data could be easily integrated in the `Jinja <http://docs.saltstack.com/en/latest/topics/tutorials/pillar.html>`_ templates, it is a good mechanism to store values to be used in the final rendering of state files and templates.
42 There are a variety of approaches on the usage of pillar and templates as seen in the `saltstack-formulas <https://github.com/saltstack-formulas>`_' repositories. `Some <https://github.com/saltstack-formulas/nginx-formula/pull/18>`_ `developments <https://github.com/saltstack-formulas/php-formula/pull/14>`_ stress the initial purpose of pillar data into a storage for most of the possible variables for a determined system configuration. This, in my opinion, is shifting too much load from the original template files approach. Adding up some `non-trivial Jinja <https://github.com/saltstack-formulas/nginx-formula/blob/f74254c07e188bd448eaf1c5f9c802d78c4c005e/nginx/files/default/nginx.conf>`_ code as essential part of composing the state file definitely makes SaltStack state files (hence formulas) more difficult to read. The extreme of this approach is that we could end up with a new render mechanism, implemented in Jinja, storing everything needed in pillar data to compose configurations. Additionally, we are establishing a strong dependency with the Jinja renderer.
44 In opposition to the *put the code in file_roots and the data in pillars* approach, there is the *pillar as a store for a set of key-values* approach. A full-blown configuration file abstracted in pillar and jinja is complicated to develop, understand and maintain. I think a better and simpler approach is to keep a configuration file templated using just a basic (non-extensive but extensible) set of pillar values.
46 On the reusability of SaltStack state files
47 -------------------------------------------
49 There is a brilliant initiative of the SaltStack community called `salt-formulas <https://github.com/saltstack-formulas>`_. Their goal is to provide state files, pillar examples and configuration templates ready to be used for provisioning. I am a contributor for two small ones: `zabbix-formula <https://github.com/saltstack-formulas/zabbix-formula>`_ and `varnish-formula <https://github.com/saltstack-formulas/varnish-formula>`_.
51 The `design guidelines <http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html>`_ for formulas are clear in many aspects and it is a recommended reading for anyone willing to write state files, even non-formulaic ones.
53 In the next section, I am going to describe my proposal to extend further the reusability of formulas, suggesting some patterns of usage.
55 The Template Override and Files Switch (TOFS) pattern
56 -----------------------------------------------------
58 I understand a formula as a **complete, independent set of SaltStack state and configuration template files sufficient to configure a system**. A system could be something as simple as an NTP server or some other much more complex service that requires many state and configuration template files.
60 The customization of a formula should be done mainly by providing pillar data used later to render either the state or the configuration template files.
62 Example: NTP before applying TOFS
63 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
65 Let's work with the NTP example. A basic formula that follows the `design guidelines <http://docs.saltstack.com/en/latest/topics/development/conventions/formulas.html>`_ has the following files and directories tree:
67 .. code-block:: console
69 /srv/saltstack/salt-formulas/ntp-saltstack-formula/
79 In order to use it, let's assume a `masterless configuration <http://docs.saltstack.com/en/latest/topics/tutorials/quickstart.html>`_ and this relevant section of ``/etc/salt/minion``:
85 - /srv/saltstack/pillar
90 - /srv/saltstack/salt-formulas/ntp-saltstack-formula
94 {#- /srv/saltstack/salt-formulas/ntp-saltstack-formula/ntp/map.jinja #}
95 {%- set ntp = salt['grains.filter_by']({
99 'config': '/etc/ntp.conf',
101 }, merge=salt['pillar.get']('ntp:lookup')) %}
103 In ``init.sls`` we have the minimal states required to have NTP configured. In many cases ``init.sls`` is almost equivalent to an ``apt-get install`` or a ``yum install`` of the package.
107 ## /srv/saltstack/salt-formulas/ntp-saltstack-formula/ntp/init.sls
108 {%- from 'ntp/map.jinja' import ntp with context %}
112 - name: {{ ntp.pkg }}
114 Enable and start NTP:
116 - name: {{ ntp.service }}
119 - pkg: Install NTP package
121 In ``conf.sls`` we have the configuration states. In most cases, that is just managing configuration file templates and making them to be watched by the service.
125 ## /srv/saltstack/salt-formulas/ntp-saltstack-formula/ntp/conf.sls
129 {%- from 'ntp/map.jinja' import ntp with context %}
133 - name: {{ ntp.config }}
135 - source: salt://ntp/files/default/etc/ntp.conf.jinja
137 - service: Enable and start NTP service
139 - pkg: Install NTP package
141 Under ``files/default``, there is a structure that mimics the one in the minion in order to avoid clashes and confusion on where to put the needed templates. There you can find a mostly standard template for the configuration file.
143 .. code-block:: jinja
145 {#- /srv/saltstack/salt-formulas/ntp-saltstack-formula/ntp/files/default/etc/ntp.conf.jinja #}
146 {#- Managed by saltstack #}
147 {#- Edit pillars or override this template in saltstack if you need customization #}
148 {%- set settings = salt['pillar.get']('ntp', {}) %}
149 {%- set default_servers = ['0.ubuntu.pool.ntp.org',
150 '1.ubuntu.pool.ntp.org',
151 '2.ubuntu.pool.ntp.org',
152 '3.ubuntu.pool.ntp.org'] %}
154 driftfile /var/lib/ntp/ntp.drift
155 statistics loopstats peerstats clockstats
156 filegen loopstats file loopstats type day enable
157 filegen peerstats file peerstats type day enable
158 filegen clockstats file clockstats type day enable
160 {%- for server in settings.get('servers', default_servers) %}
164 restrict -4 default kod notrap nomodify nopeer noquery
165 restrict -6 default kod notrap nomodify nopeer noquery
170 With all this, it is easy to install and configure a simple NTP server by just running ``salt-call state.sls ntp.conf``: the package will be installed, the service will be running and the configuration should be correct for most of cases, even without pillar data.
172 Alternatively, you can define a highstate in ``/srv/saltstack/salt/top.sls`` and run ``salt-call state.highstate``.
176 ## /srv/saltstack/salt/top.sls
181 **Customizing the formula just with pillar data**, we have the option to define the NTP servers.
185 ## /srv/saltstack/pillar/top.sls
192 ## /srv/saltstack/pillar/ntp.sls
203 If the customization based on pillar data is not enough, we can override the template by creating a new one in ``/srv/saltstack/salt/ntp/files/default/etc/ntp.conf.jinja``
205 .. code-block:: jinja
207 {#- /srv/saltstack/salt/ntp/files/default/etc/ntp.conf.jinja #}
208 {#- Managed by saltstack #}
209 {#- Edit pillars or override this template in saltstack if you need customization #}
211 {#- Some bizarre configurations here #}
214 {%- for server in settings.get('servers', default_servers) %}
218 This way we are locally **overriding the template files** offered by the formula in order to make a more complex adaptation. Of course, this could be applied as well to any of the files, including the state files.
223 To bring some order into the set of template files included in a formula, as we commented, we suggest having a similar structure to a normal final file system under ``files/default``.
225 We can make different templates coexist for different minions, classified by any `grain <http://docs.saltstack.com/en/latest/topics/targeting/grains.html>`_ value, by simply creating new directories under ``files``. This mechanism is based on **using values of some grains as a switch for the directories under** ``files/``.
227 If we decide that we want ``os_family`` as switch, then we could provide the formula template variants for both the ``RedHat`` and ``Debian`` families.
229 .. code-block:: console
231 /srv/saltstack/salt-formulas/ntp-saltstack-formula/ntp/files/
242 To make this work we need a ``conf.sls`` state file that takes a list of possible files as the configuration template.
246 ## /srv/saltstack/salt-formulas/ntp-saltstack-formula/ntp/conf.sls
250 {%- from 'ntp/map.jinja' import ntp with context %}
254 - name: {{ ntp.config }}
257 - salt://ntp/files/{{ grains.get('os_family', 'default') }}/etc/ntp.conf.jinja
258 - salt://ntp/files/default/etc/ntp.conf.jinja
260 - service: Enable and start NTP service
262 - pkg: Install NTP package
264 If we want to cover the possibility of a special template for a minion identified by ``node01`` then we could have a specific template in ``/srv/saltstack/salt/ntp/files/node01/etc/ntp.conf.jinja``.
266 .. code-block:: jinja
268 {#- /srv/saltstack/salt/ntp/files/node01/etc/ntp.conf.jinja #}
269 {#- Managed by saltstack #}
270 {#- Edit pillars or override this template in saltstack if you need customization #}
272 {#- Some crazy configurations here for node01 #}
275 To make this work we could write a specially crafted ``conf.sls``.
279 ## /srv/saltstack/salt-formulas/ntp-saltstack-formula/ntp/conf.sls
283 {%- from 'ntp/map.jinja' import ntp with context %}
287 - name: {{ ntp.config }}
290 - salt://ntp/files/{{ grains.get('id') }}/etc/ntp.conf.jinja
291 - salt://ntp/files/{{ grains.get('os_family') }}/etc/ntp.conf.jinja
292 - salt://ntp/files/default/etc/ntp.conf.jinja
294 - service: Enable and start NTP service
296 - pkg: Install NTP package
298 Using the ``files_switch`` macro
299 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
301 We can simplify the ``conf.sls`` with the new ``files_switch`` macro to use in the ``source`` parameter for the ``file.managed`` state.
305 ## /srv/saltstack/salt-formulas/ntp-saltstack-formula/ntp/conf.sls
309 {%- set tplroot = tpldir.split('/')[0] %}
310 {%- from 'ntp/map.jinja' import ntp with context %}
311 {%- from 'ntp/libtofs.jinja' import files_switch %}
315 - name: {{ ntp.config }}
317 - source: {{ files_switch(['/etc/ntp.conf.jinja'],
318 lookup='Configure NTP'
322 - service: Enable and start NTP service
324 - pkg: Install NTP package
327 * This uses ``config.get``, searching for ``ntp:tofs:source_files:Configure NTP`` to determine the list of template files to use.
328 * If this returns a result, the default of ``['/etc/ntp.conf.jinja']`` will be appended to it.
329 * If this does not yield any results, the default of ``['/etc/ntp.conf.jinja']`` will be used.
331 In ``libtofs.jinja``, we define this new macro ``files_switch``.
333 .. literalinclude:: ../template/libtofs.jinja
334 :caption: /srv/saltstack/salt-formulas/ntp-saltstack-formula/ntp/libtofs.jinja
337 How to customise the ``source`` further
338 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
340 The examples below are based on an ``Ubuntu`` minion called ``theminion`` being configured via. pillar.
342 Using the default settings of the ``files_switch`` macro above,
343 the ``source`` will be:
348 - salt://ntp/files/theminion/etc/ntp.conf.jinja
349 - salt://ntp/files/Debian/etc/ntp.conf.jinja
350 - salt://ntp/files/default/etc/ntp.conf.jinja
355 The ``files`` portion can be customised:
369 - salt://ntp/files_alt/theminion/etc/ntp.conf.jinja
370 - salt://ntp/files_alt/Debian/etc/ntp.conf.jinja
371 - salt://ntp/files_alt/default/etc/ntp.conf.jinja
373 Customise the use of grains
374 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
376 Grains can be customised and even arbitrary paths can be supplied:
383 - any/path/can/be/used/here
393 - salt://ntp/files/any/path/can/be/used/here/etc/ntp.conf.jinja
394 - salt://ntp/files/theminion/etc/ntp.conf.jinja
395 - salt://ntp/files/Ubuntu/etc/ntp.conf.jinja
396 - salt://ntp/files/Debian/etc/ntp.conf.jinja
397 - salt://ntp/files/default/etc/ntp.conf.jinja
399 Customise the ``default`` path
400 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
402 The ``default`` portion of the path can be customised:
417 - salt://ntp/files/default_alt/etc/ntp.conf.jinja
419 Customise the list of ``source_files``
420 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
422 The list of ``source_files`` can be given:
430 - '/etc/ntp.conf_alt.jinja'
437 - salt://ntp/files/theminion/etc/ntp.conf_alt.jinja
438 - salt://ntp/files/theminion/etc/ntp.conf.jinja
439 - salt://ntp/files/Debian/etc/ntp.conf_alt.jinja
440 - salt://ntp/files/Debian/etc/ntp.conf.jinja
441 - salt://ntp/files/default/etc/ntp.conf_alt.jinja
442 - salt://ntp/files/default/etc/ntp.conf.jinja
444 Note: This does *not* override the default value.
445 Rather, the value from the pillar/config is prepended to the default.
447 Using sub-directories for ``components``
448 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
450 If your formula is composed of several components, you may prefer to provides files under sub-directories, like in the `systemd-formula <https://github.com/saltstack-formulas/systemd-formula>`_.
452 .. code-block:: console
454 /srv/saltstack/systemd-formula/
482 For example, the following ``formula.component.config`` SLS:
486 {%- from "formula/libtofs.jinja" import files_switch with context %}
488 formula configuration file:
490 - name: /etc/formula.conf
495 - source: {{ files_switch(['formula.conf'],
501 will be rendered on a ``Debian`` minion named ``salt-formula.ci.local`` as:
505 formula configuration file:
507 - name: /etc/formula.conf
513 - salt://formula/component/files/salt-formula.ci.local/formula.conf
514 - salt://formula/component/files/Debian/formula.conf
515 - salt://formula/component/files/default/formula.conf
516 - salt://formula/files/salt-formula.ci.local/formula.conf
517 - salt://formula/files/Debian/formula.conf
518 - salt://formula/files/default/formula.conf