Table of contents
1.
Introduction
2.
What is an Environment in a Puppet? 
3.
Uses of Environment
4.
Environment Resources
4.1.
Manifest
4.2.
Classes
4.3.
Modules
5.
Environment Structure
6.
Environment Resource Variables
6.1.
The modulepath
6.2.
The main manifest
6.3.
Hiera Data
6.4.
The config_version script
6.5.
The environment.conf file
7.
Creating Environments
8.
Limitations of Environment
8.1.
Conflict in Exported resources
8.2.
Environment Leakage
9.
Environment Isolation
10.
Frequently Asked Questions
10.1.
What is Puppet and how does it work?
10.2.
Which script paradigm does Puppet use?
10.3.
Can Puppet Server be installed in Windows?
11.
Conclusion
Last Updated: Mar 27, 2024

Environments in Puppet

Author Neha Chauhan
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

DevOps is a word created by the combination of two words - development and operations. It is a combination of the software development team and the operations team. There are many DevOps platforms like - GitHub, Jenkins, Chef, Docker, Red Hat Ansible Automation Platform, Puppet and many more. 

Puppet is a DevOps platform for configuration management. It is a system management tool for automating and centralizing the configuration management process. It uses a declarative model based approach, that is, in Puppet, we describe the state of the system and not the steps needed to get there. It uses a Domain Specific Language (DSL) called the Puppet Code. This code is neither a shell script (like Bash) nor a programming language (like PHP). This code enables Puppet to define infrastructure as code.

Environments in puppet

Puppet has a feature called ‘Environment’. With an environment you can create multiple configurations, like you can create separate states for testing and development. In this article, we will discuss what environments are in Puppet, how to create them and how to create isolation between them. 

What is an Environment in a Puppet? 

Puppet Server (Master) stores the code that defines the desired state. A Puppet Agent (Node) converts the code to commands and runs the code in the system. There is one Master but there can be multiple nodes to one master. In an environment, an agent or a group of agents being controlled by a master server have their own set of parameters (manifests) and a set of modules. The nodes will receive separate code based on the environment they belong to. 

For example, You can create separate environments for testing and development. Not all the changes made for a web server are needed for a database, Environment can be used to create a separation between the database and web server so that the configuration for the web server is not applied to the database. 

Uses of Environment

Uses of Environment

1️⃣ Divided Infrastructure

If the teams are working on different ideas which do not share any code, they can create environments and work on them separately without interrupting each other’s work. 

2️⃣ Create a Permanent Test Environment

The tests are performed by creating a separate environment. These test environments are smaller versions of the entire system. After the developer is satisfied with the results of the test environment they push the code to production. Such test environments are used for longer time periods, mostly through the entire lifecycle of the development process. Puppet allows developers to create test environments that are used for the entire development process or we can say which are permanent. 

3️⃣ Create Temporary Test Environments

Suppose we are testing functionality that does not affect much of the application, so, creating a permanent environment for such case will be a waste. Puppet allows us to create environments with short lifetimes. After we are done with testing we can delete the environment. 

In conclusion, Puppet allows us to divide the infrastructure by creating separate environments. We can create environments that are permanent i.e they are long-lived. We can also delete an environment to create temporary environments for testing functionality that does not affect our entire system. 

Environment Resources

In Puppet we need to declare the resources that will be used during the development at the beginning of the project code. These resources give information about the state of the system. It contains information about the user’s name, which packages should be added or what kind of user or file should exist before starting the project.

Declaring a Resource

Following syntax is used to declare resources

resource_type { 'resource_name'
  attribute => value
  ...
}

 

Example:

user { ‘Harry’:
  ensure     => present,
  uid        => '1000',
  gid        => '1000',
  shell      => '/bin/bash',
  home       => '/home/harry'
}

The above resource declaration describes a user Harry and their specified attributes.

Manifest

Puppet programs are called Manifest. These manifests are written using the Puppet Code and the files have a .pp extension. 

Classes

Like any other programming language, Classes in Puppet are code blocks that can be reused. It makes reading manifests easier. 

Class definition

class example_class {
  ...
  code
  ...
}

Modules

A collection of manifests and data is called a Module. Using modules we can split the puppet code into multiple manifests, which organizes the code. The modules have a specific directory structure. 

Modules are placed in /etc/puppet/modules directory. 

Environment Structure

✔️ It contains a Modules directory. This directory is a part of the environment’s default module path. 

✔️ It also contains a Manifest directory which is also a part of the enviornment’s default main manifest. 

✔️ It can contain an environment.conf file that overrides the values of Modules, manifest, Hiera data and some more configuration settings. 

Environment Resource Variables

The modulepath

All the modules are installed and saved in the modulepath. So, we can say it is a list of directories containing the modules. The directories which are loaded first are given more priority than the later ones. 

The first directory that is loaded by default, is the environment’s directory, followed by the primary server’s basemodulepath setting. 

The main manifest

Like the main method in C/C++, the main manifest is the starting point of the puppet code. It can be a single file or a directory of files. The environment will use the default_manifest setting to determine the main manifest. The value of default_manifest is ./manifests. If the main manifest is empty, Puppet uses a blank main manifest and continues the process. 

Hiera Data

Puppet stores the configuration data in form of key-value pairs. This collection of key-value pairs is called Hiera

Hiera can be thought of as a lookup system to look for data while compiling a catalog.

Each environment has it’s own hiera hierarchy to provide data. 

The config_version script

It is the time at which the catalog was compiled, which is counted as the number of seconds since the 1st of January, 1970. It is a piece of information that uniquely identifies the catalogs and events. 

The environment.conf file

This file overrides the values of the modulepathmain manifestconfig_version and environment_timeout files. 

Creating Environments

🟧 Create a directory with the name as environments

🟧 Give the new environment a name and create a new directory under $codedir/environments with the same name as the environment. 

🟧 To store your puppet code create new manifests and modules directory. 

🟧 Like we have mentioned before, an environment.conf has variables like modulepath, main manifest and config_version. 

🟧 Configure modulepath in the environment.conf file

$ sudo puppet config print modulepath --section server --environment test /etc/puppetlabs/code/environments/test/modules:/etc/puppetlabs/code/modules:/opt/puppetlabs/puppet/modules.

 

🟧 Configure main manifest in the environment.conf file. Prevent any environment from setting it’s own main manifest by using disable_per_environment_manifest

🟧 Enter the path of the script in the config_version setting in environment.conf. After running this script, the output is set as the value of config_versionIf you specify a value here, the global config_version setting from puppet.conf is not used by an environment.

✅ Result

# /etc/puppetlabs/code/environments/test/environment.conf
# Puppet Enterprise requires $basemodulepath; see note below under "modulepath".
modulepath = site:dist:modules:$basemodulepath
# Use our custom script to get a git commit for the current state of the code:
config_version = get_environment_commit.sh


🎊 We have learned what is an environment in Puppet, puppet environment resources and steps to create an environment. 

Limitations of Environment

Conflict in Exported resources

Suppose a resource was exported from another environment, but a resource with the same name already exists in this environment, this will result in a compilation error. To prevent this use separate primary servers for each environment. 

Environment Leakage

In Puppet, the first loaded version of the resource is treated as global. Suppose a resource file is required by several environments and in each environment, a different version of the file is present, because of this wrong resource version will be used for some environments. 

Let’s learn how we can prevent environment leakage in puppet. 

Environment Isolation

Environment Isolation

You can prevent environment leakage by isolation. Create metadata files to ensure environment isolation. 

From the command line terminal run the following command for each environment. 

puppet generate types --environment <ENV_NAME>

 

Everytime you update the environment, run

puppet generate types --environment <ENV_NAME> - -force.

Frequently Asked Questions

What is Puppet and how does it work?

Puppet is a DevOps platform for configuration management. It is a system management tool for automating and centralizing the configuration management process. It uses a declarative model based approach, that is, in Puppet, we describe the state of the system and not the steps needed to get there. It uses a Domain Specific Language (DSL) called the Puppet Code.

Which script paradigm does Puppet use?

Puppet uses the client-server script paradigm with a primary server called the master and many agent servers called nodes. 

Can Puppet Server be installed in Windows?

No. Puppet Server can only be installed in a Linux environment. However, we can create a linux environment using a virtual machine in a windows system to use the Puppet server. 

Conclusion

Yay! You have come to the end of this blog. In this blog, we discussed what are environments in Puppet and the uses of environments. We also discussed about environment resources and how to create them. We finished this blog by looking at some of the limitations of Puppet and how to overcome them using environment isolation. 

Do not stop learning! We recommend you read some of our articles on Puppet - 

🔥 Ansible vs Puppet

🔥 Overview of Puppet Server

🔥 Overview of Puppet Database

Head to the Guided Path on the Coding Ninjas Studio and upskill in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more courses.

If you want to Practice top Coding Problems, attempt mock tests, or read interview experiences, head to the Coding Ninjas Studio, our practice platform.

We wish you Good Luck!🎈 Please upvote our blog 🏆 and help other ninjas grow.

Live masterclass