Access to one element from a yaml

48 views
Skip to first unread message

Fran Rodríguez

unread,
Feb 25, 2015, 1:52:14 PM2/25/15
to puppet...@googlegroups.com
Hi group,

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

node:
   host1
:
      ip
: xxx.xxx.xxx
   host2
:
      ip
: xxx.xxx.xxx


Those parameter are used to in a template:

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

The question is that i only need the first parameter, i tried some ways to do it but without any luck, so if someone could put me on the correct way or tell me what i would have to do....

Thanks.

Dan White

unread,
Feb 25, 2015, 2:15:50 PM2/25/15
to puppet...@googlegroups.com
More detail is needed to answer the question.

Here are (I hope) some helpful suggestions:

The data you list is a hash.
How does one get the first "element" from a hash ?

In what context are you trying to access the information ?  Manifest ?  Template ?  No in Puppet at all ?

One thing that caught my eye: The top-mose key of the hash is "node" while the template code says "nodes".  That could be the problem.
“Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.”  (Bill Waterson: Calvin & Hobbes)
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/13ed1412-5807-424e-971e-284e588411c7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fran Rodríguez

unread,
Feb 25, 2015, 2:42:44 PM2/25/15
to puppet...@googlegroups.com
Hi there,

Some information is missing, sorry for that, this is the correct yaml:

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, we use Foreman like Hiera, and it uses for 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

Henrik Lindberg

unread,
Feb 27, 2015, 2:19:16 PM2/27/15
to puppet...@googlegroups.com
On 2015-25-02 15:42, Fran Rodríguez wrote:
> Hi there,
>
> Some information is missing, sorry for that, this is the correct yaml:
>
> |
> 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, we use Foreman like Hiera, and it
> uses for a template:
>
> |
> <%=@nodes[0][name]%>
> IPaddr2::<%=@nodes[0]['ip']%>/<%=@nodes[0]['cidr']%>/<%=@nodes[0]['interface']-%>
> |
>

I assume that is not working. The yaml you outline means nodes is a
hash, it has the keys 'host1', and 'host2'. so...

@nodes['host1']['ip']

But, I guess that you do not know the name of the host and want to pick
the "first". The order in a hash is undefined, so you will be picking a
random entry - it may not be the first entry you see in the yaml because
it may change as it is read into memory. (This depends on which version
of ruby you are using, and what kind of processing that has been made
internally on the particular hash).

If you want to get the "first key", you could do this:

key = @nodes.keys[0]

and then

@nodes[key]['ip']

- henrik

>
> And its defined on a manifest:
>
> |
> classheartbeat {
> template_resources =>'site/heartbeat/resources.erb'
> }
> |
>
>
> I only need the first element to apply, in this case. Thanks for the
> fast answer.
>
> Cheers
> On Wednesday, 25 February 2015 15:15:50 UTC+1, LinuxDan wrote:
>
> More detail is needed to answer the question.
>
> Here are (I hope) some helpful suggestions:
>
> The data you list is a hash.
> How does one get the first "element" from a hash ?
>
> In what context are you trying to access the information ? Manifest
> ? Template ? No in Puppet at all ?
>
> One thing that caught my eye: The top-mose key of the hash is "node"
> while the template code says "nodes". That could be the problem.
>
> “Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.” (Bill Waterson: Calvin & Hobbes)
>
>
> On Feb 25, 2015, at 08:52 AM, Fran Rodríguez <fran.c....@gmail.com
> <javascript:>> wrote:
>
>> Hi group,
>>
>> Im trying to get one element from a yaml structure which come from
>> Foreman, the yaml is like this:
>>
>> node :
>> host1 :
>> ip : xxx . xxx . xxx
>> host2 :
>> ip : xxx . xxx . xxx
>>
>>
>> Those parameter are used to in a template:
>>
>> <%= @nodes [ 0 ][ name ] %> IPaddr2:: <%= @nodes [ 0 ][ 'ip' ] %>/
>> <%= @nodes [ 0 ][ 'cidr' ] %>/ <%= @nodes [ 0 ][ 'interface' ] - %>
>
>>
>> The question is that i only need the first parameter, i tried some
>> ways to do it but without any luck, so if someone could put me on
>> the correct way or tell me what i would have to do.....
>>
>> Thanks.
>>
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Puppet Users" group.
>> To unsubscribe from this group and stop receiving emails from it,
>> send an email to puppet-users...@googlegroups.com <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google..com/d/msgid/puppet-users/13ed1412-5807-424e-971e-284e588411c7%40googlegroups.com
>> <https://groups.google.com/d/msgid/puppet-users/13ed1412-5807-424e-971e-284e588411c7%40googlegroups.com?utm_medium=email&utm_source=footer>.
>> For more options, visit https://groups.google.com/d/optout
>> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to puppet-users...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/c9398100-1076-4f40-bf2c-bc0378008534%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/c9398100-1076-4f40-bf2c-bc0378008534%40googlegroups..com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Fran Rodríguez

unread,
Mar 3, 2015, 9:36:12 AM3/3/15
to puppet...@googlegroups.com
Thanks Henrik for the reply, it works.
Reply all
Reply to author
Forward
0 new messages