From 91bc2f046b3629b9f1f66ed0eacddb626348da6c Mon Sep 17 00:00:00 2001 From: Alexander Weidinger Date: Wed, 15 May 2019 18:30:27 +0200 Subject: [PATCH] fix(`map.jinja`): _merge_ defaults and `config.get` test resulting config --- pillar.example | 11 ++++++ template/config/file.sls | 4 +- template/defaults.yaml | 4 ++ template/files/default/example.tmpl.jinja | 5 +++ template/map.jinja | 38 ++++++++++++++----- template/osfamilymap.yaml | 6 ++- .../default/controls/config_spec.rb | 12 +++++- 7 files changed, 67 insertions(+), 13 deletions(-) diff --git a/pillar.example b/pillar.example index 30755fb6..ce073070 100644 --- a/pillar.example +++ b/pillar.example @@ -4,6 +4,9 @@ template: lookup: master: template-master + # Just for testing purposes + winner: lookup + added_in_lookup: lookup_value # Using bash package and udev service as an example. This allows us to # test the template formula itself. You should set these parameters to @@ -42,3 +45,11 @@ template: # - 'example_alt.tmpl' # - 'example_alt.tmpl.jinja' + # For testing purposes + source_files: + template-config-file-file-managed: + - 'example.tmpl.jinja' + + # Just for testing purposes + winner: pillar + added_in_pillar: pillar_value diff --git a/template/config/file.sls b/template/config/file.sls index fc07019d..88f4c283 100644 --- a/template/config/file.sls +++ b/template/config/file.sls @@ -19,7 +19,9 @@ template-config-file-file-managed: }} - mode: 644 - user: root - - group: root + - group: {{ template.rootgroup }} - template: jinja - require: - sls: {{ sls_package_install }} + - context: + template: {{ template | json}} diff --git a/template/defaults.yaml b/template/defaults.yaml index 757ce6d3..a50346a2 100644 --- a/template/defaults.yaml +++ b/template/defaults.yaml @@ -3,6 +3,10 @@ --- template: pkg: template + rootgroup: root config: '/etc/template' service: name: template + # Just here for testing + added_in_defaults: defaults_value + winner: defaults diff --git a/template/files/default/example.tmpl.jinja b/template/files/default/example.tmpl.jinja index 76aa70f4..acacff24 100644 --- a/template/files/default/example.tmpl.jinja +++ b/template/files/default/example.tmpl.jinja @@ -4,3 +4,8 @@ ######################################################################## This is another example file from SaltStack template-formula. + +# This is here for testing purposes +{{ template | json }} + +winner of the merge: {{ template['winner'] }} diff --git a/template/map.jinja b/template/map.jinja index 2593ce08..d65bc926 100644 --- a/template/map.jinja +++ b/template/map.jinja @@ -9,16 +9,36 @@ {%- import_yaml tplroot ~ "/osmap.yaml" as osmap %} {%- import_yaml tplroot ~ "/osfingermap.yaml" as osfingermap %} -{%- set defaults = salt['grains.filter_by'](default_settings, - default=tplroot, - merge=salt['grains.filter_by'](osfamilymap, grain='os_family', - merge=salt['grains.filter_by'](osmap, grain='os', - merge=salt['grains.filter_by'](osfingermap, grain='osfinger', - merge=salt['config.get'](tplroot ~ ':lookup', default={}) +{#- Retrieve the config dict only once #} +{%- set _config = salt['config.get'](tplroot, default={}) %} + +{%- set defaults = salt['grains.filter_by']( + default_settings, + default=tplroot, + merge=salt['grains.filter_by']( + osfamilymap, + grain='os_family', + merge=salt['grains.filter_by']( + osmap, + grain='os', + merge=salt['grains.filter_by']( + osfingermap, + grain='osfinger', + merge=salt['grains.filter_by']( + _config, + default='lookup' + ) + ) ) ) ) -) %} + %} +{%- set config = salt['grains.filter_by']( + {'defaults': defaults}, + default='defaults', + merge=_config + ) + %} -{#- Merge the template config (e.g. from pillar) #} -{%- set template = salt['config.get'](tplroot, default=defaults) %} +{#- Change to whatever variable name you like #} +{%- set template = config %} diff --git a/template/osfamilymap.yaml b/template/osfamilymap.yaml index 8538a120..92cbb4a7 100644 --- a/template/osfamilymap.yaml +++ b/template/osfamilymap.yaml @@ -30,9 +30,11 @@ Arch: Alpine: {} -FreeBSD: {} +FreeBSD: + rootgroup: wheel -OpenBSD: {} +OpenBSD: + rootgroup: wheel Solaris: {} diff --git a/test/integration/default/controls/config_spec.rb b/test/integration/default/controls/config_spec.rb index d2261739..d76c7f66 100644 --- a/test/integration/default/controls/config_spec.rb +++ b/test/integration/default/controls/config_spec.rb @@ -6,6 +6,16 @@ it { should be_owned_by 'root' } it { should be_grouped_into 'root' } its('mode') { should cmp '0644' } - its('content') { should include 'This is an example file from SaltStack template-formula.' } + its('content') { should include 'This is another example file from SaltStack template-formula.' } + its('content') { should include '"added_in_pillar": "pillar_value"' } + its('content') { should include '"added_in_defaults": "defaults_value"' } + its('content') { should include '"added_in_lookup": "lookup_value"' } + its('content') { should include '"config": "/etc/template-formula.conf"' } + its('content') { should include '"lookup": {"added_in_lookup": "lookup_value",' } + its('content') { should include '"pkg": "' } + its('content') { should include '"service": {"name": "' } + 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"]}' } + its('content') { should include '"winner": "pillar"}' } + its('content') { should include 'winner of the merge: pillar' } end end