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.

223 line
6.4KB

  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. {%- set query_map = {
  7. "C": "config.get",
  8. "G": "grains.get",
  9. "I": "pillar.get",
  10. } %}
  11. {#- When no part before `@` is provided: #}
  12. {#- - define a YAML file path, noted `Y` #}
  13. {#- - use `salt["config.get"]`, noted `C` #}
  14. {#- - use colon `:` delimiter for querying #}
  15. {%- set _defaults = {
  16. "type": "Y",
  17. "query_type": "C",
  18. "query_delimiter": ":"
  19. } %}
  20. {%- macro parse_matchers(
  21. matchers,
  22. config_get_strategy=None,
  23. log_prefix="libmatchers: "
  24. ) %}
  25. {#- matcher format is `[<TYPE>[:<OPTION>[:DELIMITER]]@]<KEY>` #}
  26. {#- each matcher has a type: #}
  27. {#- - `Y` to build a YAML file name (the default when no type is set) #}
  28. {#- - `C` to lookup values with `config.get` #}
  29. {#- - `G` to lookup values with `grains.get` #}
  30. {#- - `I` to lookup values with `pillar.get` #}
  31. {#- The `YAML` type option can define query type to build the file name: #}
  32. {#- - `C` for query with `config.get` (the default when to query type is set) #}
  33. {#- - `G` for query with `grains.get` #}
  34. {#- - `I` for query with `pillar.get` #}
  35. {#- With `DELIMITER`, you can choose a different delimiter when doing queries #}
  36. {%- set parsed_matchers = [] %}
  37. {%- for matcher in matchers %}
  38. {%- do salt["log.debug"](
  39. log_prefix
  40. ~ "process matcher: '"
  41. ~ matcher
  42. ~ "'"
  43. ) %}
  44. {%- set parsed = {} %}
  45. {%- set matcher_parts = matcher.split('@') %}
  46. {%- if matcher_parts | length == 1 %}
  47. {#- By default we load YAML files for config looked up by `config.get` #}
  48. {%- do parsed.update(
  49. {
  50. "type": _defaults["type"],
  51. "option": None,
  52. "query_method": query_map[_defaults["query_type"]],
  53. "query_delimiter": _defaults["query_delimiter"],
  54. "query": matcher
  55. }
  56. ) %}
  57. {%- do salt["log.debug"](
  58. log_prefix
  59. ~ "use built-in defaults for matcher:\n"
  60. ~ parsed
  61. | yaml(False)
  62. | indent(4, True)
  63. ) %}
  64. {%- else %}
  65. {%- do salt["log.debug"](
  66. log_prefix
  67. ~ "parse matcher: '"
  68. ~ matcher
  69. ~ "'"
  70. ) %}
  71. {%- set metadatas = matcher_parts[0].split(":") %}
  72. {%- do parsed.update(
  73. {
  74. "query": matcher_parts[1]
  75. }
  76. ) %}
  77. {%- if metadatas | length == 1 %}
  78. {%- do parsed.update(
  79. {
  80. "type": metadatas[0],
  81. "option": "C",
  82. "query_delimiter": ":"
  83. }
  84. ) %}
  85. {%- do salt["log.debug"](
  86. log_prefix
  87. ~ "parse as 1 metadata matcher:\n"
  88. ~ parsed
  89. | yaml(False)
  90. | indent(4, True)
  91. ) %}
  92. {%- elif metadatas | length == 2 %}
  93. {%- do parsed.update(
  94. {
  95. "type": metadatas[0],
  96. "option": metadatas[1],
  97. "query_delimiter": ":"
  98. }
  99. ) %}
  100. {%- do salt["log.debug"](
  101. log_prefix
  102. ~ "parse as 2 metadata matcher:\n"
  103. ~ parsed
  104. | yaml(False)
  105. | indent(4, True)
  106. ) %}
  107. {%- elif metadatas | length == 3 %}
  108. {%- do parsed.update(
  109. {
  110. "type": metadatas[0],
  111. "option": metadatas[1],
  112. "query_delimiter": metadatas[2] | default(":", boolean=True)
  113. }
  114. ) %}
  115. {%- do salt["log.debug"](
  116. log_prefix
  117. ~ "parse as 3 metadata matcher:\n"
  118. ~ parsed
  119. | yaml(False)
  120. | indent(4, True)
  121. ) %}
  122. {%- elif metadatas | length == 4 %}
  123. {#- The delimiter is `:` #}
  124. {%- do parsed.update(
  125. {
  126. "type": metadatas[0],
  127. "option": metadatas[1],
  128. "query_delimiter": ":"
  129. }
  130. ) %}
  131. {%- do salt["log.debug"](
  132. log_prefix
  133. ~ "parse as 4 metadata matcher:\n"
  134. ~ parsed
  135. | yaml(False)
  136. | indent(4, True)
  137. ) %}
  138. {%- endif %}
  139. {%- endif %}
  140. {#- The `<OPTION>` has different meaning based on type #}
  141. {%- if query_map.get(parsed.type, False) %}
  142. {%- do parsed.update(
  143. {
  144. "query_method": query_map[parsed.type]
  145. }
  146. ) %}
  147. {%- else %}
  148. {%- do parsed.update(
  149. {
  150. "query_method": query_map[
  151. parsed.option
  152. | default("C", boolean=True)
  153. ]
  154. }
  155. ) %}
  156. {%- endif %}
  157. {#- Add `merge:` option to `salt["config.get"]` if configured #}
  158. {%- if cli not in ["ssh", "unknown"] and parsed.query_method == "config.get" and config_get_strategy %}
  159. {%- set query_opts = {
  160. "merge": config_get_strategy,
  161. "delimiter": parsed.query_delimiter,
  162. } %}
  163. {%- set query_opts_msg = (
  164. ", delimiter='"
  165. ~ parsed.query_delimiter
  166. ~ "', merge: strategy='"
  167. ~ config_get_strategy
  168. ~ "'"
  169. ) %}
  170. {%- else %}
  171. {%- if cli in ["ssh", "unknown"] %}
  172. {%- do salt["log.warning"](
  173. log_prefix
  174. ~ "the 'delimiter' and 'merge' options of 'config.get' are skipped when the salt command type is '"
  175. ~ cli
  176. ~ "'"
  177. ) %}
  178. {%- endif %}
  179. {%- set query_opts = {} %}
  180. {%- set query_opts_msg = "" %}
  181. {%- endif %}
  182. {%- do salt["log.debug"](
  183. log_prefix
  184. ~ "lookup '"
  185. ~ parsed.query
  186. ~ "' with '"
  187. ~ parsed.query_method
  188. ~ "'"
  189. ~ query_opts_msg
  190. ) %}
  191. {%- set values = salt[parsed.query_method](
  192. parsed.query,
  193. default=[],
  194. **query_opts
  195. ) %}
  196. {%- do parsed.update(
  197. {
  198. "value": values
  199. }
  200. ) %}
  201. {%- do parsed_matchers.append(parsed) %}
  202. {%- endfor %}
  203. {%- do salt["log.debug"](
  204. log_prefix
  205. ~ "parsed matchers:\n"
  206. ~ parsed_matchers
  207. | yaml(False)
  208. | indent(4, True)
  209. ) %}
  210. {{ parsed_matchers | yaml }}
  211. {%- endmacro %}