Ask Your Question

hiera key for nested yaml hash

asked 2015-02-11 12:40:07 -0500

robshep gravatar image


I have the following hiera yaml

      username:  testuser1
      userid:    20001
      groupname: testgroup1
      groupid:   20001
      pw_hash:   $6$abc123...
      home:      /home/testuser1

How can I target users->webapp1 from a hiera() call in the manifest

I.e. I have a parameterized setup that calls my user class with this username.

class my_webapp_user ( $username ) {

        $userdata = hiera( "users::$username") // fails here!

        $groupname = $userdata['groupname']

        group { 'group':
                name => $groupname,
                gid => $userdata['groupid'],
                ensure => present,

        user { 'user':
                name => $username,
                uid => $userdata['userid'],
                ensure => present,
                gid => $groupname,
                shell => '/bin/bash',
                home => $userdata['home'],
                managehome => 'true',
                password => $userdata['pw_hash'],

No matter what syntax I use, it always fails with "Could not find data item users::webapp1"....

is there a way of perform a lookup at a nested depth?

Thank you

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-02-12 12:52:37 -0500

GregLarkin gravatar image

In your situation, you have to perform a hiera lookup on the top-level key ("users") then start digging into the result with additional indexes like so:

$a = hiera('users')
$b = $a['webapp1']
$c = $b['username']
# The following variants are syntax errors
# notice(hiera('users')['webapp1'])
# notice(hiera('users')['webapp1']['username'])

You will have to assign the first lookup to a variable so you can use the array references on it.

edit flag offensive delete link more


Thanks Greg - Having exhausted my search, I figured it may well be this way. Thanks

robshep gravatar imagerobshep ( 2015-02-12 13:08:55 -0500 )edit

Glad to help!

GregLarkin gravatar imageGregLarkin ( 2015-02-12 14:17:18 -0500 )edit

Brilliant; been looking for this for hours. Examples on the web always talk about how to setup nested hiera or how pull out a whole hash/array. I just wanted one nested value. Thx Greg!

cm01 gravatar imagecm01 ( 2016-09-20 22:49:28 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-02-11 12:34:27 -0500

Seen: 2,883 times

Last updated: Feb 12 '15