rubytemplatespuppettheforeman

Access to the first element of a Foreman's yaml


Im trying to get one element from a yaml structure which come from Foreman, the yaml is like this:

nodes:
   host1:
      ip: xxx.xxx.xxx
      cdir: bbb.bbb.bbb
      interface: aaa
   host2:
      ip: yyy.yyy.yyy
      cdir: bbb.bbb.bbb
      interface: aaa

This yaml is declarated on a Foreman,and it uses in a template:

<%= @nodes[0][name] %> IPaddr2::<%= @nodes[0]['ip'] %>/<%= @nodes[0]['cidr'] %>/<%= @nodes[0]['interface'] -%>

And its defined on a manifest:

class heartbeat {
   template_resources => 'site/heartbeat/resources.erb'
}

I only need the first element to apply, in this case. Thanks for the fast answer.

Cheers


Solution

  • I found the answer,

    <% key = @nodes.keys[0] -%>
    <%= key %> IPaddr2::<%= @nodes[key]['ip'] %>/<%= @nodes[key]['cidr'] %>/<%= @nodes[key]['interface'] %>
    

    Cheers