Vagrant and Puppet, Part 4: Writing a Module

Vagrant

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.

module-file-structure

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.

This defines the Nginx module. All your resource blocks will be placed inside this class.

Step 3: Add Resources

Inside the Nginx class we’ll add the Puppet resource blocks we need to completely install Nginx.

The first block will be a package block telling Puppet to make sure the “nginx” package is installed. Place the following code inside the nginx class.

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.

We use the service name as the name for the block. We ensure the service is running and we require the nginx package block to be run first.

This is all you need for a very simple module to setup Nginx with all the default settings and virtual host.

Step 4: Include the Module

Open the puppet/manifests/init.pp (main manifest) file in an editor.

At the bottom of the file add the following line of code.

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.

You can also view more modules I have written and use in my Puppet-Modules repository on GitHub.

Leave a Reply