Show Table of Contents
Chapter 2. Building Puppet Modules from Scratch
This chapter explores how to build and test your own Puppet modules. This includes a basic tutorial on creating a Puppet module that deploys a simple web server configuration.
2.1. Examining the Anatomy of a Puppet Module
Before creating our module, we need to understand the components that create a Puppet module.
- Manifests
- Manifests are files that contain code to define a set of resource and their attributes. A resource is any configurable part of a system. Examples of resources include packages, services, files, users and groups, SELinux configuration, SSH key authentication, cron jobs, and more. A manifest defines each required resource using a set of key-value pairs for their attributes. For example:
package { 'httpd': ensure => installed, }This declaration checks if thehttpdpackage is installed. If not, the manifest executesyumand installs it.Manifests are located in themanifestdirectory of a module.Puppet modules also use atestdirectory for test manifests. These manifests are used to test certain classes contained in your official manifests. - Static Files
- Modules can contain static files that Puppet can copy to certain locations on your system. These locations, and other attributes such as permissions, are defined through
fileresource declarations in manifests.Static files are located in thefilesdirectory of a module. - Templates
- Sometimes configuration files require custom content. In this situation, users would create a template instead of a static file. Like static files, templates are defined in manifests and copied to locations on a system. The difference is that templates allow Ruby expressions to define customized content and variable input. For example, if you wanted to configure httpd with a customizable port then the template for the configuration file would include:
Listen <%= @httpd_port %>
Thehttpd_portvariable in this case is defined in the manifest that references this template.Templates are located in thetemplatesdirectory of a module. - Plugins
- Plugins allow for aspects that extend beyond the core functionality of Puppet. For example, you can use plugins to define custom facts, custom resources, or new functions. For example, a database administrator might need a resource type for PostgreSQL databases. This could help the database administrator populate PostgreSQL with a set of new databases after installing PostgreSQL. As a result, the database administrator need only create a Puppet manifest that ensures PostgreSQL installs and the databases are created afterwards.Plugins are located in the
libdirectory of a module. This includes a set of subdirectories depending on the plugin type. For example:/lib/facter- Location for custom facts./lib/puppet/type- Location for custom resource type definitions, which outline the key-value pairs for attributes./lib/puppet/provider- Location for custom resource providers, which are used in conjunction with resource type definitions to control resources./lib/puppet/parser/functions- Location for custom functions.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.