mirror of
https://github.com/saltstack-formulas/sysctl-formula.git
synced 2026-10-02 22:51:44 +02:00
feat(map): adjusted the formula to be more like the template formula
This commit is contained in:
1 parent
1402cda3e3
commit
4a92a91c70
15 files changed
+777
-50
No files matched your search
@@ -3,10 +3,10 @@
|
||||
---
|
||||
{#- Get the `tplroot` from `tpldir` #}
|
||||
{%- set tplroot = tpldir.split("/")[0] %}
|
||||
{%- from tplroot ~ "/map.jinja" import sysctl_settings with context %}
|
||||
{%- from tplroot ~ "/map.jinja" import mapdata with context %}
|
||||
|
||||
{%- set _mapdata = {
|
||||
"values": sysctl_settings,
|
||||
"values": mapdata,
|
||||
} %}
|
||||
{%- do salt["log.debug"]("### MAP.JINJA DUMP ###\n" ~ _mapdata | yaml(False)) %}
|
||||
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim: ft=yaml
|
||||
---
|
||||
sysctl:
|
||||
pkg: procps-ng
|
||||
config:
|
||||
location: '/etc/sysctl.d'
|
||||
@@ -0,0 +1,315 @@
|
||||
{#- -*- coding: utf-8 -*- #}
|
||||
{#- vim: ft=jinja #}
|
||||
|
||||
{#- Get the `tplroot` from `tpldir` #}
|
||||
{%- set tplroot = tpldir.split("/")[0] %}
|
||||
{%- from tplroot ~ "/libmatchers.jinja" import parse_matchers, query_map with context %}
|
||||
|
||||
{%- set _default_config_dirs = [
|
||||
"parameters/",
|
||||
tplroot ~ "/parameters"
|
||||
] %}
|
||||
|
||||
{%- macro mapstack(
|
||||
matchers,
|
||||
defaults=None,
|
||||
dirs=_default_config_dirs,
|
||||
log_prefix="libmapstack: "
|
||||
) %}
|
||||
{#-
|
||||
Load configuration in the order of `matchers` and merge
|
||||
successively the values with `defaults`.
|
||||
|
||||
The `matchers` are processed using `libmatchers.jinja` to select
|
||||
the configuration sources from where the values are loaded.
|
||||
|
||||
Parameters:
|
||||
|
||||
- `matchers`: list of matchers in the form
|
||||
`[<TYPE>[:<OPTION>[:<DELIMITER>]]@]<QUERY>`
|
||||
|
||||
- `defaults`: dictionary of default values to start the merging,
|
||||
they are considered built-ins. It must conform to the same
|
||||
layout as the YAML files: a mandatory `values` key and two
|
||||
optional `strategy` and `merge_lists` keys.
|
||||
|
||||
- `dirs`: list of directory where to look-up the configuration
|
||||
file matching the matchers, by default a global `salt://parameters/`
|
||||
and a per formula `salt://<tplroot>/parameters`
|
||||
|
||||
- `log_prefix`: prefix used in the log outputs, by default it is
|
||||
`libmapstack: `
|
||||
|
||||
Example: On a Debian system with `roles=["nginx/server", "telegraf"]`
|
||||
|
||||
{%- set settings = mapstack(
|
||||
matchers=[
|
||||
"Y:G@os_family",
|
||||
"I@" ~ tplroot,
|
||||
"Y:C@roles",
|
||||
],
|
||||
dirs=["defaults", tplroot ~ "/parameters"],
|
||||
)
|
||||
| load_yaml %}
|
||||
|
||||
This will merge the values:
|
||||
|
||||
- starting with the default empty dictionary `{}` (no
|
||||
`defaults` parameter)
|
||||
|
||||
- from the YAML files
|
||||
|
||||
- `salt://defaults/os_family/Debian.yaml`
|
||||
|
||||
- `salt://{{ tplroot }}/parameters/os_family/Debian.yaml`
|
||||
|
||||
- from the pillar `salt["pillar.get"](tplroot)`
|
||||
|
||||
- from the `nginx/server` YAML files:
|
||||
|
||||
- `salt://defaults/roles/nginx/server.yaml`
|
||||
|
||||
- `salt://{{ tplroot }}/parameters/roles/nginx/server.yaml`
|
||||
|
||||
- from the `telegraf` YAML files:
|
||||
|
||||
- `salt://defaults/roles/telegraf.yaml`
|
||||
|
||||
- `salt://{{ tplroot }}/parameters/roles/telegraf.yaml`
|
||||
|
||||
Each YAML file and the `defaults` parameters must conform to the
|
||||
following layout:
|
||||
|
||||
- a mandatory `values` key to store the configuration values
|
||||
|
||||
- two optional keys to configure the use of `salt.slsutil.merge`
|
||||
|
||||
- an optional `strategy` key to configure the merging
|
||||
strategy, for example `strategy: 'recurse'`, the default is
|
||||
`smart`
|
||||
|
||||
- an optional `merge_lists` key to configure if lists should
|
||||
be merged or overridden for the `recurse` and `overwrite`
|
||||
strategies, for example `merge_lists: 'true'`
|
||||
#}
|
||||
{%- set stack = defaults | default({"values": {} }, boolean=True) %}
|
||||
|
||||
{#- Build configuration file names based on matchers #}
|
||||
{%- set config_get_strategy = salt["config.get"](tplroot ~ ":strategy", None) %}
|
||||
{%- set matchers = parse_matchers(
|
||||
matchers,
|
||||
config_get_strategy=config_get_strategy,
|
||||
log_prefix=log_prefix
|
||||
)
|
||||
| load_yaml %}
|
||||
|
||||
{%- do salt["log.debug"](
|
||||
log_prefix
|
||||
~ "built-in configuration:\n"
|
||||
~ {"values": defaults | traverse("values")}
|
||||
| yaml(False)
|
||||
) %}
|
||||
|
||||
{%- for param_dir in dirs %}
|
||||
{%- for matcher in matchers %}
|
||||
{#- `slsutil.merge` options from #}
|
||||
{#- 1. the `value` #}
|
||||
{#- 2. the `defaults` #}
|
||||
{#- 3. the built-in #}
|
||||
{%- set strategy = matcher.value
|
||||
| traverse(
|
||||
"strategy",
|
||||
defaults
|
||||
| traverse(
|
||||
"strategy",
|
||||
"smart"
|
||||
)
|
||||
) %}
|
||||
{%- set merge_lists = matcher.value
|
||||
| traverse(
|
||||
"merge_lists",
|
||||
defaults
|
||||
| traverse(
|
||||
"merge_lists",
|
||||
False
|
||||
)
|
||||
)
|
||||
| to_bool %}
|
||||
|
||||
{%- if matcher.type in query_map.keys() %}
|
||||
{#- No value is an empty list, must be a dict for `stack.update` #}
|
||||
{%- set normalized_value = matcher.value | default({}, boolean=True) %}
|
||||
|
||||
{#- Merge in `mapdata.<query>` instead of directly in `mapdata` #}
|
||||
{%- set is_sub_key = matcher.option | default(False) == "SUB" %}
|
||||
{%- if is_sub_key %}
|
||||
{#- Merge values with `mapdata.<key>`, `<key>` and `<key>:lookup` are merged together #}
|
||||
{%- set value = { matcher.query | regex_replace(":lookup$", ""): normalized_value } %}
|
||||
{%- else %}
|
||||
{%- set value = normalized_value %}
|
||||
{%- endif %}
|
||||
|
||||
{%- do salt["log.debug"](
|
||||
log_prefix
|
||||
~ "merge "
|
||||
~ "sub key " * is_sub_key
|
||||
~ "'"
|
||||
~ matcher.query
|
||||
~ "' retrieved with '"
|
||||
~ matcher.query_method
|
||||
~ "', merge: strategy='"
|
||||
~ strategy
|
||||
~ "', lists='"
|
||||
~ merge_lists
|
||||
~ "':\n"
|
||||
~ value
|
||||
| yaml(False)
|
||||
) %}
|
||||
|
||||
{%- do stack.update(
|
||||
{
|
||||
"values": salt["slsutil.merge"](
|
||||
stack["values"],
|
||||
value,
|
||||
strategy=strategy,
|
||||
merge_lists=merge_lists,
|
||||
)
|
||||
}
|
||||
) %}
|
||||
|
||||
{%- else %}
|
||||
{#- Load YAML file matching the grain/pillar/... #}
|
||||
{#- Fallback to use the source name as a direct filename #}
|
||||
|
||||
{%- if matcher.value is sequence and matcher.value | length == 0 %}
|
||||
{#- Mangle `matcher.value` to use it as literal path #}
|
||||
{%- set query_parts = matcher.query.split("/") %}
|
||||
{%- set yaml_dirname = query_parts[0:-1] | join("/") %}
|
||||
{%- set yaml_names = query_parts[-1] %}
|
||||
{%- else %}
|
||||
{%- set yaml_dirname = matcher.query %}
|
||||
{%- set yaml_names = matcher.value %}
|
||||
{%- endif %}
|
||||
|
||||
{#- Some configuration return list #}
|
||||
{%- if yaml_names is string %}
|
||||
{%- set yaml_names = [yaml_names] %}
|
||||
{%- elif yaml_names is sequence %}
|
||||
{#- Convert to strings if it's a sequence of numbers #}
|
||||
{%- set yaml_names = yaml_names | map("string") | list %}
|
||||
{%- else %}
|
||||
{%- set yaml_names = [yaml_names | string] %}
|
||||
{%- endif %}
|
||||
|
||||
{#- Try to load a `.yaml.jinja` file for each `.yaml` file #}
|
||||
{%- set all_yaml_names = [] %}
|
||||
{%- for name in yaml_names %}
|
||||
{%- set extension = name.rpartition(".")[2] %}
|
||||
{%- if extension not in ["yaml", "jinja"] %}
|
||||
{%- do all_yaml_names.extend([name ~ ".yaml", name ~ ".yaml.jinja"]) %}
|
||||
{%- elif extension == "yaml" %}
|
||||
{%- do all_yaml_names.extend([name, name ~ ".jinja"]) %}
|
||||
{%- else %}
|
||||
{%- do all_yaml_names.append(name) %}
|
||||
{%- endif %}
|
||||
{%- endfor %}
|
||||
|
||||
{#- `yaml_dirname` can be an empty string with literal path like `myconf.yaml` #}
|
||||
{%- set yaml_dir = [
|
||||
param_dir,
|
||||
yaml_dirname
|
||||
]
|
||||
| select
|
||||
| join("/") %}
|
||||
|
||||
{%- for yaml_name in all_yaml_names %}
|
||||
{%- set yaml_filename = [
|
||||
yaml_dir.rstrip("/"),
|
||||
yaml_name
|
||||
]
|
||||
| select
|
||||
| join("/") %}
|
||||
|
||||
{%- do salt["log.debug"](
|
||||
log_prefix
|
||||
~ "load configuration values from "
|
||||
~ yaml_filename
|
||||
) %}
|
||||
{%- load_yaml as yaml_values %}
|
||||
{%- include yaml_filename ignore missing %}
|
||||
{%- endload %}
|
||||
|
||||
{%- if yaml_values %}
|
||||
{%- do salt["log.debug"](
|
||||
log_prefix
|
||||
~ "loaded configuration values from "
|
||||
~ yaml_filename
|
||||
~ ":\n"
|
||||
~ yaml_values
|
||||
| yaml(False)
|
||||
) %}
|
||||
|
||||
{#- `slsutil.merge` options from #}
|
||||
{#- 1. the `value` #}
|
||||
{#- 2. the `defaults` #}
|
||||
{#- 3. the built-in #}
|
||||
{%- set strategy = yaml_values
|
||||
| traverse(
|
||||
"strategy",
|
||||
defaults
|
||||
| traverse(
|
||||
"strategy",
|
||||
"smart"
|
||||
)
|
||||
) %}
|
||||
{%- set merge_lists = yaml_values
|
||||
| traverse(
|
||||
"merge_lists",
|
||||
defaults
|
||||
| traverse(
|
||||
"merge_lists",
|
||||
False
|
||||
)
|
||||
)
|
||||
| to_bool %}
|
||||
{%- do stack.update(
|
||||
{
|
||||
"values": salt["slsutil.merge"](
|
||||
stack["values"],
|
||||
yaml_values
|
||||
| traverse("values", {}),
|
||||
strategy=strategy,
|
||||
merge_lists=merge_lists,
|
||||
)
|
||||
}
|
||||
) %}
|
||||
{%- do salt["log.debug"](
|
||||
log_prefix
|
||||
~ "merged configuration values from "
|
||||
~ yaml_filename
|
||||
~ ", merge: strategy='"
|
||||
~ strategy
|
||||
~ "', merge_lists='"
|
||||
~ merge_lists
|
||||
~ "':\n"
|
||||
~ {"values": stack["values"]}
|
||||
| yaml(False)
|
||||
) %}
|
||||
{%- endif %}
|
||||
{%- endfor %}
|
||||
{%- endif %}
|
||||
{%- endfor %}
|
||||
{%- endfor %}
|
||||
|
||||
{%- do salt["log.debug"](
|
||||
log_prefix
|
||||
~ "final configuration values:\n"
|
||||
~ {"values": stack["values"]}
|
||||
| yaml(False)
|
||||
) %}
|
||||
|
||||
{#- Output stack as YAML, caller should use with something like #}
|
||||
{#- `{%- set config = mapstack(matchers=["foo"]) | load_yaml %}` #}
|
||||
{{ stack | yaml }}
|
||||
|
||||
{%- endmacro %}
|
||||
@@ -0,0 +1,222 @@
|
||||
{#- -*- coding: utf-8 -*- #}
|
||||
{#- vim: ft=jinja #}
|
||||
|
||||
{#- Get the `tplroot` from `tpldir` #}
|
||||
{%- set tplroot = tpldir.split("/")[0] %}
|
||||
{%- from tplroot ~ "/libsaltcli.jinja" import cli with context %}
|
||||
|
||||
{%- set query_map = {
|
||||
"C": "config.get",
|
||||
"G": "grains.get",
|
||||
"I": "pillar.get",
|
||||
} %}
|
||||
|
||||
{#- When no part before `@` is provided: #}
|
||||
{#- - define a YAML file path, noted `Y` #}
|
||||
{#- - use `salt["config.get"]`, noted `C` #}
|
||||
{#- - use colon `:` delimiter for querying #}
|
||||
{%- set _defaults = {
|
||||
"type": "Y",
|
||||
"query_type": "C",
|
||||
"query_delimiter": ":"
|
||||
} %}
|
||||
|
||||
{%- macro parse_matchers(
|
||||
matchers,
|
||||
config_get_strategy=None,
|
||||
log_prefix="libmatchers: "
|
||||
) %}
|
||||
{#- matcher format is `[<TYPE>[:<OPTION>[:DELIMITER]]@]<KEY>` #}
|
||||
{#- each matcher has a type: #}
|
||||
{#- - `Y` to build a YAML 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 `YAML` 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` #}
|
||||
{#- With `DELIMITER`, you can choose a different delimiter when doing queries #}
|
||||
{%- 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"]],
|
||||
"query_delimiter": _defaults["query_delimiter"],
|
||||
"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",
|
||||
"query_delimiter": ":"
|
||||
}
|
||||
) %}
|
||||
{%- 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],
|
||||
"query_delimiter": ":"
|
||||
}
|
||||
) %}
|
||||
{%- 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],
|
||||
"query_delimiter": metadatas[2] | default(":", boolean=True)
|
||||
}
|
||||
) %}
|
||||
{%- do salt["log.debug"](
|
||||
log_prefix
|
||||
~ "parse as 3 metadata matcher:\n"
|
||||
~ parsed
|
||||
| yaml(False)
|
||||
| indent(4, True)
|
||||
) %}
|
||||
{%- elif metadatas | length == 4 %}
|
||||
{#- The delimiter is `:` #}
|
||||
{%- do parsed.update(
|
||||
{
|
||||
"type": metadatas[0],
|
||||
"option": metadatas[1],
|
||||
"query_delimiter": ":"
|
||||
}
|
||||
) %}
|
||||
{%- 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 not in ["ssh", "unknown"] and parsed.query_method == "config.get" and config_get_strategy %}
|
||||
{%- set query_opts = {
|
||||
"merge": config_get_strategy,
|
||||
"delimiter": parsed.query_delimiter,
|
||||
} %}
|
||||
{%- set query_opts_msg = (
|
||||
", delimiter='"
|
||||
~ parsed.query_delimiter
|
||||
~ "', merge: strategy='"
|
||||
~ config_get_strategy
|
||||
~ "'"
|
||||
) %}
|
||||
{%- else %}
|
||||
{%- if cli in ["ssh", "unknown"] %}
|
||||
{%- do salt["log.warning"](
|
||||
log_prefix
|
||||
~ "the 'delimiter' and 'merge' options of 'config.get' are skipped when the salt command type is '"
|
||||
~ cli
|
||||
~ "'"
|
||||
) %}
|
||||
{%- endif %}
|
||||
{%- set query_opts = {} %}
|
||||
{%- set query_opts_msg = "" %}
|
||||
{%- endif %}
|
||||
|
||||
{%- do salt["log.debug"](
|
||||
log_prefix
|
||||
~ "lookup '"
|
||||
~ parsed.query
|
||||
~ "' with '"
|
||||
~ parsed.query_method
|
||||
~ "'"
|
||||
~ query_opts_msg
|
||||
) %}
|
||||
{%- set values = salt[parsed.query_method](
|
||||
parsed.query,
|
||||
default=[],
|
||||
**query_opts
|
||||
) %}
|
||||
{%- 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 %}
|
||||
@@ -0,0 +1,18 @@
|
||||
{#- -*- coding: utf-8 -*- #}
|
||||
{#- vim: ft=jinja #}
|
||||
|
||||
{#- Get the relevant values from the `opts` dict #}
|
||||
{%- set opts_cli = opts.get('__cli', '') %}
|
||||
{%- set opts_masteropts_cli = opts | traverse('__master_opts__:__cli', '') %}
|
||||
|
||||
{#- Determine the type of salt command being run #}
|
||||
{%- if opts_cli == 'salt-minion' %}
|
||||
{%- set cli = 'minion' %}
|
||||
{%- elif opts_cli == 'salt-call' %}
|
||||
{%- set cli = 'ssh' if opts_masteropts_cli in ('salt-ssh', 'salt-master') else 'local' %}
|
||||
{%- elif opts_cli %}
|
||||
{%- set cli = 'api' %}
|
||||
{%- else %}
|
||||
{%- set cli = 'unknown' %}
|
||||
{%- endif %}
|
||||
{%- do salt['log.debug']('[libsaltcli] the salt command type has been identified to be: ' ~ cli) %}
|
||||
@@ -0,0 +1,115 @@
|
||||
{%- macro files_switch(
|
||||
source_files,
|
||||
lookup=None,
|
||||
default_files_switch=["id", "os_family"],
|
||||
indent_width=6,
|
||||
use_subpath=False
|
||||
) %}
|
||||
{#-
|
||||
Returns a valid value for the "source" parameter of a "file.managed"
|
||||
state function. This makes easier the usage of the Template Override and
|
||||
Files Switch (TOFS) pattern.
|
||||
Params:
|
||||
* source_files: ordered list of files to look for
|
||||
* lookup: key under "<tplroot>:tofs:source_files" to prepend to the
|
||||
list of source files
|
||||
* default_files_switch: if there's no config (e.g. pillar)
|
||||
"<tplroot>:tofs:files_switch" this is the ordered list of grains to
|
||||
use as selector switch of the directories under
|
||||
"<path_prefix>/files"
|
||||
* indent_width: indentation of the result value to conform to YAML
|
||||
* use_subpath: defaults to `False` but if set, lookup the source file
|
||||
recursively from the current state directory up to `tplroot`
|
||||
Example (based on a `tplroot` of `xxx`):
|
||||
If we have a state:
|
||||
Deploy configuration:
|
||||
file.managed:
|
||||
- name: /etc/yyy/zzz.conf
|
||||
- source: {{ files_switch(
|
||||
["/etc/yyy/zzz.conf", "/etc/yyy/zzz.conf.jinja"],
|
||||
lookup="Deploy configuration",
|
||||
) }}
|
||||
- template: jinja
|
||||
In a minion with id=theminion and os_family=RedHat, it's going to be
|
||||
rendered as:
|
||||
Deploy configuration:
|
||||
file.managed:
|
||||
- name: /etc/yyy/zzz.conf
|
||||
- source:
|
||||
- salt://xxx/files/theminion/etc/yyy/zzz.conf
|
||||
- salt://xxx/files/theminion/etc/yyy/zzz.conf.jinja
|
||||
- salt://xxx/files/RedHat/etc/yyy/zzz.conf
|
||||
- salt://xxx/files/RedHat/etc/yyy/zzz.conf.jinja
|
||||
- salt://xxx/files/default/etc/yyy/zzz.conf
|
||||
- salt://xxx/files/default/etc/yyy/zzz.conf.jinja
|
||||
- template: jinja
|
||||
#}
|
||||
{#- Get the `tplroot` from `tpldir` #}
|
||||
{%- set tplroot = tpldir.split("/")[0] %}
|
||||
{%- set path_prefix = salt["config.get"](tplroot ~ ":tofs:path_prefix", tplroot) %}
|
||||
{%- set files_dir = salt["config.get"](tplroot ~ ":tofs:dirs:files", "files") %}
|
||||
{%- set files_switch_list = salt["config.get"](
|
||||
tplroot ~ ":tofs:files_switch", default_files_switch
|
||||
) %}
|
||||
{#- Lookup source_files (v2), files (v1), or fallback to an empty list #}
|
||||
{%- set src_files = salt["config.get"](
|
||||
tplroot ~ ":tofs:source_files:" ~ lookup,
|
||||
salt["config.get"](tplroot ~ ":tofs:files:" ~ lookup, []),
|
||||
) %}
|
||||
{#- Append the default source_files #}
|
||||
{%- set src_files = src_files + source_files %}
|
||||
{#- Only add to [""] when supporting older TOFS implementations #}
|
||||
{%- set path_prefix_exts = [""] %}
|
||||
{%- if use_subpath and tplroot != tpldir %}
|
||||
{#- Walk directory tree to find {{ files_dir }} #}
|
||||
{%- set subpath_parts = tpldir.lstrip(tplroot).lstrip("/").split("/") %}
|
||||
{%- for path in subpath_parts %}
|
||||
{%- set subpath = subpath_parts[0 : loop.index] | join("/") %}
|
||||
{%- do path_prefix_exts.append("/" ~ subpath) %}
|
||||
{%- endfor %}
|
||||
{%- endif %}
|
||||
{%- for path_prefix_ext in path_prefix_exts | reverse %}
|
||||
{%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
|
||||
{#- For older TOFS implementation, use `files_switch` from the config #}
|
||||
{#- Use the default, new method otherwise #}
|
||||
{%- set fsl = salt["config.get"](
|
||||
tplroot ~ path_prefix_ext | replace("/", ":") ~ ":files_switch",
|
||||
files_switch_list,
|
||||
) %}
|
||||
{#- Append an empty value to evaluate as `default` in the loop below #}
|
||||
{%- if "" not in fsl %}
|
||||
{%- set fsl = fsl + [""] %}
|
||||
{%- endif %}
|
||||
{%- for fs in fsl %}
|
||||
{%- for src_file in src_files %}
|
||||
{%- if fs %}
|
||||
{%- set fs_dirs = salt["config.get"](fs, fs) %}
|
||||
{%- else %}
|
||||
{%- set fs_dirs = salt["config.get"](
|
||||
tplroot ~ ":tofs:dirs:default", "default"
|
||||
) %}
|
||||
{%- endif %}
|
||||
{#- Force the `config.get` lookup result as a list where necessary #}
|
||||
{#- since we need to also handle grains that are lists #}
|
||||
{%- if fs_dirs is string %}
|
||||
{%- set fs_dirs = [fs_dirs] %}
|
||||
{%- endif %}
|
||||
{%- for fs_dir in fs_dirs %}
|
||||
{#- strip empty elements by using a select #}
|
||||
{%- set url = (
|
||||
[
|
||||
"- salt:/",
|
||||
path_prefix_inc_ext.strip("/"),
|
||||
files_dir.strip("/"),
|
||||
fs_dir.strip("/"),
|
||||
src_file.strip("/"),
|
||||
]
|
||||
| select
|
||||
| join("/")
|
||||
) %}
|
||||
{{ url | indent(indent_width, true) }}
|
||||
{%- endfor %}
|
||||
{%- endfor %}
|
||||
{%- endfor %}
|
||||
{%- endfor %}
|
||||
{%- endmacro %}
|
||||
+59
-28
@@ -1,35 +1,66 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim: ft=jinja
|
||||
{#- -*- coding: utf-8 -*- #}
|
||||
{#- vim: ft=jinja #}
|
||||
|
||||
{#- Start with defaults from defaults.sls and osfamilymap.yml #}
|
||||
{% import_yaml 'sysctl/defaults.yml' as default_settings %}
|
||||
{% import_yaml "sysctl/osfamilymap.yml" as os_family_map %}
|
||||
{#- Get the `tplroot` from `tpldir` #}
|
||||
{%- set tplroot = tpldir.split("/")[0] %}
|
||||
{%- from tplroot ~ "/libmapstack.jinja" import mapstack with context %}
|
||||
|
||||
{% set osfam = salt['grains.filter_by'](
|
||||
os_family_map,
|
||||
grain='os_family',
|
||||
) or {}
|
||||
%}
|
||||
{#- Where to lookup parameters source files #}
|
||||
{%- set formula_param_dir = tplroot ~ "/parameters" %}
|
||||
|
||||
{% do salt['defaults.merge'](default_settings['sysctl'], osfam) %}
|
||||
{% do salt['defaults.merge'](
|
||||
default_settings['sysctl'],
|
||||
salt['pillar.get']('sysctl:lookup',{}),
|
||||
{#- List of sources to lookup for parameters #}
|
||||
{#- Fallback to previously used grains plus minion `id` #}
|
||||
{%- set map_sources = [
|
||||
"Y:G@osarch",
|
||||
"Y:G@os_family",
|
||||
"Y:G@os",
|
||||
"Y:G@osfinger",
|
||||
"C@" ~ tplroot ~ ":lookup",
|
||||
"C@" ~ tplroot,
|
||||
"Y:G@id",
|
||||
] %}
|
||||
|
||||
{%- set _map_settings = mapstack(
|
||||
matchers=["map_jinja.yaml"],
|
||||
defaults={
|
||||
"values": {"sources": map_sources}
|
||||
},
|
||||
log_prefix="map.jinja configuration: ",
|
||||
)
|
||||
%}
|
||||
| load_yaml %}
|
||||
|
||||
{#- Apply backwards compatiblility after merge #}
|
||||
{% if default_settings['sysctl']['params'] is defined %}
|
||||
{% do default_settings['sysctl'].pop('params') %}
|
||||
{% endif %}
|
||||
{% if default_settings['sysctl']['params2'] is defined %}
|
||||
{% do default_settings['sysctl'].pop('params2') %}
|
||||
{% endif %}
|
||||
{%- set map_sources = _map_settings | traverse("values:sources") %}
|
||||
{%- do salt["log.debug"](
|
||||
"map.jinja: load parameters from sources:\n"
|
||||
~ map_sources
|
||||
| yaml(False)
|
||||
) %}
|
||||
|
||||
{#- Merge in sysctl pillar #}
|
||||
{% set sysctl_settings = salt['pillar.get'](
|
||||
'sysctl',
|
||||
default=default_settings['sysctl'],
|
||||
merge=True,
|
||||
{#- Load formula parameters values #}
|
||||
{%- set _formula_matchers = ["defaults.yaml"] + map_sources %}
|
||||
|
||||
{%- set _formula_settings = mapstack(
|
||||
matchers=_formula_matchers,
|
||||
dirs=[formula_param_dir],
|
||||
defaults={
|
||||
"values": {},
|
||||
"merge_strategy": salt["config.get"](tplroot ~ ":strategy", None),
|
||||
"merge_lists": salt["config.get"](tplroot ~ ":merge_lists", False),
|
||||
},
|
||||
log_prefix="map.jinja: ",
|
||||
)
|
||||
%}
|
||||
| load_yaml %}
|
||||
|
||||
{#- Make sure to track `map.jinja` configuration with `_mapdata` #}
|
||||
{%- do _formula_settings["values"].update(
|
||||
{
|
||||
"map_jinja": _map_settings["values"]
|
||||
}
|
||||
) %}
|
||||
|
||||
{%- do salt["log.debug"]("map.jinja: save parameters in variable 'mapdata'") %}
|
||||
{%- set mapdata = _formula_settings["values"] %}
|
||||
|
||||
{#- Per formula post-processing of `mapdata` if it exists #}
|
||||
{%- do salt["log.debug"]("map.jinja: post-processing of 'mapdata'") %}
|
||||
{%- include tplroot ~ "/post-map.jinja" ignore missing %}
|
||||
@@ -1,9 +0,0 @@
|
||||
---
|
||||
Suse:
|
||||
pkg: procps
|
||||
|
||||
Debian:
|
||||
pkg: procps
|
||||
|
||||
Gentoo:
|
||||
pkg: sys-process/procps
|
||||
+3
-2
@@ -1,8 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim: ft=sls
|
||||
|
||||
{#- import settings from map.jinja #}
|
||||
{%- from "sysctl/map.jinja" import sysctl_settings with context %}
|
||||
{%- set tplroot = tpldir.split('/')[0] %}
|
||||
{%- from tplroot ~ "/map.jinja" import mapdata with context %}
|
||||
{%- set sysctl_settings = mapdata.sysctl %}
|
||||
|
||||
sysctl-pkg:
|
||||
pkg.installed:
|
||||
|
||||
+3
-2
@@ -1,8 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim: ft=sls
|
||||
|
||||
{#- import settings from map.jinja #}
|
||||
{%- from "sysctl/map.jinja" import sysctl_settings with context %}
|
||||
{%- set tplroot = tpldir.split('/')[0] %}
|
||||
{%- from tplroot ~ "/map.jinja" import mapdata with context %}
|
||||
{%- set sysctl_settings = mapdata.sysctl %}
|
||||
|
||||
{% if sysctl_settings.params is defined %}
|
||||
{% for param_name, param in sysctl_settings.get('params').items() %}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim: ft=yaml
|
||||
---
|
||||
values:
|
||||
sysctl:
|
||||
pkg: procps-ng
|
||||
config:
|
||||
location: '/etc/sysctl.d'
|
||||
...
|
||||
@@ -0,0 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim: ft=yaml
|
||||
---
|
||||
values:
|
||||
sources:
|
||||
- "Y:G@os_family"
|
||||
# keep compatibility with user pillars
|
||||
- "C:SUB@sysctl:lookup"
|
||||
- "C:SUB@sysctl"
|
||||
...
|
||||
@@ -0,0 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim: ft=yaml
|
||||
---
|
||||
values:
|
||||
sysctl:
|
||||
pkg: procps
|
||||
...
|
||||
@@ -0,0 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim: ft=yaml
|
||||
---
|
||||
values:
|
||||
sysctl:
|
||||
pkg: sys-process/procps
|
||||
...
|
||||
@@ -0,0 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# vim: ft=yaml
|
||||
---
|
||||
values:
|
||||
sysctl:
|
||||
pkg: procps
|
||||
...
|
||||
Reference in new issue
Block a user