我有一些.yaml hiera文件:
iptables::test:
ip:
1.1.1.1 : 'adm-1'
2.2.2.2 : 'adm-2'
3.3.3.3 : 'adm-3'我想在inline_template中解析这个文件。我写道:
$variable1 = hiera('iptables::test.ip')
$variable2 = inline_template("<% @variable1.each do |key,value| %>Allow From <%=key %> #<%=value %>\n<% end -%>")但是得到一个错误:
Error 400 on SERVER: Could not find data item iptables::test.ip in any Hiera data file and no default supplied发布于 2020-01-02 18:17:05
您的数据结构或逻辑,或者两者都有问题。我不确定我在这里是否有足够的时间去找出哪一个。
我看到的第一个问题是您的hiera()查找函数无法直接查找嵌套ip哈希。你的希拉钥匙就是iptables::test。您可以通过查找获得该值的全部值,并在需要时进一步解析它。
$variable1 = hiera('iptables::test')如果不需要嵌套的ip哈希,则inline_template()以编写的方式工作。您的数据结构将只是一个散列。
---
iptables::test:
1.1.1.1: adm-1
2.2.2.2: adm-2
3.3.3.3: adm-3如果需要嵌套哈希,则需要嵌套循环。
$variable2 = inline_template("<% @variable1.keys.each do |ip| %><% @variable1[ip].each do |key, value| %>Allow From <%= key %> #<%= value %>\n<% end %><% end %>")把它放在一起来证明:
$ cat test.pp
$variable1 = {
ip => {
'1.1.1.1' => 'adm-1',
'2.2.2.2' => 'adm-2',
'3.3.3.3' => 'adm-3',
},
}
$variable2 = inline_template("<% @variable1.keys.each do |ip| %><% @variable1[ip].each do |key, value| %>Allow From <%= key %> #<%= value %>\n<% end %><% end %>")
notice($variable2)$ puppet apply test.pp
...
Notice: Scope(Class[main]): Allow From 1.1.1.1 #adm-1
Allow From 2.2.2.2 #adm-2
Allow From 3.3.3.3 #adm-3
Notice: Compiled catalog for localhost in environment production in 0.02 seconds
Notice: Applied catalog in 0.01 seconds我在这里的测试没有使用Hiera,因为Hiera只是一种从木偶类外部获取数据的方法。我想以这种方式演示,因为这将使您更容易隔离问题。
https://serverfault.com/questions/996137
复制相似问题