Puppet:嵌套的hash/dict,数组:如何在erb中访问?

THX*_*THX 3 ruby arrays dictionary erb puppet

玩木偶,我最终在一个嵌套字典/哈希 - 看起来或多或少像

$settings =
{
  "var1" => 
  {
    "ip" => "0.0.0.0",
    "port" => "1234",
    "option" => ["foo", "bar"],
    "machines" =>
    {
      "maschine-1" => { "ip" => "1.2.3.4", "port" => "1234"},
      "maschine-2" => { "ip" => "1.2.3.5", "port" => "1235"},
    }  
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,我还没有设法在对应的erb模板中正确解析它.

<% @settings.each_pair do |settings_key, settings_value_hash| %>
<%= settings_value_hash['ip']%>:<%= settings_value_hash['port'] %>

option <% @settings_value_hash['option'].each do |option| -%> <%= option  %>   <% end -%>

<% @{settings_value_hash['machines']}.each_pair do |machine_key, machine_value_hash| %>
  server <%= machine_key %>  <%= machine_value_hash['ip'] %>:<%= machine_value_hash['port'] %>
<% end %>   
Run Code Online (Sandbox Code Playgroud)

因此,我可以毫无问题地获取我的顶级词典中的值,即"ip"和"port",

但是,当我试图到达顶部字典中的数组"选项"或字典"机器"时,puppet会抛出编译错误.

我现在的猜测是,数组和dicts /哈希在Ruby/Puppet中不是"可以",或者?

干杯和感谢你的想法,托马斯

eng*_*nky 7

不知道你在做什么,但有一些明显的问题,如未@settings_value_hash定义它将是settings_value_hash管道变量而不是实例变量.@{settings_value_hash['machines']}是正确的,如果你运行它会发生什么

<% @settings.each do |settings_key, settings_value_hash| %>
  <%= "#{settings_value_hash['ip']}:#{settings_value_hash['port']}" %>
  option 
  <% settings_value_hash['option'].each do |option| %> 
    <%= option  %>   
  <% end %>
  <% settings_value_hash['machines'].each do |machine_key, machine_value_hash| %>
    server <%= "#{machine_key} #{machine_value_hash['ip']}:#{machine_value_hash['port']}" %>
  <% end %>
<% end %>   
Run Code Online (Sandbox Code Playgroud)

另外,为什么您的初始哈希设置为全局,$settings但您通过实例变量访问它@settings.

  • 这是一个木偶主义.`$ settings =`是Puppet DSL的一个表达式.变量以`$`Perl样式为前缀.在由Puppet评估的ERB模板中,这些变量可用作实例变量. (2认同)