Puppet: nested hash/dict,array: how to access in erb?

12,924

Not sure what you are doing but there a few glaring issues like @settings_value_hash is not defined it would be settings_value_hash piped variable not instance variable. @{settings_value_hash['machines']} is in correct as well what happens if you run this

<% @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 %>   

Also why is your initial hash set to a global $settings but you are accessing it through an instance variable @settings.

Share:
12,924
THX
Author by

THX

Updated on June 08, 2022

Comments

  • THX
    THX almost 2 years

    playing with puppet, I have ended up in a nested dictionary/hash - which looks more or less like

    $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"},
        }  
      }
    }
    

    however, I have not managed to parse properly it in the correspondig erb template.

    <% @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 %>   
    

    Thus, I can get the values in my top dictionary without problems, i.e., "ip" and "port",

    However, puppet throws me compile errors, when I try to get to the array "option" or the dict "machines" within the top dict.

    My guess at the moment is, that arrays and dicts/hashes are not "hashable" in Ruby/Puppet, or?

    Cheers and thanks for ideas, Thomas