Puppet Style Guide

Blog of Puppet Style Guide

When starting with the Puppet language, DevOps engineers want to dive right in and start making Puppet work for them. The issue, as old as programming itself, is that they or other programmers will need to be able to read and maintain their code in the future.

Puppet’s style guide has been developed to promote consistent formatting in the Puppet language, providing a common pattern, design, and style to follow when developing modules. As noted in the style guide: “This consistency in code and module structure makes it easier to update and maintain the code.”

A module can be considered as a small piece of software. By creating modular software and keeping things simple, the code is much more readable and easier to work with when changes need to be made or errors arise. The current Puppet style guide is Version 5.5 and applies to Puppet 4 and later. Puppet 3 is no longer supported, but we include some Puppet 3 guidelines in case you're maintaining older code.

Considerations

Some of the styling conventions from puppet 3 or other programming languages are not used/supported in puppet 4. If members of your team are prone to certain habits in how they write code, then there will be a small learning curve to get passed. Another small issue is that characters such as tabbing or using spacebar will be read differently and could lead to a lot of warnings and errors as we will see in figure 2.

Fix

To ensure that a Puppet manifest conforms to the style guide there are a few options available – each with varying degrees of frustration. The first option is to simply write your code as-is based on prior programming knowledge and hope that it is readable to everyone else that has to use the code. This is clearly not the best option for a small team, much less for a larger group. The second option is to use the Puppet style guide available here. This guide has all of the styling requirements for Puppet 4 and later with some information on Puppet 3. The guide is comprehensive and covers every styling scenario I have encountered so far.

To help us check our use of the Puppet style guide, we use the “puppet-lint” command. Puppet-lint scans a manifest and determines which lines conform to the style guide by showing warnings and errors. Puppet “parser validate” can also be used to validate the syntax of one or more puppet manifests.

Team working on a plan

Once a user has the basics of the style guide down, the number of change specified by puppet-lint should be more manageable as you learn the Puppet styling guide. In this way puppet-lint and puppet-validate can reinforce your understanding of the style guide and your code will be more maintainable.

The following figure indicates puppet-lint being used on two different files: user.pp and user_ty.pp.

Planning Book

In the first file, the output is empty, meaning that there were no styling errors detected. However, the second file has a number of errors that, while they may not prevent the manifest from being applied, conflict with the style guide and will generate warning and error messages with accompanying explanations to fix them.

Planning Book

We can now see that most of the spacing warnings/errors have been dealt with after running the puppet-lint command post edits.

As new Puppet users get started, they can take advantage of tools like puppet-lint and puppet-validate to verify their formatting and compliance with the Puppet Style guide. This will ensure that they and others can read and maintain their code in the future.

Share this Post:

Posted by Tyrone Pascal

Tyrone develops web-based applications focusing on custom application development, automated testing for quality assurance process improvements, and website analytics.