Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 38 ยท Getting Started

Declarative, Idempotent, and Puppet Compared

Understand declarative versus imperative configuration, idempotence and convergence, and see how Puppet compares with Ansible, Chef, Salt and plain shell scripts.

Two ideas explain most of how Puppet behaves: the language is declarative, and every resource is idempotent. Once these click, the rest of the course is mostly vocabulary. This lesson also compares Puppet with the other common tools, so you can judge where it fits.

Imperative versus declarative

An imperative program lists steps: do this, then that. A shell script that sets up a web server is imperative:

Terminal
apt-get install -y nginx
echo "hello" > /var/www/html/index.html
systemctl start nginx

A declarative description states the goal and leaves the steps to the tool:

PUPPET
package { 'nginx': ensure => installed }
file { '/var/www/html/index.html': content => "hello\n" }
service { 'nginx': ensure => running }

The practical difference appears when something is already partly done. The script above reinstalls or rewrites things on every run and fails in surprising ways if a step half-finished. The declarative version only acts on what differs.

Idempotence

An operation is idempotent if running it many times has the same effect as running it once. Puppet resources are idempotent: ensure => installed first installs, and later runs find the package present and do nothing. Puppet reports the difference in each run: changed (it had to act), unchanged (already right), or failed.

Idempotence is what makes it safe to run Puppet every 30 minutes on every machine. It also lets you repair a server: whatever went wrong, running the same code again converges it back.

Python Stand-in: an idempotent file resource

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first run changes the file, the next two do nothing, the drift is repaired once, and then the system is quiet again. This repeated "check then fix" is called convergence.

A non-idempotent contrast

Compare with a naive script that appends a line:

Python Stand-in: why appending is not idempotent

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real Puppet offers this safe version through the file_line type (from the stdlib module) and, more commonly, by managing the whole file from a template.

Idempotence is a property of the resource types, not magic. The exec resource runs any command you give it, and Puppet cannot know whether the command is safe to repeat. You make an exec idempotent with creates, onlyif or unless. See Resources and Resource Types.

Puppet compared with other tools

flowchart TB
    subgraph Pull["Agent pulls (Puppet, Chef, Salt)"]
      N1["Node agent"] -->|asks every 30 min| S1["Server"]
    end
    subgraph Push["Controller pushes (Ansible, scripts)"]
      C2["Control machine"] -->|SSH| N2["Node"]
    end

Where the work starts differs: Puppet agents pull their catalog; Ansible pushes over SSH.

Tool Language Default model Notes
Puppet Puppet DSL (declarative) Agent pulls from a server, or puppet apply Strong modelling, reports, large Forge
Ansible YAML playbooks Push over SSH, no agent Easy start, ordered tasks
Chef Ruby DSL Agent pulls (Chef Infra) Full Ruby power, steeper curve
Salt YAML + Jinja Master/minion or SSH Fast event bus
Shell scripts Bash Run by hand or cron Not idempotent unless you make it so

Key differences with Puppet:

  • Dependency graph. Puppet builds a graph of resources and orders them from the relationships you declare. Ansible runs tasks top to bottom. Puppet code order is mostly irrelevant, which surprises beginners (see Resource Relationships and Ordering).
  • Separate data. Puppet encourages keeping data in Hiera, away from code (Hiera).
  • Continuous enforcement. An agent keeps repairing drift without anyone pressing run.

Choose by team and need. Puppet shines for large, long-lived fleets where drift correction and compliance reporting matter; push tools suit quick tasks and orchestration. Many teams use both.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does idempotent mean?

    An idempotent operation gives the same end result no matter how many times it runs.
  2. 2. Which resource is NOT automatically idempotent?

    Puppet cannot know if an arbitrary command is safe to repeat, so exec needs creates, onlyif or unless.
  3. 3. How does a Puppet agent normally get its instructions?

    Puppet is pull-based: the agent asks the server for its catalog.
  4. 4. What does a Puppet run report for a resource already in the right state?

    No action is needed, so the resource is unchanged.

Summary

  • Declarative code states the goal; imperative code lists steps.
  • Puppet resources are idempotent, so repeated runs are safe and repair drift (convergence).
  • exec is the exception and needs guards.
  • Puppet pulls, builds a dependency graph, and encourages separating data (Hiera).
  • Other tools (Ansible, Chef, Salt) differ in language and push/pull model, not in goals.

Next: Your First Puppet Run with puppet apply