Puppet is an open-source configuration management tool that automates the deployment, configuration, and management of servers and other infrastructure. It works by having a central server (the Puppet master) compile a catalog of desired system states based on manifests and modules, which agent nodes then apply to bring themselves into compliance.
Background: The Problem Puppet Solves
Before tools like Puppet, system administrators configured servers manually or with ad-hoc scripts. This approach led to configuration drift—servers that were supposed to be identical gradually diverged as changes were made by hand. It also made scaling difficult: provisioning a new server meant repeating a long list of manual steps, with plenty of room for error.
Puppet, first released in 2005, introduced a declarative model. Instead of writing scripts that describe how to configure a system (imperative), you write code that describes what the system should look like (declarative). Puppet then figures out how to make that happen. This model is known as infrastructure as code (IaC).
Core Components of Puppet
Puppet's architecture consists of several key parts:
- Puppet master (server): Compiles manifests into a catalog and serves it to agents. In newer versions, this role can be filled by Puppet Server, a JVM-based application.
- Puppet agent: Runs on managed nodes, periodically requests its catalog from the master, and applies it.
- Manifests: Files with a .pp extension containing Puppet code that describes resources and their desired state.
- Resources: The basic building blocks—a file, a package, a service, a user, etc. Each resource declares a type, a title, and attributes.
- Modules: Reusable bundles of manifests, files, and templates that encapsulate a piece of configuration (e.g., Apache, Nginx, MySQL).
- Facter: A tool that gathers system facts (OS, IP address, memory) which can be used in manifests to make decisions.
- Hiera: A key-value lookup system for separating data from code, making manifests more reusable.
How Puppet Works: Step by Step
The typical Puppet run follows a predictable cycle:
- Fact gathering: The agent runs Facter to collect system information (facts) such as operating system, hostname, and network interfaces.
- Catalog request: The agent sends these facts to the Puppet master and requests its catalog.
- Catalog compilation: The master uses the facts, along with manifests, modules, and Hiera data, to compile a catalog—a detailed description of the desired state for that specific node.
- Catalog transfer: The compiled catalog is sent back to the agent.
- Application: The agent compares the current state of the system with the catalog and makes any necessary changes. For example, if a package is missing, it installs it; if a file's content differs, it rewrites it; if a service is stopped, it starts it.
- Report: The agent sends a report back to the master, detailing what was changed, what was already in the desired state, and any failures.
This cycle repeats at a configurable interval, typically every 30 minutes, ensuring continuous compliance.
Declarative vs. Imperative: A Key Distinction
Puppet's declarative language means you specify the end state, not the steps to get there. For instance, to ensure the Apache package is installed, you write:
package { 'httpd':
ensure => installed,
}
You don't write commands to check if it's installed and run the installer if not—Puppet handles that logic. This makes manifests idempotent: applying them multiple times yields the same result without unintended side effects.
In contrast, imperative tools (like shell scripts) execute commands in order and may not be idempotent. Puppet's model reduces the risk of configuration drift and makes infrastructure reproducible.
Puppet's Language and Structure
Puppet code is organized into manifests, which are files containing resource declarations. Manifests can include conditional logic, variables, and functions, but the core is resource management. A simple manifest might look like:
file { '/etc/motd':
ensure => file,
content => "Welcome to ${::fqdn}\n",
owner => 'root',
group => 'root',
mode => '0644',
}
This ensures a file exists with specific content and permissions. Puppet checks the current state and only makes changes if needed.
Modules bundle related resources. For example, a module for Apache might include resources to install the package, manage the configuration file, and ensure the service is running. Modules can be shared via the Puppet Forge, a repository of community-contributed modules.
Common Use Cases and Benefits
Puppet is widely used for:
- Server provisioning: Automatically configuring new servers to a known state.
- Configuration enforcement: Ensuring servers remain compliant with security policies and organizational standards.
- Application deployment: Managing the configuration of applications and their dependencies.
- Continuous compliance: Regular runs detect and correct drift.
Benefits include reduced manual effort, consistency across environments, and the ability to version-control infrastructure code. However, Puppet has a learning curve, and its declarative model may not suit all workflows.
Common Problems and Misconceptions
New users often encounter these issues:
- Ordering: Puppet applies resources in a non-deterministic order unless you explicitly define dependencies with
before,require,notify, orsubscribe. Forgetting this can lead to race conditions. - Complexity: Large environments can become complex to manage without proper module design and Hiera data separation.
- Misconception: Puppet is a scripting language. It's declarative; writing imperative-style code in Puppet often leads to fragile manifests.
- Misconception: Puppet only works on Linux. It supports Windows and various Unix-like systems, though module support varies.
Puppet vs. Other Configuration Management Tools
Puppet competes with tools like Chef, Ansible, and SaltStack. Key differences:
- Chef: Also declarative but uses a Ruby-based DSL and a pull model similar to Puppet.
- Ansible: Agentless, using SSH, and typically imperative (though can be declarative). Easier to start with but may scale differently.
- SaltStack: Uses a master-minion architecture and is known for speed and real-time execution.
Puppet's strengths are its mature ecosystem, strong declarative model, and robust reporting. The choice depends on team skills, infrastructure size, and specific needs.
Frequently Asked Questions
Is Puppet free?
Puppet offers an open-source version (Puppet Core) that is free to use. There is also a commercial version (Puppet Enterprise) with additional features and support, which requires a license.
Do I need a Puppet master?
Puppet can run in a masterless mode (using puppet apply), where each node compiles and applies its own catalog. However, the master-agent architecture is common for centralized management and reporting.
What language is Puppet written in?
Puppet is written in Ruby and Clojure (for Puppet Server). The Puppet language itself is a custom DSL.
Can Puppet manage network devices?
Yes, Puppet supports network device management through modules and integrations, though support varies by vendor.
How often does Puppet run?
By default, the Puppet agent runs every 30 minutes, but this interval is configurable.
Is Puppet idempotent?
Yes, Puppet is designed to be idempotent: applying the same manifest multiple times will not change the system if it's already in the desired state.