|
3 | 3 |
|
4 | 4 | {#- Get the `tplroot` from `tpldir` #} |
5 | 5 | {%- set tplroot = tpldir.split('/')[0] %} |
6 | | -{#- Start imports as #} |
7 | | -{%- import_yaml tplroot ~ "/defaults.yaml" as default_settings %} |
8 | | -{%- import_yaml tplroot ~ "/osfamilymap.yaml" as osfamilymap %} |
9 | | -{%- import_yaml tplroot ~ "/osmap.yaml" as osmap %} |
10 | | -{%- import_yaml tplroot ~ "/osfingermap.yaml" as osfingermap %} |
11 | | - |
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 | | - ) |
32 | | - ) |
33 | | - ) |
34 | | - ) |
35 | | - %} |
36 | | - |
37 | | -{%- set config = salt['grains.filter_by']( |
38 | | - {'defaults': defaults}, |
39 | | - default='defaults', |
40 | | - merge=_config |
41 | | - ) |
42 | | -%} |
43 | | - |
44 | | -{%- set libvirt_settings = config %} |
| 6 | + |
| 7 | +{#- Where to lookup parameters source files #} |
| 8 | +{%- set map_sources_dir = tplroot | path_join('parameters') %} |
| 9 | + |
| 10 | +{#- Load defaults first to allow per formula default map.jinja configuration #} |
| 11 | +{%- set _defaults_filename = map_sources_dir | path_join('defaults.yaml') %} |
| 12 | +{%- do salt['log.debug']('map.jinja: initialise parameters from ' ~ _defaults_filename ) %} |
| 13 | +{%- import_yaml _defaults_filename as default_settings %} |
| 14 | + |
| 15 | +{#- List of sources to lookup for parameters #} |
| 16 | +{%- do salt['log.debug']("map.jinja: lookup 'map_jinja' configuration sources") %} |
| 17 | +{#- Fallback to previously used grains plus minion `id` #} |
| 18 | +{%- set map_sources = ['osarch', 'os_family', 'os', 'osfinger', 'config_get_lookup', 'config_get', 'id'] %} |
| 19 | +{#- Configure map.jinja from defaults.yaml #} |
| 20 | +{%- set map_sources = default_settings | traverse('values:map_jinja:sources', map_sources) %} |
| 21 | + |
| 22 | +{#- Lookup global sources #} |
| 23 | +{%- set map_sources = salt['config.get']('map_jinja:sources', map_sources) %} |
| 24 | +{#- Lookup per formula sources #} |
| 25 | +{%- set map_sources = salt['config.get'](tplroot ~ ':map_jinja:sources', map_sources) %} |
| 26 | + |
| 27 | +{%- do salt['log.debug']('map.jinja: load parameters with sources from ' ~ map_sources) %} |
| 28 | + |
| 29 | + |
| 30 | +{#- Work around assignment inside for loop #} |
| 31 | +{#- load configuration values used in `config.get` merging strategies #} |
| 32 | +{%- set _config = {'stack': default_settings.get('values', {}), |
| 33 | + 'merge_strategy': salt['config.get'](tplroot ~ ':strategy', None), |
| 34 | + 'merge_lists': salt['config.get'](tplroot ~ ':merge_lists', False), |
| 35 | + 'cli': salt['config.get']('__cli'), |
| 36 | + 'root_dir': salt['config.get']('root_dir') |
| 37 | + } %} |
| 38 | + |
| 39 | +{#- `config.get` merge option works only for `minion` or local `salt-call` #} |
| 40 | +{%- if _config['cli'] == 'salt-minion' |
| 41 | + or (_config['cli'] == 'salt-call' and not _config['root_dir'].endswith('/running_data')) %} |
| 42 | + {%- do _config.update({'merge_opt': {'merge': _config['merge_strategy']}, |
| 43 | + 'merge_msg': ", merge: strategy='" ~ _config['merge_strategy'] ~ "'"}) %} |
| 44 | +{%- else %} |
| 45 | + {#- no `config.get` merge option for `salt-ssh` or `unknown` cli #} |
| 46 | + {%- if _config['merge_strategy'] %} |
| 47 | + {%- do salt['log.error']("map.jinja: the 'merge' option of 'config.get' is skipped with salt cli '" |
| 48 | + ~ _config['cli'] | replace('call', 'ssh') | default('unknown', True) ~ "'") |
| 49 | + %} |
| 50 | + {%- endif %} |
| 51 | + {%- do _config.update({'merge_opt': {}, |
| 52 | + 'merge_msg': ''}) %} |
| 53 | +{%- endif %} |
| 54 | + |
| 55 | + |
| 56 | +{#- process each `map.jinja` source #} |
| 57 | +{%- for map_source in map_sources %} |
| 58 | + {%- if map_source in ['config_get', 'config_get_lookup'] %} |
| 59 | + {%- set _config_key = {'config_get': tplroot, |
| 60 | + 'config_get_lookup': tplroot ~ ':lookup'}.get(map_source) %} |
| 61 | + {%- set _config_type = {'config_get': 'configuration', |
| 62 | + 'config_get_lookup': 'lookup'}.get(map_source) %} |
| 63 | + |
| 64 | + {%- do salt['log.debug']("map.jinja: retrieve formula " ~ _config_type |
| 65 | + ~ " with 'config.get'" |
| 66 | + ~ _config['merge_msg'] |
| 67 | + ) %} |
| 68 | + {%- set _config_get = salt['config.get'](_config_key, default={}, **_config['merge_opt']) %} |
| 69 | + |
| 70 | + {#- `slsutil.merge` defaults to `smart` instead of `None` for `config.get` #} |
| 71 | + {%- set _strategy = _config['merge_strategy'] | default('smart', boolean=True) %} |
| 72 | + {%- do salt['log.debug']("map.jinja: merge formula " ~ _config_type |
| 73 | + ~ " retrieved with 'config.get'" |
| 74 | + ~ ", merge: strategy='" ~ _strategy |
| 75 | + ~ "', lists='" ~ _config['merge_lists'] ~ "'" |
| 76 | + ) %} |
| 77 | + {%- do _config.update({'stack': salt['slsutil.merge'](_config['stack'], |
| 78 | + _config_get, |
| 79 | + strategy=_strategy, |
| 80 | + merge_lists=_config['merge_lists'])}) |
| 81 | + %} |
| 82 | + {%- else %} |
| 83 | + {#- Lookup the grain/pillar/... #} |
| 84 | + {#- Fallback to use the source name as a direct filename #} |
| 85 | + {%- set map_values = salt['config.get'](map_source, []) %} |
| 86 | + |
| 87 | + {#- Mangle `map_source` to use it as literal path #} |
| 88 | + {%- if map_values|length == 0 %} |
| 89 | + {%- set map_source_parts = map_source.split('/') %} |
| 90 | + {%- set map_source = map_source_parts[0:-1] | join('/') %} |
| 91 | + {%- set map_values = map_source_parts[-1].rstrip('.yaml') %} |
| 92 | + {%- endif %} |
| 93 | + |
| 94 | + {#- Some configuration return list #} |
| 95 | + {%- if map_values is string %} |
| 96 | + {%- set map_values = [map_values] %} |
| 97 | + {%- endif %} |
| 98 | + |
| 99 | + {%- for map_value in map_values %} |
| 100 | + {%- set yamlfile = map_sources_dir | path_join(map_source, map_value ~ '.yaml') %} |
| 101 | + {%- do salt['log.debug']('map.jinja: load parameters from file ' ~ yamlfile) %} |
| 102 | + {%- load_yaml as loaded_values %} |
| 103 | + {%- include yamlfile ignore missing %} |
| 104 | + {%- endload %} |
| 105 | + |
| 106 | + {%- if loaded_values %} |
| 107 | + {#- Merge loaded values on the stack #} |
| 108 | + {%- do salt['log.debug']('map.jinja: merge parameters from ' ~ yamlfile) %} |
| 109 | + {%- do _config.update({'stack': salt['slsutil.merge'](_config['stack'], |
| 110 | + loaded_values.get('values', {}), |
| 111 | + strategy=loaded_values.get('strategy', 'smart'), |
| 112 | + merge_lists=loaded_values.get('merge_lists', False) | to_bool)}) |
| 113 | + %} |
| 114 | + {%- endif %} |
| 115 | + {%- endfor %} |
| 116 | + {%- endif %} |
| 117 | +{%- endfor %} |
| 118 | + |
| 119 | +{%- do salt['log.debug']("map.jinja: save parameters in variable 'libvirt_settings'") %} |
| 120 | +{%- set libvirt_settings = _config['stack'] %} |
0 commit comments