Search code examples
puppethiera

How hiera works?


I have the following hiera data files (yml).

  • hiera/data/domain/abcd.com
  • hiera/data/role/webserver.yml

lets say i have a puppet node which has 'webserver' role assigned and its hostname is abcd.com

Now under - hiera/data/domain/abcd.com file, i have this

config::xyz_category::name: 'bbbb'

and i have the following data under - hiera/data/role/webserver.yml

config::xyz_category:
  username: 'aaaa'

my hiera data lookup order is like below (hiera.yml)

:backends: yaml
:logger: console
:merge_behavior: deeper
:yaml:
  :datadir: /etc/puppet/branches/hiera/data
:hierarchy:
   - fqdn/%{::fqdn}
   - role/%{::rolename}
   - domain/%{::domain}
   - defaults

So my question is lets say i have a module written for this 'webserver' role.

Inside that module, can i call hiera data like below?

$config = hiera('config::xyz_category', {})

so that

$config['name'] should return 'bbbb'
$config['username'] should return 'aaaa'

Solution

  • No, not exactly.

    An Hiera priority lookup, such as performed by the hiera() function, looks up a complete value associated with the key, taking the whole value from highest-priority hierarchy level that provides one. In the case you describe, that value will be a hash with exactly one key, 'username'.

    On the other hand, if you want to assemble a hash value from multiple levels of the data hierarchy, then you're looking for a hash-merge lookup. Hiera supports that, but you need to use the correct lookup function:

    $config = hiera_hash('config::xyz_category', {})
    

    Note in particular that the difference between hiera() and hiera_hash() (and hiera_array()) is primarily about lookup strategy, not about the type of the return value. The plain hiera() function can return scalars, hashes, and arrays, as appropriate.