Chapter 2. Types

The type API expresses resources that can be leveraged from Puppet’s DSL to manage the underlying system. In fact, all native resource types that ship as a part of Puppet (packages, users, files, services, …) are implemented using the type API covered in this chapter. In addition, the type API specifies the attributes for describing each resource. For example:

  • Files have modes.
  • Users have shells.
  • Hosts have IP addresses.

This API offers developers a simple collection of Ruby methods to create resource interfaces that serve as an abstraction layer on which multiple providers can be implemented. It also allows sysadmins to describe system configuration as resources without understanding the procedures developers have implemented via providers.

This section will focus on how custom types are written in Ruby, and will cover the following:

  • How to create a custom type
  • How to add attributes to a type
  • Special attributes

    • Namevars
    • Properties
    • ensure property
    • Parameters
  • Input validation and data transformation
  • Implicit resource dependencies

Defining Puppet Types

Resource types are defined by calling the newtype method on the Puppet::Type class and passing it a symbol representing the name of the type to create.

Ruby Symbols

In Ruby, symbols are constructed by placing a colon before a string (i.e., :hello or :"hello"). Symbols are often used instead of strings in Ruby for performance reasons because symbols, unlike strings, refer to a unique object. Because of this, symbols ...

Get Puppet Types and Providers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.