1 {%- macro files_switch(
4 default_files_switch=["id", "os_family"],
9 Returns a valid value for the "source" parameter of a "file.managed"
10 state function. This makes easier the usage of the Template Override and
11 Files Switch (TOFS) pattern.
13 * source_files: ordered list of files to look for
14 * lookup: key under "<tplroot>:tofs:source_files" to prepend to the
16 * default_files_switch: if there's no config (e.g. pillar)
17 "<tplroot>:tofs:files_switch" this is the ordered list of grains to
18 use as selector switch of the directories under
20 * indent_width: indentation of the result value to conform to YAML
21 * use_subpath: defaults to `False` but if set, lookup the source file
22 recursively from the current state directory up to `tplroot`
23 Example (based on a `tplroot` of `xxx`):
27 - name: /etc/yyy/zzz.conf
28 - source: {{ files_switch(
29 ["/etc/yyy/zzz.conf", "/etc/yyy/zzz.conf.jinja"],
30 lookup="Deploy configuration",
33 In a minion with id=theminion and os_family=RedHat, it's going to be
37 - name: /etc/yyy/zzz.conf
39 - salt://xxx/files/theminion/etc/yyy/zzz.conf
40 - salt://xxx/files/theminion/etc/yyy/zzz.conf.jinja
41 - salt://xxx/files/RedHat/etc/yyy/zzz.conf
42 - salt://xxx/files/RedHat/etc/yyy/zzz.conf.jinja
43 - salt://xxx/files/default/etc/yyy/zzz.conf
44 - salt://xxx/files/default/etc/yyy/zzz.conf.jinja
47 {#- Get the `tplroot` from `tpldir` #}
48 {%- set tplroot = tpldir.split("/")[0] %}
49 {%- set path_prefix = salt["config.get"](tplroot ~ ":tofs:path_prefix", tplroot) %}
50 {%- set files_dir = salt["config.get"](tplroot ~ ":tofs:dirs:files", "files") %}
51 {%- set files_switch_list = salt["config.get"](
52 tplroot ~ ":tofs:files_switch", default_files_switch
54 {#- Lookup source_files (v2), files (v1), or fallback to an empty list #}
55 {%- set src_files = salt["config.get"](
56 tplroot ~ ":tofs:source_files:" ~ lookup,
57 salt["config.get"](tplroot ~ ":tofs:files:" ~ lookup, []),
59 {#- Append the default source_files #}
60 {%- set src_files = src_files + source_files %}
61 {#- Only add to [""] when supporting older TOFS implementations #}
62 {%- set path_prefix_exts = [""] %}
63 {%- if use_subpath and tplroot != tpldir %}
64 {#- Walk directory tree to find {{ files_dir }} #}
65 {%- set subpath_parts = tpldir.lstrip(tplroot).lstrip("/").split("/") %}
66 {%- for path in subpath_parts %}
67 {%- set subpath = subpath_parts[0 : loop.index] | join("/") %}
68 {%- do path_prefix_exts.append("/" ~ subpath) %}
71 {%- for path_prefix_ext in path_prefix_exts | reverse %}
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",
79 {#- Append an empty value to evaluate as `default` in the loop below #}
80 {%- if "" not in fsl %}
81 {%- set fsl = fsl + [""] %}
84 {%- for src_file in src_files %}
86 {%- set fs_dirs = salt["config.get"](fs, fs) %}
88 {%- set fs_dirs = salt["config.get"](
89 tplroot ~ ":tofs:dirs:default", "default"
92 {#- Force the `config.get` lookup result as a list where necessary #}
93 {#- since we need to also handle grains that are lists #}
94 {%- if fs_dirs is string %}
95 {%- set fs_dirs = [fs_dirs] %}
97 {%- for fs_dir in fs_dirs %}
98 {#- strip empty elements by using a select #}
102 path_prefix_inc_ext.strip("/"),
103 files_dir.strip("/"),
110 {{ url | indent(indent_width, true) }}