大网数据广告活动
服务器运维问答

Puppet parse hiera in inline_template

· 52CE 我爱测

I have got some .yaml hiera file with: iptables::test: ip: 1.1.1.1 : 'adm-1' 2.2.2.2 : 'adm-2' 3.3.3.3 : 'adm-3' And i want to parse this file in inline_template. I write: $variable1 = hiera('iptables::test.ip') $variable2 = inli…

问题原文

原帖链接 · perrfect · CC BY-SA 4.0

I have got some .yaml hiera file with:

iptables::test:
  ip:
    1.1.1.1  : 'adm-1'
    2.2.2.2  : 'adm-2'
    3.3.3.3  : 'adm-3'

And i want to parse this file in inline_template. I write:

$variable1 = hiera('iptables::test.ip')
$variable2 = inline_template("<% @variable1.each do |key,value| %>Allow From <%=key %> #<%=value %>\n<% end -%>")

But get an error:

 Error 400 on SERVER: Could not find data item iptables::test.ip in any Hiera data file and no default supplied

已采纳解答

原帖链接 · Aaron Copley · CC BY-SA 4.0

There's a problem with either your data structure or your logic, or both. I am not sure I have enough here to sort out which.

The first issue I see is your hiera() lookup function cannot lookup the nest ip hash directly. Your Hiera key is just iptables::test. You can get the full value of that via lookup and parse it further if needed.

$variable1 = hiera('iptables::test')

If you don't need the nested ip hash, your inline_template() works as written. Your data structure would just be a single hash.

---
iptables::test:
  1.1.1.1: adm-1
  2.2.2.2: adm-2
  3.3.3.3: adm-3

If you need the nested hash, then you need a nested loop.

$variable2 = inline_template("<% @variable1.keys.each do |ip| %><% @variable1[ip].each do |key, value| %>Allow From <%= key %> #<%= value %>\n<% end %><% end %>")

Putting it together to demonstrate:

$ 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

My test here did not use Hiera, because Hiera is just a way to bring data from outside of a Puppet class. I wanted to demonstrate this way because it will be easier for you to isolate your problem.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-12-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。