Vagrant and Puppet, Part 4: Writing a Module

A module is a collection of Puppet resource blocks that work together to carry out a related task. All the blocks are grouped together in a class that is included in your main manifest.

This article will go through creating a very simple module for the Nginx web server.

Step 1: File Structure

Inside the puppet/modules folder we created in Part 3 create a folder named nginx. Inside the nginx folder create a folder named manifests and a folder named files. Inside the nginx/manifests folder create a file called init.pp.

This is the basic file structure for a Puppet module. The module folder will have the same name as your module. Inside it you have a directory for the module’s manifest. The init.pp file in the nginx/manifests folder is the main manifest file for the module.

The files directory is used to store any files that you might call from your manifest. We will not be using it in this article but I want to make you aware of it.

Step 2: Define the Class

Open the puppet/modules/nginx/manifests/init.pp file in an editor and add the following code.

We define the package we want to install by naming the block nginx. We ensure the package is present and we require the update-package-list exec block we defined in our main manifest to be run before this block.

Next we add a service block to make sure the Nginx service is running.

Step 4: Include the Module

This will let Puppet know that we want to include the nginx module. Puppet will look in the module path defined in our Vagrantfile for a folder named nginx then will run the resource blocks found in the init.pp file under the modules manifests directory.

Run vagrant provision. Puppet will see the new blocks and apply the changes. We now have Nginx running on our virtual machine.

Next Steps

This concludes my series on using Vagrant and Puppet. I have only covered very simple tasks to keep it easy to follow.

If you would like to learn more here are some good resources to get you going.

The Puppet online documentation is a good place to start. It will take you through all the different resource blocks and directives you can use as well as more advanced Puppet features. They also offer a free course with a VM to guide you through lessons.

The Forge is a repository where people can submit and download Puppet modules.