The Puppet lookup function

The lookup function uses Hiera to retrieve a value for a given key.

By default, the lookup function returns the first value found and fails compilation if no values are available. You can also configure the lookup function to merge multiple values into one.

When looking up a key, Hiera searches up to four hierarchy layers of data, in the following order:

  1. Global hierarchy.
  2. The current environment's hierarchy.
  3. The indicated module's hierarchy, if the key is of the form <MODULE NAME>::<SOMETHING>.
  4. If not found and the module's hierarchy has a default_hierarchy entry in its hiera.yaml — the lookup is repeated if steps 1-3 did not produce a value.

Hiera checks the global layer before the environment layer. If no global hiera.yaml file has been configured, Hiera defaults are used. If you do not want it to use the defaults, you can create an empty hiera.yaml file in /etc/puppetlabs/puppet/hiera.yaml.

Default global hiera.yaml is installed at /etc/puppetlabs/puppet/hiera.yaml.

Arguments

You must provide the key's name. The other arguments are optional.

You can combine these arguments in the following ways:

  • lookup( <NAME>, [<VALUE TYPE>], [<MERGE BEHAVIOR>], [<DEFAULT VALUE>] )

  • lookup( [<NAME>], <OPTIONS HASH> )

  • lookup( as above ) |$key| { <VALUE> } # lambda returns a default value

Arguments in [square brackets] are optional.

Giving a hash of options containing default_value at the same time as giving a lambda means that the lambda wins. The rationale for allowing this is that you might be using the same hash of options multiple times, and you might want to override the production of the default value. A default_values_hash wins over the lambda if it has a value for the looked up key.
Arguments accepted by lookup:

  • <NAME> (String or Array) - The name of the key to look up. This can also be an array of keys. If Hiera doesn't find anything for the first key, it tries with the subsequent ones, only resorting to a default value if none of them succeed.
  • <VALUE TYPE> (data Type) - A data type that must match the retrieved value; if not, the lookup (and catalog compilation) fails. Defaults to Data which accepts any normal value.
  • <MERGE BEHAVIOR> (String or Hash; see Merge behaviors) - Whether and how to combine multiple values. If present, this overrides any merge behavior specified in the data sources. Defaults to no value; Hiera uses merge behavior from the data sources if present, otherwise it does a first-found lookup.
  • <DEFAULT VALUE> (any normal value) - If present, lookup returns this when it can't find a normal value. Default values are never merged with found values. Like a normal value, the default must match the value type. Defaults to no value; if Hiera can't find a normal value, the lookup (and compilation) fails.
  • <OPTIONS HASH> (Hash) - Alternate way to set the arguments above, plus some less common additional options. If you pass an options hash, you can't combine it with any regular arguments (except <NAME>). An options hash can have the following keys:
    • 'name' - Same as <NAME> (argument 1). You can pass this as an argument or in the hash, but not both.
    • 'value_type' - Same as <VALUE TYPE>.
    • 'merge' - Same as <MERGE BEHAVIOR>.
    • 'default_value' - Same as <DEFAULT VALUE> .
    • 'default_values_hash' (Hash) - A hash of lookup keys and default values. If Hiera can't find a normal value, it checks this hash for the requested key before giving up. You can combine this with default_value or a lambda, which is used if the key isn't present in this hash. Defaults to an empty hash.
    • 'override' (Hash) - A hash of lookup keys and override values. Puppet checks for the requested key in the overrides hash first. If found, it returns that value as the final value, ignoring merge behavior. Defaults to an empty hash.
    • lookup - can take a lambda, which must accept a single parameter. This is yet another way to set a default value for the lookup; if no results are found, Puppet passes the requested key to the lambda and use its result as the default value.

Merge behaviors

Hiera uses a hierarchy of data sources, and a given key can have values in multiple sources. Hiera can either return the first value it finds, or continue to search and merge all the values together. When Hiera searches, it first searches the global layer, then the environment layer, and finally the module layer — where it only searches in modules that have a matching namespace. By default (unless you use one of the merge strategies) it is priority/"first found wins", in which case the search ends as soon as a value is found.

Data sources can use the lookup_options metadata key to request a specific merge behavior for a key. The lookup function uses that requested behavior unless you specify one.
Examples:

Default values for a lookup:

(Still works, but deprecated)

hiera('some::key', 'the default value')
(Recommended)
lookup('some::key', undef, undef, 'the default value')

Look up a key and returning the first value found:

lookup('ntp::service_name')
A unique merge lookup of class names, then adding all of those classes to the catalog:
lookup('classes', Array[String], 'unique').include
A deep hash merge lookup of user data, but letting higher priority sources remove values by prefixing them with:
lookup( { 'name'  => 'users',
          'merge' => {
            'strategy'        => 'deep',
            'knockout_prefix' => '--',
          },
})