This recipe will introduce the language and show you the basics of writing Puppet code. Puppet code files are called manifests; manifests declare resources. A resource in Puppet may be a type, class, or node. A type is something like a file or package or anything that has a type declared in the language. The current list of standard types is available on the puppetlabs website at https://puppet.com/docs/puppet/latest/type.html. I find myself referencing this site very often. You may define your own types, either using a mechanism, similar to a subroutine, named defined types, extending the language using a custom type. Types are the heart of the language; they describe the things that make up a node (node is the word Puppet uses for client computers/devices). Puppet uses resources to describe the state of a node; for example, we will declare the following package resource for a node using a site manifest: site.pp
.
Create a site.pp
file and place the following code in it:
node default { package { 'httpd': ensure => 'installed' } }
This manifest will ensure that any node on which this manifest is applied will install a package called httpd
. The default keyword is a wildcard to Puppet; it applies anything within the node default
definition to any node. When Puppet applies the manifest to a node, it uses a Resource Abstraction Layer (RAL) to translate the package type into the package management system of the target node. What this means is that we can use the same manifest to install the httpd
package on any system where Puppet has a Provider for the package type. Providers are the pieces of code that do the real work of applying a manifest. When the previous code is applied to a node running on a YUM-based distribution, the YUM provider will be used to install the httpd
RPM packages. When the same code is applied to a node running on an APT-based distribution, the APT provider will be used to install the httpd
DEB package (which may not exist, as most Debian-based systems call this package apache2
; we'll deal with this sort of naming problem later).