Hiera arry into ERB template

380 views
Skip to first unread message

Sans

unread,
Apr 16, 2014, 12:31:53 PM4/16/14
to puppet...@googlegroups.com
Hi there,

Trying to figure out how to use  hiera-array in my template. So, this is what I have in .yaml file:


my_coll_list:
    - mon502.local
    - mon522.local

and in my nodes.pp, I have this:

$my_colloctors = hiera_array('my_coll_list')


then in one of my ERB templates, I using that like this:

COLLECTORS = <%= @my_colloctors %>


if i understood correctly, according the Puppet documentation, using array merge lookup, I was expecting a result like this: COLLECTORS = ['mon502.local', 'mon522.local']
but what I'm getting is: COLLECTORS = mon502.localmon522.local


What am I missing here?


Best,
San

José Luis Ledesma

unread,
Apr 16, 2014, 1:50:48 PM4/16/14
to puppet...@googlegroups.com

Hi,

First, hiera_array function if for creating an array looking for the key in every hiera hierarchy, if the values are just on one level, you should use the hiera function.

Second, the problem here is the way erb templates print arrays, so just a matter of format.

Regards,

--
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/ba8d7376-6c45-48d6-80a1-5e5d58af71cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jcbollinger

unread,
Apr 16, 2014, 1:59:27 PM4/16/14
to puppet...@googlegroups.com
On Wednesday, April 16, 2014 11:31:53 AM UTC-5, Sans wrote:
Hi there,

Trying to figure out how to use  hiera-array in my template. So, this is what I have in .yaml file:


No, you are just trying to use an array.  Your template can't tell where the array came from or how it was constructed.

 


my_coll_list:
    - mon502.local
    - mon522.local

and in my nodes.pp, I have this:

$my_colloctors = hiera_array('my_coll_list')




You probably do not want to use hiera_array() for that.  Use plain hiera() instead.  The former traverses your entire hierarchy, returning an array whose elements are the values matching the specified key at each level.  The latter returns the value associated with the requested key at the highest-priority level of your hierarchy that provides one, as whatever data type is modeled by the data (an array, in your example).

 
then in one of my ERB templates, I using that like this:

COLLECTORS = <%= @my_colloctors %>


if i understood correctly, according the Puppet documentation, using array merge lookup, I was expecting a result like this: COLLECTORS = ['mon502.local', 'mon522.local']
but what I'm getting is: COLLECTORS = mon502.localmon522.local

 
  1. You are confusing the Puppet DSL representation of an array with its (Ruby) stringification.  The latter is what your template will produce, and it is simply the concatenation of the string values of the array elements.
  2. The Puppet DSL form of the value of $my_colloctors that you actually have is in all likelihood [['mon502.local', 'mon522.local']].  That is, an array whose only element is an array of your colloctors (whatever those are).  See my previous comments about hiera_array() vs. hiera().
 

What am I missing here?




If you really do want to collect collectors from multiple levels of your hierarchy, then you will need to flatten the result.  Additionally, you will need to format it into your template yourself.  There are many ways to do that, but one would be something like this:

COLLECTORS = [<%= @my_collectors.map { |c| "'#{c.to_s}'"}.join(', ') %>]


John

Sans

unread,
Apr 17, 2014, 5:35:46 AM4/17/14
to puppet...@googlegroups.com
Thanks guys for the explanation; worked just fine.
Is there any other "good" hiera documentation apart from the one in Puppetlab doc?

-San
Reply all
Reply to author
Forward
0 new messages