One of the most important things you can do to make your Puppet manifests clearer and more maintainable is to organize them into modules.
A module is simply a way of grouping related things: for example, a webserver module might include everything necessary for a machine to be a web server: Apache configuration files, virtual host templates, and the Puppet code necessary to deploy these.
Separating things into modules makes it easier to re-use and share code; it's also the most logical way to organize your manifests. In this example we'll create a module to manage memcached, a memory caching system commonly used with web applications.
How to do it…
Here are the steps to create an example module.
Create the following new directories in your Puppet repo:
All manifest files (those containing Puppet code) live in the manifests directory. In our example, the memcached class is defined in the file manifests/init.pp, which will be imported automatically.
Inside the memcached class, we refer to the memcached.conf file:
As we saw in the section on Puppet's file server and custom mount points, the preceding source parameter tells Puppet to look for the file in
MODULEPATH/
memcached/
files/
memcached.conf
There's more…
Learn to love modules, because they'll make your Puppet life a lot easier. They're not complicated. However, practice and experience will help you judge when things should be grouped into modules, and how best to arrange your module structure. Here are a few tips which may help you on the way.
Templates
If you need to use a template as a part of the module, place it in the module's templates directory and refer to it as follows:
Modules can also contain custom facts, custom functions, custom types, and providers. For more information about these, refer to Chapter 8, External Tools and the Puppet Ecosystem.
Autogenerating module layout
You can also use the puppet module generate command to generate the directory layout for new modules, rather than doing it by hand. See the Using public modules section in Chapter 8, External Tools and the Puppet Ecosystem, for more details.
Third party modules
You can download modules provided by other people and use them in your own manifests just like the modules you create. For more on this, see the section on using public modules.
Module organization
For more details on how to organize your modules, see the Puppet Labs website: