Writing Puppet Manifests
Puppet Manifest Basics
Puppet manifests are written in the Puppet Domain-Specific Language (DSL), which is a declarative language used to describe the desired state of a system. Puppet manifests typically have the .pp
file extension.
Here's a basic example of a Puppet manifest that ensures the presence of a package and a file:
package { 'nginx':
ensure => installed,
}
file { '/var/www/html/index.html':
ensure => file,
content => 'Hello, LabEx!',
}
In this example, the package
resource ensures that the nginx
package is installed, and the file
resource ensures that the /var/www/html/index.html
file exists and contains the specified content.
Resource Types and Attributes
Puppet has a wide range of built-in resource types, such as package
, file
, service
, user
, and cron
. Each resource type has a set of attributes that define the desired state of the resource.
For example, the file
resource type has attributes like ensure
, content
, owner
, group
, and mode
, which allow you to specify the desired state of a file.
file { '/etc/motd':
ensure => file,
owner => 'root',
group => 'root',
mode => '0644',
content => 'Welcome to this LabEx server!',
}
Organizing Manifests
As your infrastructure grows, it's important to organize your Puppet manifests in a structured way. Puppet supports the concept of modules, which are self-contained collections of manifests, files, and other resources.
Here's an example of a simple module structure:
my_module/
├── manifests
│ └── init.pp
├── files
│ └── motd.txt
└── templates
└── nginx.conf.epp
In this example, the my_module
module contains a main manifest (init.pp
), a file resource (motd.txt
), and a template for an Nginx configuration file (nginx.conf.epp
).
By organizing your manifests in this way, you can easily manage and reuse your Puppet code across different environments and projects.