From 60d43e7feedcb0aa0b656d6a100d5e8fbea7c881 Mon Sep 17 00:00:00 2001 From: Daniel Dehennin Date: Thu, 14 Mar 2019 10:14:29 +0100 Subject: [PATCH] =?UTF-8?q?refactor(tofs):=20avoid=20using=20=E2=80=9Csalt?= =?UTF-8?q?['config.get']=E2=80=9D=20for=20formula=20writers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We can hide the call to “salt['config.get']” in the macro by only asking for a “lookup key” where to lookup the list of “source_files”. * docs/TOFS_pattern.rst (Example): document the use of the new optional “lookup” parameter. * template/macros.jinja: add a new optional “lookup” parameter. Lookup “files override” under the “:tofs:sources_files:” and fallback to “source_files” parameter. * template/config/file.sls (template-config-file-file-managed): use the new “lookup” parameter. --- docs/TOFS_pattern.rst | 10 ++++------ template/config/file.sls | 10 ++++------ template/macros.jinja | 19 ++++++++++++------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/docs/TOFS_pattern.rst b/docs/TOFS_pattern.rst index 318096a5..952e77c7 100644 --- a/docs/TOFS_pattern.rst +++ b/docs/TOFS_pattern.rst @@ -314,12 +314,10 @@ We can simplify the ``conf.sls`` with the new ``files_switch`` macro to use in t file.managed: - name: {{ ntp.config }} - template: jinja - - source: {{ files_switch( - salt['config.get']( - tplroot ~ ':tofs:source_files:Configure NTP', - ['/etc/ntp.conf.jinja'] - ) - ) }} + - source: {{ files_switch(['/etc/ntp.conf.jinja'], + lookup='Configure NTP' + ) + }} - watch_in: - service: Enable and start NTP service - require: diff --git a/template/config/file.sls b/template/config/file.sls index 48d55fa9..ce3eb6e6 100644 --- a/template/config/file.sls +++ b/template/config/file.sls @@ -13,12 +13,10 @@ include: template-config-file-file-managed: file.managed: - name: {{ template.config }} - - source: {{ files_switch( - salt['config.get']( - tplroot ~ ':tofs:source_files:template-config-file-file-managed', - ['example.tmpl', 'example.tmpl.jinja'] - ) - ) }} + - source: {{ files_switch(['example.tmpl', 'example.tmpl.jinja'], + lookup='template-config-file-file-managed' + ) + }} - mode: 644 - user: root - group: root diff --git a/template/macros.jinja b/template/macros.jinja index 26c804cb..018e63d6 100644 --- a/template/macros.jinja +++ b/template/macros.jinja @@ -1,4 +1,5 @@ {%- macro files_switch(source_files, + lookup=None, default_files_switch=['id', 'os_family'], indent_width=6) %} {#- @@ -8,6 +9,8 @@ Params: * source_files: ordered list of files to look for + * lookup: key under ':tofs:source_files' to override + list of source files * default_files_switch: if there's no pillar ':tofs:files_switch' this is the ordered list of grains to use as selector switch of the directories under @@ -21,11 +24,8 @@ Deploy configuration: file.managed: - name: /etc/yyy/zzz.conf - - source: {{ files_switch( - salt['config.get']( - tplroot ~ ':tofs:source_files:Deploy configuration', - ['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja'] - ) + - source: {{ files_switch(['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja'], + lookup='Deploy configuration' ) }} - template: jinja @@ -52,6 +52,11 @@ tplroot ~ ':tofs:files_switch', default_files_switch ) %} + {#- Lookup files or fallback to source_files parameter #} + {%- set src_files = salt['config.get']( + tplroot ~ ':tofs:source_files:' ~ lookup, + source_files + ) %} {#- Only add to [''] when supporting older TOFS implementations #} {%- for path_prefix_ext in [''] %} {%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %} @@ -66,7 +71,7 @@ {%- do fsl.append('') %} {%- endif %} {%- for fs in fsl %} - {%- for source_file in source_files %} + {%- for src_file in src_files %} {%- if fs %} {%- set fs_dir = salt['config.get'](fs, fs) %} {%- else %} @@ -76,7 +81,7 @@ path_prefix_inc_ext, files_dir, fs_dir, - source_file.lstrip('/') + src_file.lstrip('/') ]) %} {{ url | indent(indent_width, true) }} {%- endfor %}