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.

299 lines
9.3KB

  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. {#- List of sources to lookup for parameters #}
  9. {#- Fallback to previously used grains plus minion `id` #}
  10. {%- set map_sources = [
  11. "Y:G@osarch",
  12. "Y:G@os_family",
  13. "Y:G@os",
  14. "Y:G@osfinger",
  15. "C@" ~ tplroot ~ ":lookup",
  16. "C@" ~ tplroot,
  17. "Y:G@id",
  18. ] %}
  19. {%- do salt["log.debug"](
  20. "map.jinja: built-in configuration sources:\n"
  21. ~ {"values": {
  22. "map_jinja": {"sources": map_sources}
  23. }
  24. }
  25. | yaml(False)
  26. ) %}
  27. {#- Allow centralised map.jinja configuration #}
  28. {%- set _global_map_filename = "parameters/map_jinja.yaml" %}
  29. {%- do salt["log.debug"](
  30. "map.jinja: load global map.jinja values from "
  31. ~ _global_map_filename
  32. ) %}
  33. {%- load_yaml as global_map_settings %}
  34. {%- include _global_map_filename ignore missing %}
  35. {%- endload %}
  36. {%- if global_map_settings %}
  37. {%- do salt["log.debug"](
  38. "map.jinja: configure sources from global map.jinja configuration "
  39. ~ _global_map_filename
  40. ~ ":\n"
  41. ~ {"map_jinja": global_map_settings}
  42. | yaml(False)
  43. ) %}
  44. {%- set map_sources = global_map_settings
  45. | traverse(
  46. "values:sources",
  47. map_sources,
  48. ) %}
  49. {%- endif %}
  50. {#- Allow per formula map.jinja configuration #}
  51. {%- set _map_filename = map_sources_dir ~ "/map_jinja.yaml" %}
  52. {%- do salt["log.debug"](
  53. "map.jinja: load per formula map.jinja values from "
  54. ~ _map_filename
  55. ) %}
  56. {%- load_yaml as map_settings %}
  57. {%- include _map_filename ignore missing %}
  58. {%- endload %}
  59. {%- if map_settings %}
  60. {%- do salt["log.debug"](
  61. "map.jinja: configure sources from formula map.jinja configuration "
  62. ~ _map_filename
  63. ~ ":\n"
  64. ~ {"map_jinja": map_settings}
  65. | yaml(False)
  66. ) %}
  67. {%- set map_sources = map_settings
  68. | traverse(
  69. "values:sources",
  70. map_sources,
  71. ) %}
  72. {%- endif %}
  73. {%- do salt["log.debug"](
  74. "map.jinja: load parameters from sources:\n"
  75. ~ map_sources
  76. | yaml(False)
  77. ) %}
  78. {#- Load formula defaults values #}
  79. {%- set _defaults_filename = map_sources_dir ~ "/defaults.yaml" %}
  80. {%- do salt["log.debug"](
  81. "map.jinja: load per formula default values from "
  82. ~ _defaults_filename
  83. ) %}
  84. {%- load_yaml as default_settings %}
  85. {%- include _defaults_filename ignore missing %}
  86. {%- endload %}
  87. {%- if not default_settings %}
  88. {%- set default_settings = {'values': {} } %}
  89. {%- endif %}
  90. {#- Make sure to track `map.jinja` configuration with `_mapdata` #}
  91. {%- do default_settings["values"].update(
  92. {
  93. "map_jinja": map_settings
  94. | traverse("values", {})
  95. }
  96. ) %}
  97. {#- Work around assignment inside for loop #}
  98. {#- load configuration values used in `config.get` merging strategies #}
  99. {%- set _config = {
  100. "stack": default_settings.get("values", {}),
  101. "merge_strategy": salt["config.get"](tplroot ~ ":strategy", None),
  102. "merge_lists": salt["config.get"](tplroot ~ ":merge_lists", False),
  103. } %}
  104. {#- the `config.get` merge option only works for `minion` or `local` salt command types #}
  105. {%- if cli in ["minion", "local"] %}
  106. {%- do _config.update(
  107. {
  108. "merge_opt": {"merge": _config["merge_strategy"]},
  109. "merge_msg": ", merge: strategy='" ~ _config["merge_strategy"] ~ "'",
  110. }
  111. ) %}
  112. {#- the `config.get` merge option is not available for `ssh` or `unknown` salt command types #}
  113. {%- else %}
  114. {%- if _config["merge_strategy"] %}
  115. {%- do salt["log.error"](
  116. "map.jinja: the 'merge' option of 'config.get' is skipped when the salt command type is '"
  117. ~ cli
  118. ~ "'"
  119. ) %}
  120. {%- endif %}
  121. {%- do _config.update(
  122. {
  123. "merge_opt": {},
  124. "merge_msg": "",
  125. }
  126. ) %}
  127. {%- endif %}
  128. {%- set query_map = {
  129. "C": "config.get",
  130. "G": "grains.get",
  131. "I": "pillar.get",
  132. } %}
  133. {#- Process each `map.jinja` source #}
  134. {#- each source has a type: #}
  135. {#- - `Y` to load values from YAML files (the default when no type is set) #}
  136. {#- - `C` to lookup values with `config.get` #}
  137. {#- - `G` to lookup values with `grains.get` #}
  138. {#- - `I` to lookup values with `pillar.get` #}
  139. {#- The YAML type option can define query type to build the file name: #}
  140. {#- - `C` for query with `config.get` (the default when to query type is set) #}
  141. {#- - `G` for query with `grains.get` #}
  142. {#- - `I` for query with `pillar.get` #}
  143. {#- The `C`, `G` or `I` types can define the `SUB` option #}
  144. {#- to merge values in the sub key `mapdata.<key>` instead of directly in `mapdata` #}
  145. {%- for map_source in map_sources %}
  146. {%- set source_parts = map_source.split('@') %}
  147. {%- if source_parts|length == 1 %}
  148. {#- By default we load YAML files for config looked up by `config.get` #}
  149. {%- set source_type = "Y" %}
  150. {%- set query_type = "C" %}
  151. {%- set source_key = map_source %}
  152. {%- elif source_parts[0][0] == "Y" %}
  153. {%- set source_type = "Y" %}
  154. {%- set query_type = source_parts[0].split(':')[1] | default("C") %}
  155. {%- set source_key = source_parts[1] %}
  156. {%- elif source_parts[0][0] in query_map.keys() %}
  157. {%- set source_type = source_parts[0].split(':') | first %}
  158. {%- set query_type = source_type %}
  159. {%- set is_sub_key = source_parts[0].split(':')[1] | default(False) == "SUB" %}
  160. {%- set source_key = source_parts[1] %}
  161. {%- endif %}
  162. {%- set query_method = query_map[query_type] %}
  163. {%- if source_type in query_map.keys() %}
  164. {#- Lookup source `<QUERY_METHOD>@key:to:query` #}
  165. {%- if source_type == "C" %}
  166. {%- set merge_opts = _config["merge_opt"] %}
  167. {%- set merge_msg = _config["merge_msg"] %}
  168. {%- else %}
  169. {#- No merging strategy supported for `grains.get` and `pillar.get` #}
  170. {%- set merge_opts = {} %}
  171. {%- set merge_msg = "" %}
  172. {%- endif %}
  173. {%- do salt["log.debug"](
  174. "map.jinja: retrieve '"
  175. ~ source_key
  176. ~ "' with '"
  177. ~ query_method
  178. ~ "'"
  179. ~ merge_msg
  180. ) %}
  181. {%- set _config_get = salt[query_method](
  182. source_key,
  183. default={},
  184. **merge_opts
  185. ) %}
  186. {#- `slsutil.merge` defaults to `smart` instead of `None` for `config.get` #}
  187. {%- set _strategy = _config["merge_strategy"] | default("smart", boolean=True) %}
  188. {%- do salt["log.debug"](
  189. "map.jinja: merge "
  190. ~ "sub key " * is_sub_key
  191. ~ "'"
  192. ~ source_key
  193. ~ "' retrieved with '"
  194. ~ query_method
  195. ~ "', merge: strategy='"
  196. ~ _strategy
  197. ~ "', lists='"
  198. ~ _config["merge_lists"]
  199. ~ "'"
  200. ) %}
  201. {%- if is_sub_key %}
  202. {#- Merge values with `mapdata.<key>`, `<key>` and `<key>:lookup` are merged together #}
  203. {%- set _config_get = { source_key.rstrip(':lookup'): _config_get } %}
  204. {%- endif %}
  205. {%- do _config.update(
  206. {
  207. "stack": salt["slsutil.merge"](
  208. _config["stack"],
  209. _config_get,
  210. strategy=_strategy,
  211. merge_lists=_config["merge_lists"],
  212. )
  213. }
  214. ) %}
  215. {%- else %}
  216. {#- Load YAML file matching the grain/pillar/... #}
  217. {#- Fallback to use the source name as a direct filename #}
  218. {%- do salt["log.debug"](
  219. "map.jinja: lookup '"
  220. ~ source_key
  221. ~ "' with '"
  222. ~ query_method
  223. ~ "'"
  224. ) %}
  225. {%- set map_values = salt[query_method](source_key, []) %}
  226. {#- Mangle `source_key` to use it as literal path #}
  227. {%- if map_values | length == 0 %}
  228. {%- set map_source_parts = source_key.split("/") %}
  229. {%- set source_key = map_source_parts[0:-1] | join("/") %}
  230. {%- set map_values = map_source_parts[-1].rstrip(".yaml") %}
  231. {%- endif %}
  232. {#- Some configuration return list #}
  233. {%- if map_values is string %}
  234. {%- set map_values = [map_values] %}
  235. {%- endif %}
  236. {#- `source_key` can be an empty string with literal path like `myconf.yaml` #}
  237. {%- set yaml_dir = [
  238. map_sources_dir,
  239. source_key
  240. ]
  241. | select
  242. | join("/") %}
  243. {%- for map_value in map_values %}
  244. {%- set yamlfile = [
  245. yaml_dir,
  246. map_value ~ ".yaml"
  247. ]
  248. | join("/") %}
  249. {%- do salt["log.debug"]("map.jinja: load parameters from file " ~ yamlfile) %}
  250. {%- load_yaml as loaded_values %}
  251. {%- include yamlfile ignore missing %}
  252. {%- endload %}
  253. {%- if loaded_values %}
  254. {#- Merge loaded values on the stack #}
  255. {%- do salt["log.debug"]("map.jinja: merge parameters from " ~ yamlfile) %}
  256. {%- do _config.update(
  257. {
  258. "stack": salt["slsutil.merge"](
  259. _config["stack"],
  260. loaded_values.get("values", {}),
  261. strategy=loaded_values.get("strategy", "smart"),
  262. merge_lists=loaded_values.get("merge_lists", False)
  263. | to_bool,
  264. )
  265. }
  266. ) %}
  267. {%- endif %}
  268. {%- endfor %}
  269. {%- endif %}
  270. {%- endfor %}
  271. {%- do salt["log.debug"]("map.jinja: save parameters in variable 'mapdata'") %}
  272. {%- set mapdata = _config["stack"] %}