-
Notifications
You must be signed in to change notification settings - Fork 85
/
macros.jinja
83 lines (77 loc) · 3.12 KB
/
macros.jinja
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
{%- macro files_switch(files,
default_files_switch=['id', 'os_family'],
indent_width=6) %}
{#-
Returns a valid value for the "source" parameter of a "file.managed"
state function. This makes easier the usage of the Template Override and
Files Switch (TOFS) pattern.
Params:
* prefix: pillar prefix to custom ':files_switch'. Colons ':'
are replaced by '/' to be used as directory prefix (<path_prefix>)
* files: ordered list of files to look for
* default_files_switch: if there's no pillar
'<prefix>:files_switch' this is the ordered list of grains to
use as selector switch of the directories under
"<path_prefix>/files"
* indent_witdh: indentation of the result value to conform to YAML
Example:
If we have a state:
Deploy configuration:
file.managed:
- name: /etc/yyy/zzz.conf
- source: {{ files_switch('xxx', ['/etc/yyy/zzz.conf',
'/etc/yyy/zzz.conf.jinja']) }}
- template: jinja
In a minion with id=theminion and os_family=RedHat, it's going to be
rendered as:
Deploy configuration:
file.managed:
- name: /etc/yyy/zzz.conf
- source:
- salt://xxx/files/theminion/etc/yyy/zzz.conf
- salt://xxx/files/theminion/etc/yyy/zzz.conf.jinja
- salt://xxx/files/RedHat/etc/yyy/zzz.conf
- salt://xxx/files/RedHat/etc/yyy/zzz.conf.jinja
- salt://xxx/files/default/etc/yyy/zzz.conf
- salt://xxx/files/default/etc/yyy/zzz.conf.jinja
- template: jinja
#}
{#- Get the `topdir` from `tpldir` #}
{%- set topdir = tpldir.split('/')[0] %}
{%- set path_prefix = salt['config.get'](topdir ~ ':tofs:path_prefix', topdir) %}
{%- set files_dir = salt['config.get'](topdir ~ ':tofs:dirs:files', 'files') %}
{%- set files_switch_list = salt['config.get'](
topdir ~ ':tofs:files_switch',
default_files_switch
) %}
{#- Only add to [''] when supporting older TOFS implementations #}
{%- for path_prefix_ext in [''] %}
{%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
{#- For older TOFS implementation, use `files_switch` from the pillar #}
{#- Use the default, new method otherwise #}
{%- set fsl = salt['pillar.get'](
topdir ~ path_prefix_ext|replace('/', ':') ~ ':files_switch',
files_switch_list
) %}
{#- Append an empty value to evaluate as `default` in the loop below #}
{%- if '' not in fsl %}
{%- do fsl.append('') %}
{%- endif %}
{%- for fs in fsl %}
{%- for file in files %}
{%- if fs %}
{%- set fs_dir = salt['config.get'](fs, fs) %}
{%- else %}
{%- set fs_dir = salt['config.get'](topdir ~ ':tofs:dirs:default', 'default') %}
{%- endif %}
{%- set url = '- salt://' ~ '/'.join([
path_prefix_inc_ext,
files_dir,
fs_dir,
file.lstrip('/')
]) %}
{{ url | indent(indent_width, true) }}
{%- endfor %}
{%- endfor %}
{%- endfor %}
{%- endmacro %}