Loading Hiera into array/hash

954 views
Skip to first unread message

Douglas Garstang

unread,
Aug 27, 2012, 5:14:08 PM8/27/12
to Puppet Users
This is driving me crazy. I'm trying to load the nodes array into a
puppet array with hiera. Hiera seems to always flatten an array or a
hash to a string, with no delimiter in between, which makes it hard to
split into an array later on.

The hiera_hash and hiera_array functions only take a single key, so
there's no way that I can see to drill down into a multi level yaml
file like below.

glusterfs_volumes:
gfsvol01:
master_server: gfs01.us1.xxx.com
transport: tcp
name: gfsvol01
replicas: 1
nodes:
- gfs01.us1.xxx.com
- gfs02.us1.xxx.com
brick_store: /var/bricks

How can I do this?

Doug

jcbollinger

unread,
Aug 28, 2012, 10:21:07 AM8/28/12
to puppet...@googlegroups.com

Hiera does not flatten arrays and hashes, but there are several ways you can make Puppet do so after the value is loaded.  What you describe sounds exactly like what I would expect to see in such a case.

In the first place, you should normally use the hiera() function to retrieve values of all types.  The hiera_array() and hiera_hash() functions are for collecting the values of the specified key across all levels of your hierarchy, and returning them in array or hash form, respectively.  Among other things, that means that the structure of the values returned from hiera_array() and hiera_hash() will always differ from the structure the hiera() function would return for the same key: at minimum, the former two functions wrap the value returned by the latter in an outer array or hash.

What you are looking for is probably something like this:

  $volumes = hiera('glusterfs_volumes')
  $vol01 = $volumes['gfsvol01']
  $v01_master = $vol01['master_server']

  # I think this works, too:
  $v01_transport = $volumes['gfsvol01']['transport']

etc.

Be sure not interpolate array or hash values into strings, or to assign them to parameters that assume a string value; these are some of the ways to inadvertently flatten your data.

John

Reply all
Reply to author
Forward
0 new messages