Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Puppet 5

You're reading from   Mastering Puppet 5 Optimize enterprise-grade environment performance with Puppet

Arrow left icon
Product type Paperback
Published in Sep 2018
Publisher Packt
ISBN-13 9781788831864
Length 292 pages
Edition 1st Edition
Tools
Arrow right icon
Authors (2):
Arrow left icon
Jason Southgate Jason Southgate
Author Profile Icon Jason Southgate
Jason Southgate
Ryan Russell-Yates Ryan Russell-Yates
Author Profile Icon Ryan Russell-Yates
Ryan Russell-Yates
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Authoring Modules FREE CHAPTER 2. Roles and Profiles 3. Extending Puppet 4. Hiera 5 5. Managing Code 6. Workflow 7. Continuous Integration 8. Extending Puppet with Tasks and Discovery 9. Exported Resources 10. Application Orchestration 11. Scaling Puppet 12. Troubleshooting and Profiling 13. Other Books You May Enjoy

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.

You have been reading a chapter from
Mastering Puppet 5
Published in: Sep 2018
Publisher: Packt
ISBN-13: 9781788831864
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime