Puppet erb 中的 foreach 循环未正确解释数组中的数组
Array in array not correctly interpreted by foreach loop in Puppet erb
$array_one = [
'one',
'two'
]
$variables = [
'world',
'a',
'b',
$array_one
]
file { '/tmp/test':
content => template("test/test.erb")
}
test.erb
<% @variables.each do |variable| %>
hello_<%= variable %>
<% end %>
结果:
hello_world
hello_a
hello_b
hello_onetwo
虽然它是一个数组,根据notify {$variables:}
:
Notice: b
Notice: /Stage[main]/Test/Notify[b]/message: defined 'message' as 'b'
Notice: world
Notice: /Stage[main]/Test/Notify[world]/message: defined 'message' as 'world'
Notice: one
Notice: /Stage[main]/Test/Notify[one]/message: defined 'message' as 'one'
Notice: a
Notice: /Stage[main]/Test/Notify[a]/message: defined 'message' as 'a'
Notice: two
Notice: /Stage[main]/Test/Notify[two]/message: defined 'message' as 'two'
Notice: Finished catalog run in 0.17 seconds
您的 Ruby 循环完全按照您的要求进行。显然,notify
资源隐含地展平了数组,因此 erb
等价物将是
<% @variables.flatten.each do |variable| %>
hello_<%= variable %>
<% end %>
$array_one = [
'one',
'two'
]
$variables = [
'world',
'a',
'b',
$array_one
]
file { '/tmp/test':
content => template("test/test.erb")
}
test.erb
<% @variables.each do |variable| %>
hello_<%= variable %>
<% end %>
结果:
hello_world
hello_a
hello_b
hello_onetwo
虽然它是一个数组,根据notify {$variables:}
:
Notice: b
Notice: /Stage[main]/Test/Notify[b]/message: defined 'message' as 'b'
Notice: world
Notice: /Stage[main]/Test/Notify[world]/message: defined 'message' as 'world'
Notice: one
Notice: /Stage[main]/Test/Notify[one]/message: defined 'message' as 'one'
Notice: a
Notice: /Stage[main]/Test/Notify[a]/message: defined 'message' as 'a'
Notice: two
Notice: /Stage[main]/Test/Notify[two]/message: defined 'message' as 'two'
Notice: Finished catalog run in 0.17 seconds
您的 Ruby 循环完全按照您的要求进行。显然,notify
资源隐含地展平了数组,因此 erb
等价物将是
<% @variables.flatten.each do |variable| %>
hello_<%= variable %>
<% end %>