数组中的数组未被Puppet erb中的foreach循环正确解释

时间:2015-01-14 20:30:26

标签: arrays variables templates foreach puppet

  $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

1 个答案:

答案 0 :(得分:3)

你的Ruby循环正是你所要求的。显然,notify资源隐式地使数组变平,因此erb等价物将是

<% @variables.flatten.each do |variable| %>
  hello_<%= variable %>
<% end %>