Saltstack Official OpenSSH Formula
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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