官术网_书友最值得收藏!

  • Mastering Puppet 5
  • Ryan Russell Yates Jason Southgate
  • 424字
  • 2021-07-16 17:46:11

Using the new Hiera 5 module level data

For quite some time when module writing, we've been using the params.pp pattern. One class in the module, by convention called <MODULENAME>::params, sets the variables for any of the other classes:

class zope::params {
$autoupdate = false,
$default_service_name = 'ntpd',


case $facts['os']['family'] {
'AIX': {
$service_name = 'xntpd'
}
'Debian': {
$service_name = 'ntp'
}
'RedHat': {
$service_name = $default_service_name
}
}
}

So, you can see here that we are using some conditional logic depending on the os::family fact, so that the service_name variable can be set appropriately. We are also exposing the autoupdate variable, and giving it a default value.

This params.pp pattern is an elegant little hack, which takes advantage of Puppet's idiosyncratic class inheritance behavior (using inheritance is generally not recommended in Puppet). Then, any of the other classes in the module inherit from the params class, to have their parameters set appropriately, as shown in the following example:

class zope (
$autoupdate = $zope::params::autoupdate,
$service_name = $zope::params::service_name,
) inherits zope::params {
...
}

Since the release of Hiera 5, we are able to simplify our module complexity considerably. By using Hiera-based defaults, we can simplify our module's main classes, and they no longer need to inherit from params.pp. Additionally, you no longer need to explicitly set a default value with the = operator in the parameter declaration.

Let's look at the equivalent configuration to the params.pp pattern using Hiera 5.

First of all, in order to use this new functionality, the data_provider key needs to be set to the heira value in the module's metadata.json file:

...
"data_provider": "hiera",
...

Next, we need to add a hiera.yaml file to the root directory of the module:

---
version: 5
defaults:
datadir: data
data_hash: yaml_data
hierarchy:
- name: "OS family"
path: "os/%{facts.os.family}.yaml"


- name: "common"
path: "common.yaml"

We can then add three files to the /data directory (note that the datadir setting in the hiera.yaml file). The first file of these three is used to set the AIX service_name variable:

# zope/data/os/AIX.yaml
---
zope::service_name: xntpd

The second file is used to set the Debian service_name variable:

# zope/data/os/Debian.yaml
zope::service_name: ntp

And finally, there is the common file, and Hiera will fall through to this file to find its values if it doesn't find a corresponding operating system file when looking for the service_name setting, or a value for autoupdate when searching the previous two files:

# ntp/data/common.yaml
---
ntp::autoupdate: false
ntp::service_name: ntpd

We will look at Hiera 5 in much more detail in Chapter 4, Hiera 5.

主站蜘蛛池模板: 南开区| 永嘉县| 临潭县| 云浮市| 广州市| 沾益县| 噶尔县| 陈巴尔虎旗| 井研县| 合肥市| 西峡县| 灵武市| 营山县| 莲花县| 米脂县| 石门县| 广水市| 宝丰县| 银川市| 攀枝花市| 红安县| 黄山市| 丹凤县| 彭山县| 册亨县| 信宜市| 财经| 兰州市| 建阳市| 成都市| 广平县| 贞丰县| 北辰区| 大姚县| 鲁甸县| 三河市| 泰来县| 华坪县| 额敏县| 吉隆县| 德惠市|