2021-01-11 17:41:20 +01:00
|
|
|
{#- -*- coding: utf-8 -*- #}
|
|
|
|
{#- vim: ft=jinja #}
|
|
|
|
|
|
|
|
{#- Get the `tplroot` from `tpldir` #}
|
|
|
|
{%- set tplroot = tpldir.split("/")[0] %}
|
2021-10-26 13:11:24 +02:00
|
|
|
{%- from tplroot ~ "/libsaltcli.jinja" import cli with context %}
|
2021-01-11 17:41:20 +01:00
|
|
|
|
|
|
|
{%- set query_map = {
|
|
|
|
"C": "config.get",
|
|
|
|
"G": "grains.get",
|
|
|
|
"I": "pillar.get",
|
|
|
|
} %}
|
|
|
|
|
|
|
|
{#- When no part before `@` is provided: #}
|
|
|
|
{#- - define a filename path, noted `F` #}
|
|
|
|
{#- - use `salt["config.get"]`, noted `C` #}
|
2021-01-11 17:47:11 +01:00
|
|
|
{#- - use colon `:` delimiter for querying #}
|
2021-01-11 17:41:20 +01:00
|
|
|
{%- set _defaults = {
|
|
|
|
"type": "F",
|
|
|
|
"query_type": "C",
|
2021-01-11 17:47:11 +01:00
|
|
|
"query_delimiter": ":"
|
2021-01-11 17:41:20 +01:00
|
|
|
} %}
|
|
|
|
|
|
|
|
{%- macro parse_matchers(
|
|
|
|
matchers,
|
|
|
|
config_get_strategy=None,
|
|
|
|
log_prefix="libmatchers: "
|
|
|
|
) %}
|
2021-01-11 17:47:11 +01:00
|
|
|
{#- matcher format is `[<TYPE>[:<OPTION>[:DELIMITER]]@]<KEY>` #}
|
2021-01-11 17:41:20 +01:00
|
|
|
{#- each matcher has a type: #}
|
|
|
|
{#- - `F` to build a file name (the default when no type is set) #}
|
|
|
|
{#- - `C` to lookup values with `config.get` #}
|
|
|
|
{#- - `G` to lookup values with `grains.get` #}
|
|
|
|
{#- - `I` to lookup values with `pillar.get` #}
|
|
|
|
{#- The `FILE` type option can define query type to build the file name: #}
|
|
|
|
{#- - `C` for query with `config.get` (the default when to query type is set) #}
|
|
|
|
{#- - `G` for query with `grains.get` #}
|
|
|
|
{#- - `I` for query with `pillar.get` #}
|
2021-01-11 17:47:11 +01:00
|
|
|
{#- With `DELIMITER`, you can choose a different delimiter when doing queries #}
|
2021-01-11 17:41:20 +01:00
|
|
|
{%- set parsed_matchers = [] %}
|
|
|
|
{%- for matcher in matchers %}
|
|
|
|
{%- do salt["log.debug"](
|
|
|
|
log_prefix
|
|
|
|
~ "process matcher: '"
|
|
|
|
~ matcher
|
|
|
|
~ "'"
|
|
|
|
) %}
|
|
|
|
|
|
|
|
{%- set parsed = {} %}
|
|
|
|
{%- set matcher_parts = matcher.split('@') %}
|
|
|
|
{%- if matcher_parts | length == 1 %}
|
|
|
|
{#- By default we load YAML files for config looked up by `config.get` #}
|
|
|
|
{%- do parsed.update(
|
|
|
|
{
|
|
|
|
"type": _defaults["type"],
|
|
|
|
"option": None,
|
|
|
|
"query_method": query_map[_defaults["query_type"]],
|
2021-01-11 17:47:11 +01:00
|
|
|
"query_delimiter": _defaults["query_delimiter"],
|
2021-01-11 17:41:20 +01:00
|
|
|
"query": matcher
|
|
|
|
}
|
|
|
|
) %}
|
|
|
|
{%- do salt["log.debug"](
|
|
|
|
log_prefix
|
|
|
|
~ "use built-in defaults for matcher:\n"
|
|
|
|
~ parsed
|
|
|
|
| yaml(False)
|
|
|
|
| indent(4, True)
|
|
|
|
) %}
|
|
|
|
{%- else %}
|
|
|
|
{%- do salt["log.debug"](
|
|
|
|
log_prefix
|
|
|
|
~ "parse matcher: '"
|
|
|
|
~ matcher
|
|
|
|
~ "'"
|
|
|
|
) %}
|
|
|
|
{%- set metadatas = matcher_parts[0].split(":") %}
|
|
|
|
{%- do parsed.update(
|
|
|
|
{
|
|
|
|
"query": matcher_parts[1]
|
|
|
|
}
|
|
|
|
) %}
|
|
|
|
{%- if metadatas | length == 1 %}
|
|
|
|
{%- do parsed.update(
|
|
|
|
{
|
|
|
|
"type": metadatas[0],
|
|
|
|
"option": "C",
|
2021-01-11 17:47:11 +01:00
|
|
|
"query_delimiter": ":"
|
2021-01-11 17:41:20 +01:00
|
|
|
}
|
|
|
|
) %}
|
|
|
|
{%- do salt["log.debug"](
|
|
|
|
log_prefix
|
|
|
|
~ "parse as 1 metadata matcher:\n"
|
|
|
|
~ parsed
|
|
|
|
| yaml(False)
|
|
|
|
| indent(4, True)
|
|
|
|
) %}
|
|
|
|
{%- elif metadatas | length == 2 %}
|
|
|
|
{%- do parsed.update(
|
|
|
|
{
|
|
|
|
"type": metadatas[0],
|
|
|
|
"option": metadatas[1],
|
2021-01-11 17:47:11 +01:00
|
|
|
"query_delimiter": ":"
|
2021-01-11 17:41:20 +01:00
|
|
|
}
|
|
|
|
) %}
|
|
|
|
{%- do salt["log.debug"](
|
|
|
|
log_prefix
|
|
|
|
~ "parse as 2 metadata matcher:\n"
|
|
|
|
~ parsed
|
|
|
|
| yaml(False)
|
|
|
|
| indent(4, True)
|
|
|
|
) %}
|
|
|
|
{%- elif metadatas | length == 3 %}
|
|
|
|
{%- do parsed.update(
|
|
|
|
{
|
|
|
|
"type": metadatas[0],
|
|
|
|
"option": metadatas[1],
|
2021-01-11 17:47:11 +01:00
|
|
|
"query_delimiter": metadatas[2] | default(":", boolean=True)
|
2021-01-11 17:41:20 +01:00
|
|
|
}
|
|
|
|
) %}
|
|
|
|
{%- do salt["log.debug"](
|
|
|
|
log_prefix
|
|
|
|
~ "parse as 3 metadata matcher:\n"
|
|
|
|
~ parsed
|
|
|
|
| yaml(False)
|
|
|
|
| indent(4, True)
|
|
|
|
) %}
|
|
|
|
{%- elif metadatas | length == 4 %}
|
2021-01-11 17:47:11 +01:00
|
|
|
{#- The delimiter is `:` #}
|
2021-01-11 17:41:20 +01:00
|
|
|
{%- do parsed.update(
|
|
|
|
{
|
|
|
|
"type": metadatas[0],
|
|
|
|
"option": metadatas[1],
|
2021-01-11 17:47:11 +01:00
|
|
|
"query_delimiter": ":"
|
2021-01-11 17:41:20 +01:00
|
|
|
}
|
|
|
|
) %}
|
|
|
|
{%- do salt["log.debug"](
|
|
|
|
log_prefix
|
|
|
|
~ "parse as 4 metadata matcher:\n"
|
|
|
|
~ parsed
|
|
|
|
| yaml(False)
|
|
|
|
| indent(4, True)
|
|
|
|
) %}
|
|
|
|
{%- endif %}
|
|
|
|
{%- endif %}
|
|
|
|
|
|
|
|
{#- The `<OPTION>` has different meaning based on type #}
|
|
|
|
{%- if query_map.get(parsed.type, False) %}
|
|
|
|
{%- do parsed.update(
|
|
|
|
{
|
|
|
|
"query_method": query_map[parsed.type]
|
|
|
|
}
|
|
|
|
) %}
|
|
|
|
{%- else %}
|
|
|
|
{%- do parsed.update(
|
|
|
|
{
|
|
|
|
"query_method": query_map[
|
|
|
|
parsed.option
|
|
|
|
| default("C", boolean=True)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
) %}
|
|
|
|
{%- endif %}
|
|
|
|
|
|
|
|
{#- Add `merge:` option to `salt["config.get"]` if configured #}
|
|
|
|
{%- if cli in ["minion", "local"] and parsed.query_method == "config.get" and config_get_strategy %}
|
2021-01-11 17:47:11 +01:00
|
|
|
{%- set query_opts = {
|
|
|
|
"merge": config_get_strategy,
|
|
|
|
"delimiter": parsed.query_delimiter,
|
|
|
|
} %}
|
|
|
|
{%- set query_opts_msg = (
|
|
|
|
", delimiter='"
|
|
|
|
~ parsed.query_delimiter
|
|
|
|
~ "', merge: strategy='"
|
2021-01-11 17:41:20 +01:00
|
|
|
~ config_get_strategy
|
|
|
|
~ "'"
|
|
|
|
) %}
|
|
|
|
{%- else %}
|
|
|
|
{%- if cli not in ["minion", "local"] %}
|
|
|
|
{%- do salt["log.error"](
|
|
|
|
log_prefix
|
2021-01-11 17:47:11 +01:00
|
|
|
~ "the 'delimiter' and 'merge' options of 'config.get' are skipped when the salt command type is '"
|
2021-01-11 17:41:20 +01:00
|
|
|
~ cli
|
|
|
|
~ "'"
|
|
|
|
) %}
|
|
|
|
{%- endif %}
|
2021-01-11 17:47:11 +01:00
|
|
|
{%- set query_opts = {} %}
|
|
|
|
{%- set query_opts_msg = "" %}
|
2021-01-11 17:41:20 +01:00
|
|
|
{%- endif %}
|
|
|
|
|
|
|
|
{%- do salt["log.debug"](
|
|
|
|
log_prefix
|
|
|
|
~ "lookup '"
|
|
|
|
~ parsed.query
|
|
|
|
~ "' with '"
|
|
|
|
~ parsed.query_method
|
|
|
|
~ "'"
|
2021-01-11 17:47:11 +01:00
|
|
|
~ query_opts_msg
|
2021-01-11 17:41:20 +01:00
|
|
|
) %}
|
|
|
|
{%- set values = salt[parsed.query_method](
|
|
|
|
parsed.query,
|
|
|
|
default=[],
|
2021-01-11 17:47:11 +01:00
|
|
|
**query_opts
|
2021-01-11 17:41:20 +01:00
|
|
|
) %}
|
|
|
|
{%- do parsed.update(
|
|
|
|
{
|
|
|
|
"value": values
|
|
|
|
}
|
|
|
|
) %}
|
|
|
|
|
|
|
|
{%- do parsed_matchers.append(parsed) %}
|
|
|
|
|
|
|
|
{%- endfor %}
|
|
|
|
{%- do salt["log.debug"](
|
|
|
|
log_prefix
|
|
|
|
~ "parsed matchers:\n"
|
|
|
|
~ parsed_matchers
|
|
|
|
| yaml(False)
|
|
|
|
| indent(4, True)
|
|
|
|
) %}
|
|
|
|
|
|
|
|
{{ parsed_matchers | yaml }}
|
|
|
|
{%- endmacro %}
|