Ask Your Question
1

hiera key for nested yaml hash

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

robshep gravatar image

Hi

I have the following hiera yaml

users:
   webapp1:
      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
1

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

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')
notice($a)
$b = $a['webapp1']
notice($b)
$c = $b['username']
notice($c)
notice($b['username'])
notice($a['webapp1']['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

Comments

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

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

Glad to help!

GregLarkin gravatar imageGregLarkin ( 2015-02-12 14:17:18 -0600 )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 -0600 )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

Stats

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

Seen: 4,860 times

Last updated: Feb 12 '15