How to write modules in Puppet

Mayank Jain
3 min readJul 6, 2023

--

A module is a package of collections of code, almost all the puppet code is written in modules. Each module manages a specific task in your infrastructure, such as installing or configuring and maintaining a desired state of infrastructure. Modules are reusable and shareable. You can download and install modules from the Puppet Forge. The Forge contains thousands of modules written by Puppet developers and the open-source community for a wide variety of use cases. Expect to write at least a few of your modules to meet specific needs in your infrastructure.

Modules have a specific directory structure that allows Puppet to find and load classes, defined types, facts, custom types, providers, functions, and tasks. Module names must match the expression: [a-z][a-z0-9_]*. In other words, they can contain only lowercase letters, numbers, and underscores, and begin with a lowercase letter.

Directory Structure of Module

The directory structure of a module contains 2 main folder

  1. manifests
  2. templates

Manifests

A manifest is a file containing Puppet configuration language that describes how resources should be configured. The manifest is the closest thing to what one might consider a Puppet program. It declares resources that define the state to be enforced on a node. Manifests contained in the module’s manifests/ folder each contain one class or defined type. The init.pp manifest is the main class of a module and, unlike other classes or defined types, it is referred to only by the name of the module itself. The manifests folder contains the class files which is responsible for running the modules. The extension of the class file is .pp.

Templates

The templates folder contains the file template which you want to replace in the system when the puppet runs. The extension of the template file in the template folder is .erb.

Directory structure of the module

In the above screenshot, the module name is learning.

the main class of the module is always named init.pp.

my_files.pp is the custom class created in the module.

motd.erb is the template that is used by the module.

How to write the class in puppet manifest?

Class is simply written by the keyword class classname {}. If the classname is the same as the module name then that class is the main class of the module. The extension of the class file is .pp

main class declaration.

To declare the custom class or any other class it will be declared as

class modulename::classname {}

custom class declaration.

This is the simple explanation of modules and classes used in a puppet. In the next blog, we will learn how to write and run the class and manage the infrastructure.

Here is the link to learn how to setup puppet master and agent in a local machine.

--

--

Mayank Jain

Skilled DevOps Engineer with 7.5+ years of hands-on experience supporting, automating, & optimizing mission critical deployments in cloud.