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
-
ensureproperty - 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 ...
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.
Read now
Unlock full access