Saltstack Official OpenSSH Formula
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

178 lines
6.0KB

  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. {%- from tplroot ~ "/libmapstack.jinja" import mapstack %}
  7. {#- Where to lookup parameters source files #}
  8. {%- set map_sources_dir = tplroot ~ "/parameters" %}
  9. {#- List of sources to lookup for parameters #}
  10. {#- Fallback to previously used grains plus minion `id` #}
  11. {%- set map_sources = [
  12. "Y:G@osarch",
  13. "Y:G@os_family",
  14. "Y:G@os",
  15. "Y:G@osfinger",
  16. "C@" ~ tplroot ~ ":lookup",
  17. "C@" ~ tplroot,
  18. "Y:G@id",
  19. ] %}
  20. {#- Allow centralised map.jinja configuration #}
  21. {%- set _global_param_filename = "parameters/map_jinja.yaml" %}
  22. {#- Allow per formula map.jinja configuration #}
  23. {%- set _formula_param_filename = map_sources_dir ~ "/map_jinja.yaml" %}
  24. {%- set _map_settings = mapstack(
  25. files=[_global_param_filename, _formula_param_filename],
  26. defaults={
  27. "values": {"sources": map_sources}
  28. },
  29. log_prefix="map.jinja configuration: ",
  30. )
  31. | load_yaml %}
  32. {%- set map_sources = _map_settings | traverse("values:sources") %}
  33. {%- do salt["log.debug"](
  34. "map.jinja: load parameters from sources:\n"
  35. ~ map_sources
  36. | yaml(False)
  37. ) %}
  38. {#- Load formula defaults values #}
  39. {%- set _defaults_filename = map_sources_dir ~ "/defaults.yaml" %}
  40. {%- set default_settings = mapstack(
  41. files=[_defaults_filename],
  42. defaults={"values": {} },
  43. log_prefix="map.jinja defaults: ",
  44. )
  45. | load_yaml %}
  46. {#- Make sure to track `map.jinja` configuration with `_mapdata` #}
  47. {%- do default_settings["values"].update(
  48. {
  49. "map_jinja": _map_settings["values"]
  50. }
  51. ) %}
  52. {#- Work around assignment inside for loop #}
  53. {#- load configuration values used in `config.get` merging strategies #}
  54. {%- set _config = {
  55. "stack": default_settings.get("values", {}),
  56. "merge_strategy": salt["config.get"](tplroot ~ ":strategy", None),
  57. "merge_lists": salt["config.get"](tplroot ~ ":merge_lists", False),
  58. } %}
  59. {#- `parse_matchers` returns a `list` of `dict` with #}
  60. {#- - type: `F` to load file, `C`, `G`, `I` for lookup #}
  61. {#- - option: specific to the type #}
  62. {#- - query: which key is requested #}
  63. {#- - query_delimiter: the separator between query component #}
  64. {#- - query_method: the salt method doing the query `config.get`, `pillar.get` and `grains.get` #}
  65. {#- - value: the result of the `salt[<query_method>](<query>)` #}
  66. {%- set map_matchers = parse_matchers(
  67. matchers=map_sources,
  68. config_get_strategy=_config["merge_strategy"],
  69. log_prefix="map.jinja: "
  70. )
  71. | load_yaml %}
  72. {%- for matcher in map_matchers %}
  73. {%- if matcher.type in query_map.keys() %}
  74. {#- Merge in `mapdata.<query>` instead of directcly in `mapdata` #}
  75. {%- set is_sub_key = matcher.option | default(False) == "SUB" %}
  76. {#- `slsutil.merge` defaults to `smart` instead of `None` for `config.get` #}
  77. {%- set _strategy = _config["merge_strategy"] | default("smart", boolean=True) %}
  78. {%- do salt["log.debug"](
  79. "map.jinja: merge "
  80. ~ "sub key " * is_sub_key
  81. ~ "'"
  82. ~ matcher.query
  83. ~ "' retrieved with '"
  84. ~ matcher.query_method
  85. ~ "', merge: strategy='"
  86. ~ _strategy
  87. ~ "', lists='"
  88. ~ _config["merge_lists"]
  89. ~ "'"
  90. ) %}
  91. {#- empty value is an empty list, it must be an empty dict for `.update` #}
  92. {%- set value = matcher.value | default({}, boolean=True) %}
  93. {%- if is_sub_key %}
  94. {#- Merge values with `mapdata.<key>`, `<key>` and `<key>:lookup` are merged together #}
  95. {%- set value = { matcher.query | regex_replace("(:lookup)?$", ""): value } %}
  96. {%- endif %}
  97. {%- do _config.update(
  98. {
  99. "stack": salt["slsutil.merge"](
  100. _config["stack"],
  101. value,
  102. strategy=_strategy,
  103. merge_lists=_config["merge_lists"],
  104. )
  105. }
  106. ) %}
  107. {%- else %}
  108. {#- Load YAML file matching the grain/pillar/... #}
  109. {#- Fallback to use the source name as a direct filename #}
  110. {#- Mangle `source_key` to use it as literal path #}
  111. {%- if matcher.value | length == 0 %}
  112. {%- set query_parts = matcher.query.split("/") %}
  113. {%- set map_dirname = query_parts[0:-1] | join("/") %}
  114. {%- set map_values = query_parts[-1] | regex_replace("(\.yaml)?$", ".yaml") %}
  115. {%- else %}
  116. {%- set map_dirname = matcher.query %}
  117. {%- set map_values = matcher.value %}
  118. {%- endif %}
  119. {#- Some configuration return list #}
  120. {%- if map_values is string %}
  121. {%- set map_values = [map_values] %}
  122. {%- endif %}
  123. {#- `map_dirname` can be an empty string with literal path like `myconf.yaml` #}
  124. {%- set yaml_dir = [
  125. map_sources_dir,
  126. map_dirname
  127. ]
  128. | select
  129. | join("/") %}
  130. {%- for map_value in map_values %}
  131. {%- set yamlfile = [
  132. yaml_dir,
  133. map_value ~ ".yaml"
  134. ]
  135. | join("/") %}
  136. {%- do salt["log.debug"]("map.jinja: load parameters from file " ~ yamlfile) %}
  137. {%- load_yaml as loaded_values %}
  138. {%- include yamlfile ignore missing %}
  139. {%- endload %}
  140. {%- if loaded_values %}
  141. {#- Merge loaded values on the stack #}
  142. {%- do salt["log.debug"]("map.jinja: merge parameters from " ~ yamlfile) %}
  143. {%- do _config.update(
  144. {
  145. "stack": salt["slsutil.merge"](
  146. _config["stack"],
  147. loaded_values.get("values", {}),
  148. strategy=loaded_values.get("strategy", "smart"),
  149. merge_lists=loaded_values.get("merge_lists", False)
  150. | to_bool,
  151. )
  152. }
  153. ) %}
  154. {%- endif %}
  155. {%- endfor %}
  156. {%- endif %}
  157. {%- endfor %}
  158. {%- do salt["log.debug"]("map.jinja: save parameters in variable 'mapdata'") %}
  159. {%- set mapdata = _config["stack"] %}