Resource Syntax
| Part | Meaning |
|---|---|
| type | Kind of thing: package, file, service, user... (lowercase) |
| title | A unique name for this resource in the catalog (string in quotes) |
| attributes | Properties describing desired state, in name => value form |
| comma | Separates attributes. A trailing comma after the last one is allowed and recommended |
Title and Namevar
Each resource type has one attribute that identifies the real thing, called the namevar. If you do not set it, Puppet uses the title. For file the namevar is path, for package and service it is name, for user it is name.
name => lets two modules refer to the same resource name without a clash, and lets you write a stable title like Package['webserver'] while the real package name changes per OS.
The ensure Attribute
| Type | Common ensure values |
|---|---|
| package | installed (or present), absent, latest, or a version like '1.2.3' |
| file | file, directory, link, absent |
| service | running, stopped |
| user, group | present, absent |
latest in production: ensure => latest upgrades the package on every run whenever a new version appears, which may surprise you. Prefer installed, or an exact version, and update on purpose.
Quoting and Values
| Kind | Example | Notes |
|---|---|---|
| Single-quoted string | 'hello ${name}' | No variable interpolation. Use for plain text |
| Double-quoted string | "hello ${name}" | Interpolates ${variable}. Supports \n |
| Bareword | installed, present, true | Lowercase words are allowed as values |
| Number | 8080, 0.5 | Real numbers |
| Boolean | true, false | No quotes |
| Undef | undef | Means no value |
| Array | ['a', 'b', 'c'] | Ordered list |
| Hash | { key => 'value' } | Key-value pairs |
mode => '0644' in quotes. An unquoted number may be read as a different value.
Multiple Resources in One Declaration
Resource Defaults
Defaults apply to all resources of that type in the same scope (and nested scopes). Use them sparingly inside classes, so they do not surprise other code.
Resource References
To point to another resource, write the type with a capital first letter and the title in square brackets.
Comments
Duplicate Declarations
A resource title (and namevar) may be declared only once per catalog for each type. If two places declare the same resource Puppet stops with:
Fix it by declaring the resource in one place only (usually inside one class) and having other code depend on that class. Later you will learn ensure_resource, virtual resources and include to avoid duplicates.
The Resource Abstraction Layer and Providers
The same resource type works on different operating systems because each type has providers, the code that knows how to do the job on one platform. For package the providers include apt, yum, dnf, zypper, gem, pip, chocolatey. Puppet chooses the right provider automatically, or you can set it with provider =>.
Metaparameters
Some attributes work on every resource type. They are called metaparameters.
| Metaparameter | Purpose |
|---|---|
require, before | Ordering |
notify, subscribe | Ordering plus refresh (restart) |
tag | Label resources for filtering |
noop | Per-resource dry run |
schedule, audit, loglevel | Advanced control |
Practice Tasks
- Write a manifest that installs three packages with one array declaration
- Create a directory, a file inside it and a symbolic link to the file
- Use a resource default so every file has owner
rootand mode0644, with one override - Cause a duplicate declaration error on purpose and read the message
- Use
puppet describe fileto find five attributes you have not used yet
Conclusion
Every Puppet resource has a type, a unique title and attributes. The namevar identifies the real object, providers adapt the type to each OS, and metaparameters such as require work on all resources.

