Puppet provides the ability to serve clients different versions of modules and data using environments. You can use environments to provide unique catalogs to different groups of machines, which can be very useful in large projects with many teams. However, a primary usage of environments is to enable testing of changes to Puppet policy without breaking production environments.
Puppet environments have proven so necessary and useful for code testing and deployment that they are enabled by default in Puppet 4. So before we go on to install Puppet modules, let’s configure the production and test environments.
The default environment used by Puppet clients is named production. The Puppet installation creates the production environment during the installation of Puppet. You should find that this already exists:
[vagrant@client~]$ls-l/etc/puppetlabs/code/environments/productiontotal4-rw-r--r--1rootroot879Mar2619:27environment.confdrwxr-xr-x2rootroot6Mar2619:38hieradatadrwxr-xr-x2rootroot6Mar2619:38manifestsdrwxr-xr-x2rootroot6Mar2619:38modules
The default environment is prepared with an environment configuration file, and directories for Hiera data, modules, and manifests.
Now let’s create a test environment you can use to test out new modules or changes to modules prior to implementing them in production. Create the environment like so:
[vagrant@client~]$mkdir-p/etc/puppetlabs/code/environments/test/modules[vagrant@client~]$cd/etc/puppetlabs/code/environments/test[vagrant@clienttest]$mkdirhieradata
This gives us a place to test module changes without breaking any production nodes. Let’s also enable a reminder that we are using the testing environment:
[vagrant@clienttest]$mkdirmanifests[vagrant@clienttest]$$EDITORmanifests/site.pp
The contents of this file should be something like this:
notify{'UsingTest':message=>"Processing catalog from the Test environment.",}
This will give you a warning any time you use this environment, which can be a helpful reminder to move a node back to the production environment when testing is complete.
The Puppet configuration has a convenience setting that allows you to share modules between all environments. The following can be specified in /etc/puppetlabs/puppet/puppet.conf:
[main]# these are both default valuesenvironmentpath=/etc/puppetlabs/code/environmentsbasemodulepath=/etc/puppetlabs/code/modules
The environmentpath variable contains a path under which a directory for each environment will be created.
The basemodulepath variable contains a directory that will be used as a fallback location for modules not found in the environment’s modules/ directory. This allows you to place common and well-tested modules in a single location shared by all environments.
Both of the directory names shown are the default values, and do not need to be specified in the configuration file unless you wish to change them.
While there are many things you can fine-tune and customize with Puppet environments, many of them wouldn’t make much sense at this point in our learning process. These environments are ready for us to use.
We’ll come back to cover environments in much greater detail in Chapter 29.