如何在hiera_哈希循环中设置puppet类变量?
hiera数据如何在hiera_哈希循环中设置puppet类变量?,puppet,hiera,Puppet,Hiera,hiera数据 ae::namespace_by_fqdn_pattern: '((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((client))([0-9]{2}).pp-devcos-ae.us-central1.gcp.dev.blah.com': '/test/blah/regression/client' '((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((server))([0-9]{2}).
ae::namespace_by_fqdn_pattern:
'((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((client))([0-9]{2}).pp-devcos-ae.us-central1.gcp.dev.blah.com': '/test/blah/regression/client'
'((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((server))([0-9]{2}).pp-devcos-ae.us-central1.gcp.dev.blah.com': '/test/blah/regression/server'
阶级
。。。这里有人知道如何正确操作吗?试图有条件地重写$ae::namespace变量,但我太无知了,不知道如何让它工作:(
循环和模式匹配位起作用。只是不知道如何从hiera_hash()中正确设置该类变量。每个循环。因此我找到的解决方案是将hiera数据更改为:
ae::namespace : '/test/blah/regression'
ae::namespace_patterns: ['((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((client))([0-9]{2}).pp-devcos-ae.us-central1.gcp.dev.blah.com', '((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((server))([0-9]{2}).pp-devcos-ae.us-central1.gcp.dev.blah.com']
ae::namespace_by_pattern:
'((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((client))([0-9]{2}).pp-devcos-ae.us-central1.gcp.dev.blah.com': '/test/paypal/regression/client'
'((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((server))([0-9]{2}).pp-devcos-ae.us-central1.gcp.dev.blah.com': '/test/paypal/regression/server'
然后将类代码设置为:
$pattern = hiera_hash('ae::namespace_patterns').filter |$pattern| {
$facts['networking']['fqdn'] =~ $pattern
}
if length($pattern) {
$namespace = hiera('ae::namespace_by_pattern')[$pattern[0]]
} else {
$namespace = hiera('ae::namespace')
}
当然仍然有更好的答案。这正是我自己的黑客通过多次尝试和错误所产生的可行的结果
如何在hiera_哈希循环中设置puppet类变量
不能。与each()关联的块
call为每次迭代建立一个局部范围。中的变量赋值应用于该局部范围,因此仅在块的一次执行期间有效。在变量的生命周期内,您无论如何都无法为变量赋值,因此即使您可以从each()中为类变量赋值
call,则很难使用该功能(而且您的方法不起作用)
有几种方法可以在不修改数据形式的情况下解决问题。例如,您可以利用该功能,但我个人的建议是使用该功能,如下所示:
$namespace = lookup('ae::target_host_patterns').reduce(lookup('ae::namespace')) |$ns, $entry| {
$facts['networking']['fqdn'].match($entry[0]) ? { true => $entry[1], default => $ns }
}
这与您的原始代码似乎要做的几乎完全相同,只是所选名称空间由reduce()返回
调用,由类作用域中的代码分配给变量,而不是lambda试图直接分配它。还要注意的是,它不仅负责测试模式,还负责在没有模式匹配时分配默认名称空间,因为您只能分配给名称空间
变量一次
$pattern = hiera_hash('ae::namespace_patterns').filter |$pattern| {
$facts['networking']['fqdn'] =~ $pattern
}
if length($pattern) {
$namespace = hiera('ae::namespace_by_pattern')[$pattern[0]]
} else {
$namespace = hiera('ae::namespace')
}
$namespace = lookup('ae::target_host_patterns').reduce(lookup('ae::namespace')) |$ns, $entry| {
$facts['networking']['fqdn'].match($entry[0]) ? { true => $entry[1], default => $ns }
}