Saltstack Official OpenSSH Formula
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.7KB

  1. # -*- coding: utf-8 -*-
  2. # vim: ft=jinja
  3. {#- Get the `tplroot` from `tpldir` #}
  4. {%- set tplroot = tpldir.split("/")[0] %}
  5. {%- from tplroot ~ "/libsaltcli.jinja" import cli with context %}
  6. {#- Where to lookup parameters source files #}
  7. {%- set map_sources_dir = tplroot ~ "/parameters" %}
  8. {#- Load defaults first to allow per formula default map.jinja configuration #}
  9. {%- set _defaults_filename = map_sources_dir ~ "/defaults.yaml" %}
  10. {%- do salt["log.debug"](
  11. "map.jinja: initialise parameters from "
  12. ~ _defaults_filename
  13. ) %}
  14. {%- import_yaml _defaults_filename as default_settings %}
  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 = [
  19. "osarch",
  20. "os_family",
  21. "os",
  22. "osfinger",
  23. "config_get_lookup",
  24. "config_get",
  25. "id",
  26. ] %}
  27. {#- Configure map.jinja from defaults.yaml #}
  28. {%- set map_sources = default_settings | traverse(
  29. "values:map_jinja:sources",
  30. map_sources,
  31. ) %}
  32. {#- Lookup global sources #}
  33. {%- set map_sources = salt["config.get"]("map_jinja:sources", map_sources) %}
  34. {#- Lookup per formula sources #}
  35. {%- set map_sources = salt["config.get"](
  36. tplroot ~ ":map_jinja:sources",
  37. map_sources,
  38. ) %}
  39. {%- do salt["log.debug"](
  40. "map.jinja: load parameters with sources from "
  41. ~ map_sources
  42. ) %}
  43. {#- Lookup with `config.get` from configurable roots #}
  44. {%- do salt["log.debug"](
  45. "map.jinja: initialise 'config.get' roots with 'tplroot' "
  46. ~ tplroot
  47. ) %}
  48. {%- set config_get_roots = [tplroot] %}
  49. {#- Configure `config.get` from defaults.yaml #}
  50. {%- set config_get_roots = default_settings | traverse(
  51. "values:map_jinja:config_get_roots",
  52. config_get_roots
  53. ) %}
  54. {#- Lookup global `config.get` roots #}
  55. {%- set config_get_roots = salt["config.get"](
  56. "map_jinja:config_get_roots",
  57. config_get_roots
  58. ) %}
  59. {#- Lookup per formula `config.get` roots #}
  60. {%- set config_get_roots = salt["config.get"](
  61. tplroot ~ ":map_jinja:config_get_roots",
  62. config_get_roots,
  63. ) %}
  64. {%- do salt["log.debug"](
  65. "map.jinja: load parameters with 'config.get' from roots "
  66. ~ config_get_roots
  67. ) %}
  68. {#- Work around assignment inside for loop #}
  69. {#- load configuration values used in `config.get` merging strategies #}
  70. {%- set _config = {
  71. "stack": default_settings.get("values", {}),
  72. "merge_strategy": salt["config.get"](tplroot ~ ":strategy", None),
  73. "merge_lists": salt["config.get"](tplroot ~ ":merge_lists", False),
  74. } %}
  75. {#- the `config.get` merge option only works for `minion` or `local` salt command types #}
  76. {%- if cli in ["minion", "local"] %}
  77. {%- do _config.update(
  78. {
  79. "merge_opt": {"merge": _config["merge_strategy"]},
  80. "merge_msg": ", merge: strategy='" ~ _config["merge_strategy"] ~ "'",
  81. }
  82. ) %}
  83. {#- the `config.get` merge option is not available for `ssh` or `unknown` salt command types #}
  84. {%- else %}
  85. {%- if _config["merge_strategy"] %}
  86. {%- do salt["log.error"](
  87. "map.jinja: the 'merge' option of 'config.get' is skipped when the salt command type is '"
  88. ~ cli
  89. ~ "'"
  90. ) %}
  91. {%- endif %}
  92. {%- do _config.update(
  93. {
  94. "merge_opt": {},
  95. "merge_msg": "",
  96. }
  97. ) %}
  98. {%- endif %}
  99. {#- process each `map.jinja` source #}
  100. {%- for map_source in map_sources %}
  101. {%- if map_source in ["config_get", "config_get_lookup"] %}
  102. {%- for _config_root in config_get_roots %}
  103. {%- set _config_key = {
  104. "config_get": _config_root,
  105. "config_get_lookup": _config_root ~ ":lookup",
  106. }.get(map_source) %}
  107. {%- do salt["log.debug"](
  108. "map.jinja: retrieve '"
  109. ~ _config_key
  110. ~ "' with 'config.get'"
  111. ~ _config["merge_msg"]
  112. ) %}
  113. {%- set _config_get = salt["config.get"](
  114. _config_key, default={}, **_config["merge_opt"]
  115. ) %}
  116. {#- `slsutil.merge` defaults to `smart` instead of `None` for `config.get` #}
  117. {%- set _strategy = _config["merge_strategy"] | default("smart", boolean=True) %}
  118. {%- do salt["log.debug"](
  119. "map.jinja: merge '"
  120. ~ _config_key
  121. ~ "' retrieved with 'config.get'"
  122. ~ ", merge: strategy='"
  123. ~ _strategy
  124. ~ "', lists='"
  125. ~ _config["merge_lists"]
  126. ~ "'"
  127. ) %}
  128. {#- Keep values under each root key when there are more than one #}
  129. {%- if config_get_roots|length > 1 %}
  130. {%- set _config_get = { _config_root: _config_get } %}
  131. {%- endif %}
  132. {%- do _config.update(
  133. {
  134. "stack": salt["slsutil.merge"](
  135. _config["stack"],
  136. _config_get,
  137. strategy=_strategy,
  138. merge_lists=_config["merge_lists"],
  139. )
  140. }
  141. ) %}
  142. {%- endfor %}
  143. {%- else %}
  144. {#- Lookup the grain/pillar/... #}
  145. {#- Fallback to use the source name as a direct filename #}
  146. {%- set map_values = salt["config.get"](map_source, []) %}
  147. {#- Mangle `map_source` to use it as literal path #}
  148. {%- if map_values | length == 0 %}
  149. {%- set map_source_parts = map_source.split("/") %}
  150. {%- set map_source = map_source_parts[0:-1] | join("/") %}
  151. {%- set map_values = map_source_parts[-1].rstrip(".yaml") %}
  152. {%- endif %}
  153. {#- Some configuration return list #}
  154. {%- if map_values is string %}
  155. {%- set map_values = [map_values] %}
  156. {%- endif %}
  157. {%- for map_value in map_values %}
  158. {%- set yamlfile = [
  159. map_sources_dir,
  160. map_source,
  161. map_value ~ ".yaml",
  162. ]
  163. | join("/")
  164. %}
  165. {%- do salt["log.debug"]("map.jinja: load parameters from file " ~ yamlfile) %}
  166. {%- load_yaml as loaded_values %}
  167. {%- include yamlfile ignore missing %}
  168. {%- endload %}
  169. {%- if loaded_values %}
  170. {#- Merge loaded values on the stack #}
  171. {%- do salt["log.debug"]("map.jinja: merge parameters from " ~ yamlfile) %}
  172. {%- do _config.update(
  173. {
  174. "stack": salt["slsutil.merge"](
  175. _config["stack"],
  176. loaded_values.get("values", {}),
  177. strategy=loaded_values.get("strategy", "smart"),
  178. merge_lists=loaded_values.get("merge_lists", False)
  179. | to_bool,
  180. )
  181. }
  182. ) %}
  183. {%- endif %}
  184. {%- endfor %}
  185. {%- endif %}
  186. {%- endfor %}
  187. {%- do salt["log.debug"]("map.jinja: save parameters in variable 'mapdata'") %}
  188. {%- set mapdata = _config["stack"] %}