将键附加到数组的顶部
Appending a key to the top of an array
我有一些 hiera 与下面的相似(我知道这是无效的 hiera,有两个键...与我无关):
an::example::rule_files:
my_rules:
groups:
- name: my_rules
rules:
- alert: highCPU
expr: CPU > 90
for: 5m
annotations:
summary: "CPU is too high"
description: "CPU should be less than 90"
someone_elses_rules:
groups:
- name: someone_elses_rules
rules:
- alert: highCPU
expr: CPU > 70
for: 5m
annotations:
summary: "CPU is too high"
description: "CPU should be less than 70 on someone else's system"
我正在尝试将其转换为 yaml 文件(关键是文件名)。现在我知道这是无效的 hiera,我可以删除 groups 键来让它工作(正是我所做的),但是当我尝试将它重新插入到数组中时,我无法正确设置格式。这是我正在使用的木偶代码:
$alert_files = hiera('an::example::rule_files'),
$alert_files.each | String $alerts_file_name, Array $alert_config_pre | {
$prefix = [ "groups:" ]
$alert_config = $prefix + $alert_config_pre
file { "/etc/prometheus/${alerts_file_name}.rules":
ensure => file,
content => $alert_config.to_yaml,
}
}
这是我想要的:
cat /etc/prometheus/my_rules.rules
---
groups:
- name: my_rules
rules:
- alert: highCPU
expr: CPU > 90
for: 5m
annotations:
summary: CPU is too high
description: CPU should be less than 90
这是我得到的:
---
- 'groups:'
- name: my_rules
rules:
- alert: highCPU
expr: CPU > 90
for: 5m
annotations:
summary: CPU is too high
description: CPU should be less than 90
如有任何帮助,我们将不胜感激。我觉得这应该很简单,但我并没有真正取得任何进展(我什至无法从单词组中删除引号)。如果这在 hiera 或 puppet 中都是可能的(也许我对 hiera 的定义是错误的),那就太好了;我将以任何方式取得任何进展,我将不胜感激。
这个...
$alert_files = hiera('an::example::rule_files'),
$alert_files.each | String $alerts_file_name, Array $alert_config_pre | {
... 取决于与键 an::example::rule_files
关联的数据是具有字符串键和数组值的哈希。在问题顶部显示的 YAML 中,该项目是带有 String 键和 Hash 值的散列。由于数据似乎与想要的文件内容相匹配,问题似乎不在于 YAML(除了不一致的缩进),而在于 Puppet 代码。
要按照您想要的方式处理您想要的数据,Puppet 代码可能看起来更像这样:
$alert_files = lookup('an::example::rule_files'),
$alert_files.each |String $alerts_file_name, Hash $alert_config| {
file { "/etc/prometheus/${alerts_file_name}.rules":
ensure => 'file',
content => $alert_config.to_yaml,
}
}
请注意,我已从已弃用的 hiera()
函数切换到其替代函数 lookup()
。
我有一些 hiera 与下面的相似(我知道这是无效的 hiera,有两个键...与我无关):
an::example::rule_files:
my_rules:
groups:
- name: my_rules
rules:
- alert: highCPU
expr: CPU > 90
for: 5m
annotations:
summary: "CPU is too high"
description: "CPU should be less than 90"
someone_elses_rules:
groups:
- name: someone_elses_rules
rules:
- alert: highCPU
expr: CPU > 70
for: 5m
annotations:
summary: "CPU is too high"
description: "CPU should be less than 70 on someone else's system"
我正在尝试将其转换为 yaml 文件(关键是文件名)。现在我知道这是无效的 hiera,我可以删除 groups 键来让它工作(正是我所做的),但是当我尝试将它重新插入到数组中时,我无法正确设置格式。这是我正在使用的木偶代码:
$alert_files = hiera('an::example::rule_files'),
$alert_files.each | String $alerts_file_name, Array $alert_config_pre | {
$prefix = [ "groups:" ]
$alert_config = $prefix + $alert_config_pre
file { "/etc/prometheus/${alerts_file_name}.rules":
ensure => file,
content => $alert_config.to_yaml,
}
}
这是我想要的:
cat /etc/prometheus/my_rules.rules
---
groups:
- name: my_rules
rules:
- alert: highCPU
expr: CPU > 90
for: 5m
annotations:
summary: CPU is too high
description: CPU should be less than 90
这是我得到的:
---
- 'groups:'
- name: my_rules
rules:
- alert: highCPU
expr: CPU > 90
for: 5m
annotations:
summary: CPU is too high
description: CPU should be less than 90
如有任何帮助,我们将不胜感激。我觉得这应该很简单,但我并没有真正取得任何进展(我什至无法从单词组中删除引号)。如果这在 hiera 或 puppet 中都是可能的(也许我对 hiera 的定义是错误的),那就太好了;我将以任何方式取得任何进展,我将不胜感激。
这个...
$alert_files = hiera('an::example::rule_files'), $alert_files.each | String $alerts_file_name, Array $alert_config_pre | {
... 取决于与键 an::example::rule_files
关联的数据是具有字符串键和数组值的哈希。在问题顶部显示的 YAML 中,该项目是带有 String 键和 Hash 值的散列。由于数据似乎与想要的文件内容相匹配,问题似乎不在于 YAML(除了不一致的缩进),而在于 Puppet 代码。
要按照您想要的方式处理您想要的数据,Puppet 代码可能看起来更像这样:
$alert_files = lookup('an::example::rule_files'),
$alert_files.each |String $alerts_file_name, Hash $alert_config| {
file { "/etc/prometheus/${alerts_file_name}.rules":
ensure => 'file',
content => $alert_config.to_yaml,
}
}
请注意,我已从已弃用的 hiera()
函数切换到其替代函数 lookup()
。