Module data with a one-off custom Hiera backend
With Hiera 5's custom backend system, you can convert and existing params class to a hash-based Hiera backend.
To create a Hiera backend, create a function written in the Puppet language that returns a hash.
Using the params
class as a starting point:
# ntp/functions/params.pp function ntp::params( Hash $options, # We ignore both of these arguments, but Puppet::LookupContext $context, # the function still needs to accept them. ) { $base_params = { 'ntp::autoupdate' => false, # Keys have to start with the module's namespace, which in this case is `ntp::`. 'ntp::service_name' => 'ntpd', # Use key names that work with automatic class parameter lookup. This # key corresponds to the `ntp` class's `$service_name` parameter. } $os_params = case $facts['os']['family'] { 'Debian': { { 'ntp::service_name' => 'ntp' } } default: { {} } } # Merge the hashes, overriding the service name if this platform uses a non-standard one: $base_params + $os_params }
hiera.yaml
. A simple backend like this one
doesn’t require path
, datadir
,
or options
keys. You have a choice of adding it to the default_hierarch
if you want the exact same behaviour as with the
earlier params.pp
pattern, and use the regular hierarchy
if you want the values to be merged with values of higher
priority when a merging lookup is specified. You can split up the key-values so that some are
in the hierarchy
, and some in the default_hierarchy
, depending on whether it makes sense to merge a value or
not.
Here we add it to the regular hierarchy:
# ntp/hiera.yaml --- version: 5 hierarchy: - name: "NTP class parameter defaults" data_hash: "ntp::params" # We only need one hierarchy level, because one function provides all the data.
With Hiera-based defaults, you can simplify your module’s main classes:
They do not need to inherit from any other class.
You do not need to explicitly set a default value with the
=
operator.Instead APL comes into effect for each parameter without a given value. In the example, the function
ntp::params
is called to get the default params, and those can then be either overridden or merged, just as with all values in Hiera.# ntp/manifests/init.pp class ntp ( # default values are in ntp/functions/params.pp $autoupdate, $service_name, ) { ... }