Saltstack Official OpenSSH Formula
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

226 Zeilen
7.1KB

  1. # -*- coding: utf-8 -*-
  2. # vim: ft=jinja
  3. {#- Get the `tplroot` from `tpldir` #}
  4. {%- set tplroot = tpldir.split("/")[0] %}
  5. {%- from tplroot ~ "/libmatchers.jinja" import parse_matchers, query_map %}
  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. {#- `parse_matchers` returns a `list` of `dict` with #}
  105. {#- - type: `F` to load file, `C`, `G`, `I` for lookup #}
  106. {#- - option: specific to the type #}
  107. {#- - query: which key is requested #}
  108. {#- - query_method: the salt method doing the query `config.get`, `pillar.get` and `grains.get` #}
  109. {#- - value: the result of the `salt[<query_method>](<query>)` #}
  110. {%- set map_matchers = parse_matchers(
  111. matchers=map_sources,
  112. config_get_strategy=_config["merge_strategy"],
  113. log_prefix="map.jinja: "
  114. )
  115. | load_yaml %}
  116. {%- for matcher in map_matchers %}
  117. {%- if matcher.type in query_map.keys() %}
  118. {#- Merge in `mapdata.<query>` instead of directcly in `mapdata` #}
  119. {%- set is_sub_key = matcher.option | default(False) == "SUB" %}
  120. {#- `slsutil.merge` defaults to `smart` instead of `None` for `config.get` #}
  121. {%- set _strategy = _config["merge_strategy"] | default("smart", boolean=True) %}
  122. {%- do salt["log.debug"](
  123. "map.jinja: merge "
  124. ~ "sub key " * is_sub_key
  125. ~ "'"
  126. ~ matcher.query
  127. ~ "' retrieved with '"
  128. ~ matcher.query_method
  129. ~ "', merge: strategy='"
  130. ~ _strategy
  131. ~ "', lists='"
  132. ~ _config["merge_lists"]
  133. ~ "'"
  134. ) %}
  135. {#- empty value is an empty list, it must be an empty dict for `.update` #}
  136. {%- set value = matcher.value | default({}, boolean=True) %}
  137. {%- if is_sub_key %}
  138. {#- Merge values with `mapdata.<key>`, `<key>` and `<key>:lookup` are merged together #}
  139. {%- set value = { matcher.query | regex_replace("(:lookup)?$", ""): value } %}
  140. {%- endif %}
  141. {%- do _config.update(
  142. {
  143. "stack": salt["slsutil.merge"](
  144. _config["stack"],
  145. value,
  146. strategy=_strategy,
  147. merge_lists=_config["merge_lists"],
  148. )
  149. }
  150. ) %}
  151. {%- else %}
  152. {#- Load YAML file matching the grain/pillar/... #}
  153. {#- Fallback to use the source name as a direct filename #}
  154. {#- Mangle `source_key` to use it as literal path #}
  155. {%- if matcher.value | length == 0 %}
  156. {%- set query_parts = matcher.query.split("/") %}
  157. {%- set map_dirname = query_parts[0:-1] | join("/") %}
  158. {%- set map_values = query_parts[-1] | regex_replace("(\.yaml)?$", ".yaml") %}
  159. {%- else %}
  160. {%- set map_dirname = matcher.query %}
  161. {%- set map_values = matcher.value %}
  162. {%- endif %}
  163. {#- Some configuration return list #}
  164. {%- if map_values is string %}
  165. {%- set map_values = [map_values] %}
  166. {%- endif %}
  167. {#- `map_dirname` can be an empty string with literal path like `myconf.yaml` #}
  168. {%- set yaml_dir = [
  169. map_sources_dir,
  170. map_dirname
  171. ]
  172. | select
  173. | join("/") %}
  174. {%- for map_value in map_values %}
  175. {%- set yamlfile = [
  176. yaml_dir,
  177. map_value ~ ".yaml"
  178. ]
  179. | join("/") %}
  180. {%- do salt["log.debug"]("map.jinja: load parameters from file " ~ yamlfile) %}
  181. {%- load_yaml as loaded_values %}
  182. {%- include yamlfile ignore missing %}
  183. {%- endload %}
  184. {%- if loaded_values %}
  185. {#- Merge loaded values on the stack #}
  186. {%- do salt["log.debug"]("map.jinja: merge parameters from " ~ yamlfile) %}
  187. {%- do _config.update(
  188. {
  189. "stack": salt["slsutil.merge"](
  190. _config["stack"],
  191. loaded_values.get("values", {}),
  192. strategy=loaded_values.get("strategy", "smart"),
  193. merge_lists=loaded_values.get("merge_lists", False)
  194. | to_bool,
  195. )
  196. }
  197. ) %}
  198. {%- endif %}
  199. {%- endfor %}
  200. {%- endif %}
  201. {%- endfor %}
  202. {%- do salt["log.debug"]("map.jinja: save parameters in variable 'mapdata'") %}
  203. {%- set mapdata = _config["stack"] %}