Skip to content

Commit 0d6177c

Browse files
committed
fix(map.jinja): _merge_ defaults and config.get
test resulting config
1 parent c58fa7c commit 0d6177c

File tree

7 files changed

+67
-13
lines changed

7 files changed

+67
-13
lines changed

pillar.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
template:
55
lookup:
66
master: template-master
7+
# Just for testing purposes
8+
winner: lookup
9+
added_in_lookup: asdf
710

811
# Using bash package and udev service as an example. This allows us to
912
# test the template formula itself. You should set these parameters to
@@ -42,3 +45,11 @@ template:
4245
# - 'example_alt.tmpl'
4346
# - 'example_alt.tmpl.jinja'
4447

48+
# For testing purposes
49+
source_files:
50+
template-config-file-file-managed:
51+
- 'example.tmpl.jinja'
52+
53+
# Just for testing purposes
54+
winner: pillar
55+
added_in_pillar: asdf

template/config/file.sls

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ template-config-file-file-managed:
1919
}}
2020
- mode: 644
2121
- user: root
22-
- group: root
22+
- group: {{ template.rootgroup }}
2323
- template: jinja
2424
- require:
2525
- sls: {{ sls_package_install }}
26+
- context:
27+
template: {{ template | json}}

template/defaults.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
---
44
template:
55
pkg: template
6+
rootgroup: root
67
config: '/etc/template'
78
service:
89
name: template
10+
# Just here for testing
11+
added_in_defaults: asdf
12+
winner: defaults

template/files/default/example.tmpl.jinja

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@
44
########################################################################
55

66
This is another example file from SaltStack template-formula.
7+
8+
# This is here for testing purposes
9+
{{ template | json }}
10+
11+
winner of the merge: {{ template['winner'] }}

template/map.jinja

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,36 @@
99
{%- import_yaml tplroot ~ "/osmap.yaml" as osmap %}
1010
{%- import_yaml tplroot ~ "/osfingermap.yaml" as osfingermap %}
1111

12-
{%- set defaults = salt['grains.filter_by'](default_settings,
13-
default=tplroot,
14-
merge=salt['grains.filter_by'](osfamilymap, grain='os_family',
15-
merge=salt['grains.filter_by'](osmap, grain='os',
16-
merge=salt['grains.filter_by'](osfingermap, grain='osfinger',
17-
merge=salt['config.get'](tplroot ~ ':lookup', default={})
12+
{#- Retrieve the config dict only once #}
13+
{%- set _config = salt['config.get'](tplroot, default={}) %}
14+
15+
{%- set defaults = salt['grains.filter_by'](
16+
default_settings,
17+
default=tplroot,
18+
merge=salt['grains.filter_by'](
19+
osfamilymap,
20+
grain='os_family',
21+
merge=salt['grains.filter_by'](
22+
osmap,
23+
grain='os',
24+
merge=salt['grains.filter_by'](
25+
osfingermap,
26+
grain='osfinger',
27+
merge=salt['grains.filter_by'](
28+
_config,
29+
default='lookup'
30+
)
31+
)
1832
)
1933
)
2034
)
21-
) %}
35+
%}
36+
{%- set config = salt['grains.filter_by'](
37+
{'defaults': defaults},
38+
default='defaults',
39+
merge=_config
40+
)
41+
%}
2242

23-
{#- Merge the template config (e.g. from pillar) #}
24-
{%- set template = salt['config.get'](tplroot, default=defaults) %}
43+
{#- Change to whatever variable name you like #}
44+
{%- set template = config %}

template/osfamilymap.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ Arch:
3030

3131
Alpine: {}
3232

33-
FreeBSD: {}
33+
FreeBSD:
34+
rootgroup: wheel
3435

35-
OpenBSD: {}
36+
OpenBSD:
37+
rootgroup: wheel
3638

3739
Solaris: {}
3840

test/integration/default/controls/config_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
it { should be_owned_by 'root' }
77
it { should be_grouped_into 'root' }
88
its('mode') { should cmp '0644' }
9-
its('content') { should include 'This is an example file from SaltStack template-formula.' }
9+
its('content') { should include 'This is another example file from SaltStack template-formula.' }
10+
its('content') { should include '"added_in_pillar": "asdf"' }
11+
its('content') { should include '"added_in_defaults": "asdf"' }
12+
its('content') { should include '"added_in_lookup": "asdf"' }
13+
its('content') { should include '"config": "/etc/template-formula.conf"' }
14+
its('content') { should include '"lookup": {"added_in_lookup": "asdf",' }
15+
its('content') { should include '"pkg": "' }
16+
its('content') { should include '"service": {"name": "' }
17+
its('content') { should include '"tofs": {"files_switch": ["any/path/can/be/used/here", "id", "osfinger", "os", "os_family"], "source_files": {"template-config-file-file-managed": ["example.tmpl.jinja"]}' }
18+
its('content') { should include '"winner": "pillar"}' }
19+
its('content') { should include 'winner of the merge: pillar' }
1020
end
1121
end

0 commit comments

Comments
 (0)