puppethiera

How to access a value in an hiera hash directly with puppet lookup?


Let's say there is an hiera hash:

foo::bar:
  key1: 'value1'
  key2: 'value2'

Is there any possibility to lookup the value of key1 directly with the lookup function?

I thought of something like:

class myclass (
  String $value1 = lookup('foo::bar::key1'),
) {
...
}

The only way I could solve this, is to lookup the Hash and acces the value later with hash['key1'], but maybe there is a more clean way to do this.


Solution

  • Unfortunately, there isn't a way to do this in Puppet using just lookup.

    Fundamentally, lookup works by looking up Hiera keys, which are unrelated to hash keys. A Hiera key of the form foo::bar::baz is effectively a single unstructured string - it doesn't imply that there are hashes called foo or foo::bar. The :: is just part of the key string - it isn't syntactic sugar for a hash lookup, as is, for example, . in JavaScript.

    lookup is documented at

    https://puppet.com/docs/puppet/latest/hiera_automatic.html