Skip to Content
Learning Puppet 4
book

Learning Puppet 4

by Jo Rhett
April 2016
Intermediate to advanced
594 pages
12h 53m
English
O'Reilly Media, Inc.
Content preview from Learning Puppet 4

Chapter 14. Improving the Module

There is a tremendous amount of additional features that can be utilized in a module. This chapter will cover the following features:

  • Validating input data to ensure it conforms to expectations
  • Providing data to modules using Hiera
  • Classes and subclasses within the module
  • Definitions that can be reused for processing multiple items
  • Utilizing other modules within your own
  • Documenting modules

Let’s get started.

Validating Input with Data Types

Puppet 4 introduced a new type system that can validate input parameters. This improves code quality and readability.

In older versions of Puppet, it was common to perform input validation like this:

class puppet(
  # input parameters and default values for the class
  $version = 'latest',
  $status  = 'running',
  $enabled = true,
  $datetime = '',
) {
  validate_string( $version )
  validate_string( $status )
  validate_bool( $enabled )
  # can't validate or convert timestamps
...resources defined below...

While this looks easy with three variables, it could consume pages of code if there are a lot of input variables. In my experience with larger modules, it wasn’t uncommon for the first resource defined in a manifest to be down below line 180.

Puppet 4 allows you to define the type when declaring the parameter now, which both shortens the code and improves its readability. When declaring the parameter, simply add the type before the variable name. This declares the parameter and adds validation for the input on a single ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Pro Puppet, Second Edition

Pro Puppet, Second Edition

Spencer Krum, William Van Hevelingen, Ben Kero, James Turnbull, Jeffery McCune
Mastering Puppet 5

Mastering Puppet 5

Ryan Russell-Yates, Jason Southgate

Publisher Resources

ISBN: 9781491907993Purchase bookErrata Page