|
| 1 | +{%- macro files_switch(source_files, |
| 2 | + lookup=None, |
| 3 | + default_files_switch=['id', 'os_family'], |
| 4 | + indent_width=6, |
| 5 | + v1_path_prefix='') %} |
| 6 | + {#- |
| 7 | + Returns a valid value for the "source" parameter of a "file.managed" |
| 8 | + state function. This makes easier the usage of the Template Override and |
| 9 | + Files Switch (TOFS) pattern. |
| 10 | +
|
| 11 | + Params: |
| 12 | + * source_files: ordered list of files to look for |
| 13 | + * lookup: key under '<tplroot>:tofs:source_files' to override |
| 14 | + list of source files |
| 15 | + * default_files_switch: if there's no config (e.g. pillar) |
| 16 | + '<tplroot>:tofs:files_switch' this is the ordered list of grains to |
| 17 | + use as selector switch of the directories under |
| 18 | + "<path_prefix>/files" |
| 19 | + * indent_witdh: indentation of the result value to conform to YAML |
| 20 | + * v1_path_prefix: (deprecated) only used for injecting a path prefix into |
| 21 | + the source, to support older TOFS configs |
| 22 | +
|
| 23 | + Example (based on a `tplroot` of `xxx`): |
| 24 | +
|
| 25 | + If we have a state: |
| 26 | +
|
| 27 | + Deploy configuration: |
| 28 | + file.managed: |
| 29 | + - name: /etc/yyy/zzz.conf |
| 30 | + - source: {{ files_switch(['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja'], |
| 31 | + lookup='Deploy configuration' |
| 32 | + ) }} |
| 33 | + - template: jinja |
| 34 | +
|
| 35 | + In a minion with id=theminion and os_family=RedHat, it's going to be |
| 36 | + rendered as: |
| 37 | +
|
| 38 | + Deploy configuration: |
| 39 | + file.managed: |
| 40 | + - name: /etc/yyy/zzz.conf |
| 41 | + - source: |
| 42 | + - salt://xxx/files/theminion/etc/yyy/zzz.conf |
| 43 | + - salt://xxx/files/theminion/etc/yyy/zzz.conf.jinja |
| 44 | + - salt://xxx/files/RedHat/etc/yyy/zzz.conf |
| 45 | + - salt://xxx/files/RedHat/etc/yyy/zzz.conf.jinja |
| 46 | + - salt://xxx/files/default/etc/yyy/zzz.conf |
| 47 | + - salt://xxx/files/default/etc/yyy/zzz.conf.jinja |
| 48 | + - template: jinja |
| 49 | + #} |
| 50 | + {#- Get the `tplroot` from `tpldir` #} |
| 51 | + {%- set tplroot = tpldir.split('/')[0] %} |
| 52 | + {%- set path_prefix = salt['config.get'](tplroot ~ ':tofs:path_prefix', tplroot) %} |
| 53 | + {%- set files_dir = salt['config.get'](tplroot ~ ':tofs:dirs:files', 'files') %} |
| 54 | + {%- set files_switch_list = salt['config.get']( |
| 55 | + tplroot ~ ':tofs:files_switch', |
| 56 | + default_files_switch |
| 57 | + ) %} |
| 58 | + {#- Lookup source_files (v2), files (v1), or fallback to source_files parameter #} |
| 59 | + {%- set src_files = salt['config.get']( |
| 60 | + tplroot ~ ':tofs:source_files:' ~ lookup, |
| 61 | + salt['config.get']( |
| 62 | + tplroot ~ ':tofs:files:' ~ lookup, |
| 63 | + source_files |
| 64 | + ) |
| 65 | + ) %} |
| 66 | + {#- Only add to [''] when supporting older TOFS implementations #} |
| 67 | + {%- set path_prefix_exts = [''] %} |
| 68 | + {%- if v1_path_prefix != '' %} |
| 69 | + {%- do path_prefix_exts.append(v1_path_prefix) %} |
| 70 | + {%- endif %} |
| 71 | + {%- for path_prefix_ext in path_prefix_exts %} |
| 72 | + {%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %} |
| 73 | + {#- For older TOFS implementation, use `files_switch` from the config #} |
| 74 | + {#- Use the default, new method otherwise #} |
| 75 | + {%- set fsl = salt['config.get']( |
| 76 | + tplroot ~ path_prefix_ext|replace('/', ':') ~ ':files_switch', |
| 77 | + files_switch_list |
| 78 | + ) %} |
| 79 | + {#- Append an empty value to evaluate as `default` in the loop below #} |
| 80 | + {%- if '' not in fsl %} |
| 81 | + {%- do fsl.append('') %} |
| 82 | + {%- endif %} |
| 83 | + {%- for fs in fsl %} |
| 84 | + {%- for src_file in src_files %} |
| 85 | + {%- if fs %} |
| 86 | + {%- set fs_dir = salt['config.get'](fs, fs) %} |
| 87 | + {%- else %} |
| 88 | + {%- set fs_dir = salt['config.get'](tplroot ~ ':tofs:dirs:default', 'default') %} |
| 89 | + {%- endif %} |
| 90 | + {%- set url = [ |
| 91 | + '- salt:/', |
| 92 | + path_prefix_inc_ext.strip('/'), |
| 93 | + files_dir.strip('/'), |
| 94 | + fs_dir.strip('/'), |
| 95 | + src_file.strip('/'), |
| 96 | + ] | select | join('/') %} |
| 97 | +{{ url | indent(indent_width, true) }} |
| 98 | + {%- endfor %} |
| 99 | + {%- endfor %} |
| 100 | + {%- endfor %} |
| 101 | +{%- endmacro %} |
0 commit comments