@@ -329,96 +329,11 @@ We can simplify the ``conf.sls`` with the new ``files_switch`` macro to use in t
329
329
* This uses ``config.get ``\ , searching for ``nfs:tofs:files:Configure NTP `` to determine the list of template files to use.
330
330
* If this does not yield any results, the default of ``['/etc/ntp.conf.jinja'] `` will be used.
331
331
332
- In ``macros.jinja ``\ , we define this new macro ``files_switch ``.
332
+ In ``macros.jinja ``, we define this new macro ``files_switch ``.
333
333
334
- .. code-block :: jinja
335
-
336
- ## /srv/saltstack/salt-formulas/ntp-saltstack-formula/ntp/macros.jinja
337
- {%- macro files_switch(files,
338
- default_files_switch=['id', 'os_family'],
339
- indent_width=6) %}
340
- {#-
341
- Returns a valid value for the "source" parameter of a "file.managed"
342
- state function. This makes easier the usage of the Template Override and
343
- Files Switch (TOFS) pattern.
344
-
345
- Params:
346
- * files: ordered list of files to look for
347
- * default_files_switch: if there's no pillar
348
- '<tplroot>:tofs:files_switch' this is the ordered list of grains to
349
- use as selector switch of the directories under
350
- "<path_prefix>/files"
351
- * indent_witdh: indentation of the result value to conform to YAML
352
-
353
- Example (based on a `tplroot` of `xxx`):
354
-
355
- If we have a state:
356
-
357
- Deploy configuration:
358
- file.managed:
359
- - name: /etc/yyy/zzz.conf
360
- - source: {{ files_switch(
361
- salt['config.get'](
362
- tplroot ~ ':tofs:files:Deploy configuration',
363
- ['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja']
364
- )
365
- ) }}
366
- - template: jinja
367
-
368
- In a minion with id=theminion and os_family=RedHat, it's going to be
369
- rendered as:
370
-
371
- Deploy configuration:
372
- file.managed:
373
- - name: /etc/yyy/zzz.conf
374
- - source:
375
- - salt://xxx/files/theminion/etc/yyy/zzz.conf
376
- - salt://xxx/files/theminion/etc/yyy/zzz.conf.jinja
377
- - salt://xxx/files/RedHat/etc/yyy/zzz.conf
378
- - salt://xxx/files/RedHat/etc/yyy/zzz.conf.jinja
379
- - salt://xxx/files/default/etc/yyy/zzz.conf
380
- - salt://xxx/files/default/etc/yyy/zzz.conf.jinja
381
- - template: jinja
382
- #}
383
- {#- Get the `tplroot` from `tpldir` #}
384
- {%- set tplroot = tpldir.split('/')[0] %}
385
- {%- set path_prefix = salt['config.get'](tplroot ~ ':tofs:path_prefix', tplroot) %}
386
- {%- set files_dir = salt['config.get'](tplroot ~ ':tofs:dirs:files', 'files') %}
387
- {%- set files_switch_list = salt['config.get'](
388
- tplroot ~ ':tofs:files_switch',
389
- default_files_switch
390
- ) %}
391
- {#- Only add to [''] when supporting older TOFS implementations #}
392
- {%- for path_prefix_ext in [''] %}
393
- {%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
394
- {#- For older TOFS implementation, use `files_switch` from the pillar #}
395
- {#- Use the default, new method otherwise #}
396
- {%- set fsl = salt['pillar.get'](
397
- tplroot ~ path_prefix_ext|replace('/', ':') ~ ':files_switch',
398
- files_switch_list
399
- ) %}
400
- {#- Append an empty value to evaluate as `default` in the loop below #}
401
- {%- if '' not in fsl %}
402
- {%- do fsl.append('') %}
403
- {%- endif %}
404
- {%- for fs in fsl %}
405
- {%- for file in files %}
406
- {%- if fs %}
407
- {%- set fs_dir = salt['config.get'](fs, fs) %}
408
- {%- else %}
409
- {%- set fs_dir = salt['config.get'](tplroot ~ ':tofs:dirs:default', 'default') %}
410
- {%- endif %}
411
- {%- set url = '- salt://' ~ '/'.join([
412
- path_prefix_inc_ext,
413
- files_dir,
414
- fs_dir,
415
- file.lstrip('/')
416
- ]) %}
417
- {{ url | indent(indent_width, true) }}
418
- {%- endfor %}
419
- {%- endfor %}
420
- {%- endfor %}
421
- {%- endmacro %}
334
+ .. literalinclude :: ../template/macros.jinja
335
+ :caption: /srv/saltstack/salt-formulas/ntp-saltstack-formula/ntp/macros.jinja
336
+ :language: jinja
422
337
423
338
How to customise the ``source `` further
424
339
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
0 commit comments