4 min read

In this article by Scott Coulton, author of the book, Puppet for Containerization, we are going to look at how to construct a Puppet module with the correct file structure, unit tests, and gems.

One of the most important things in development is having a solid foundation. Writing a Puppet module is no different. This topic is extremely important for the rest of the book, as we will be reusing the code over and over again to build all our modules. Let’s look at how to build a module with the Puppet module generator.

(For more resources related to this topic, see here.)

The Puppet module generator

One of the best things about working with Puppet is the number of tools out there both from the community and from Puppetlabs itself. The Puppet module generator is a tool that is developed by Puppetlabs and follows the best practices to create a module skeleton. The best thing about this tool is that it is bundled with every Puppet agent install. So, we don’t need to install any extra software. So, let’s log in to our vagrant box that we built in the last chapter. Let’s change the directory to the root of our Vagrant repo and then use the vagrant up && vagrant ssh command. Now that we are logged in to the box, let’s change the directory to root (sudo -i) and change the directory to /vagrant. The reason for this is that this folder will be mapped to our local box. Then, we can use our favorite text editor later in the chapter. Once we’re in /vagrant, we can run the command to build our Puppet module skeleton. The puppet module generate <AUTHOR>-consul command for me will look like this: puppet module generate scottyc-consul.

The script will then ask a few questions such as the version, author name, description, where the source lives, and so on. These are very important questions that are to be considered when you want to publish a module to the Forge (https://forge.puppetlabs.com/), but for now, let’s just answer the questions as per the following example:

Puppet for Containerization

Now that we have our Puppet module skeleton, we should look at what the structure looks like:

Puppet for Containerization

Now, we are going to add a few files to help us with unit tests. The first file is .fixtures.yml. This file is used by spec-puppet to pull down any module dependencies into the spec/fixtures directory when we run our unit tests. For this module, the .fixtures.yml file should like as shown in the following screenshot:

Puppet for Containerization

The next file that we are going to add is a .rspec file. This is the file that spec-puppet uses when it requires spec_helper and sets the pattern for our unit test folder structure. The file contents should look like as shown in this screenshot:

Puppet for Containerization

Now that we have our folder structure, let’s install the gems that we need to run our unit tests. My personal preference is to install the gems on the vagrant box; if you want to use your local machine, that’s fine as well. So, let’s log in to our vagrant box (cd into the root of our Vagrant repo and use the vagrant ssh command and then change the directory to root using sudo -i). First, we will install Ruby with yum install -y ruby. Once that is complete, let’s cd into /vagrant/<your modules folder> and then run gem install bundler && bundle install. You should get the following output after this:

Puppet for Containerization

As you can see from the preceding screenshot, we got some warnings. This is because we ran gem install as the root. We would not do that on a production system, but as this is our development box, it wont pose an issue. Now that we have all the gems that we need for our unit tests, let’s add some basic facts to /spec/classes/init_spec.rb. The facts we are going to add are osfamily and operatingsystemrelease. So, the file will look like as shown in this screenshot:

Puppet for Containerization

The last file that we will edit is the metadata.json file in the root of the repo. This file defines our module dependencies. For this module, we have one dependency, that is, docker, so we need to add that at the bottom of the metadata.json file, as shown in the following screenshot:

Puppet for Containerization

The last thing we need to do is put everything in its place inside our Vagrant repo. We do that by creating a folder called modules in the root of our Vagrant repo. Then, we issue the mv <AUTHOR>-consul/ modules/consul command. Note that we removed the author name because we need the module to replicate what it would look like on a Puppet master. Now that we have our basic module skeleton ready, we can start with some coding.

Summary

So in this article we covered how to build a module with the Puppet module generator. For more information on Puppet, you can check other books by Packt Publsihing, mentioned as follows:

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here