Puppet language syntax examples

A quick reference of syntax examples for the Puppet language.

Resource examples

Resource declaration

This example resource declaration includes:
  • file: The resource type.

  • ntp.conf: The resource title.

  • path: An attribute.

  • '/etc/ntp.conf': The value of an attribute; in this case, a string.

  • template('ntp/ntp.conf'): A function call that returns a value; in this case, the template function, with the name of a template in a module as its argument.

file { 'ntp.conf':
  path    => '/etc/ntp.conf',
  ensure  => file,
  content => template('ntp/ntp.conf'),
  owner   => 'root',
  mode    => '0644',
}

For details about resources and resource declaration syntax, see Resources.

Resource relationship metaparameters

Two resource declarations establishing relationships with the before and subscribe metaparameters, which accept resource references.

The first declaration ensures that the ntp package is installed before the ntp.conf file is created. The second declaration ensures that the ntpd service is notified of any changes to the ntp.conf file.
package { 'ntp':
  ensure => installed,
  before => File['ntp.conf'],
}
service { 'ntpd':
  ensure    => running,
  subscribe => File['ntp.conf'],
}

For details about relationships usage and syntax, see Relationships and ordering. For details about resource references, see Resource and class references.

Resource relationship chaining arrows

Chaining arrows forming relationships between three resources, using resource references.In this example, the ntp package must be installed before the ntp.conf file is created; after the file is created, the ntpd service is notified.

Package['ntp'] -> File['ntp.conf'] ~> Service['ntpd']

For details about relationships usage and syntax, see Relationships and ordering. For details about resource references, see Resource and class references.

Exported resource declaration

An exported resource declaration.
@@nagios_service { "check_zfs${hostname}":
  use                 => 'generic-service',
  host_name           => "$fqdn",
  check_command       => 'check_nrpe_1arg!check_zfs',
  service_description => "check_zfs${hostname}",
  target              => '/etc/nagios3/conf.d/nagios_service.cfg',
  notify              => Service[$nagios::params::nagios_service],
}

For information about declaring and collecting exported resources, see Exported resources.

Resource collector

A resource collector, sometimes called the "spaceship operator." Resource collectors select a group of resources by searching the attributes of each resource in the catalog.
User <| groups == 'admin' |>

For details about resource collector usage and syntax, see Resource collectors.

Exported resource collector

An exported resource collector, which works with exported resources, which are available for use by other nodes.
Concat::Fragment <<| tag == "bacula-storage-dir-${bacula_director}" |>>

For details about resource collector usage and syntax, see Resource collectors. For information about declaring and collecting exported resources, see Exported resources.

Resource default for the exec type

A resource default statement set default attribute values for a given resource type. This example specifies defaults for the exec resource type attributes path, environment, logoutput, and timeout.
Exec {
  path        => '/usr/bin:/bin:/usr/sbin:/sbin',
  environment => 'RUBYLIB=/opt/puppetlabs/puppet/lib/ruby/site_ruby/2.1.0/',
  logoutput   => true,
  timeout     => 180,
}

For details about default statement usage and syntax, see Resource defaults.

Virtual resource

A virtual resource, which is declared in the catalog but isn't applied to a system unless it is explicitly realized.
@user { 'deploy':
  uid     => 2004,
  comment => 'Deployment User',
  group   => www-data,
  groups  => ["enterprise"],
  tag     => [deploy, web],
}

For details about virtual resource usage and syntax, see Virtual resources.

Defined resource type examples

Defined resource type definition

Defining a type creates a new defined resource type. The name of this defined type has two namespace segments, comprising the name of the module containing the defined type, apache, and the name of the defined type itself, vhost.

define apache::vhost ($port, $docroot, $servername = $title, $vhost_name = '*') {
  include apache
  include apache::params
  $vhost_dir = $apache::params::vhost_dir
  file { "${vhost_dir}/${servername}.conf":
    content => template('apache/vhost-default.conf.erb'),
    owner   => 'www',
    group   => 'www',
    mode    => '644',
    require => Package['httpd'],
    notify  => Service['httpd'],
  }

For details about defined type usage and syntax, see Defined resource types.

Defined type resource declaration

Declarations of an instance, or resource, of a defined type are similar to other resource declarations. This example declares a instance of the apache::vhost defined type, with a title of "homepages" and the port and docroot attributes specified.

apache::vhost { 'homepages':
  port       => 8081,
  docroot => '/var/www-testhost',
} 

For details about defined type usage and syntax, see Defined resource types.

Defined type resource reference

A resource reference to an instance of the apache::vhost defined resource. Every namespace segment in a resource reference must be capitalized.

Apache::Vhost['homepages']

For details about defined type usage and syntax, see Defined resource types. For details about resource references, see Resource and class references.

Class examples

Class definition

A class definition, which makes a class available for later use.

class ntp {
  package {'ntp':
    ...
  }
  ...
}

For details about class usage and syntax, see Classes.

Class declaration

Declaring the ntp class in three different ways:
  • the include function
  • the require function

  • the resource-like syntax

Declaring a class causes the resources in it to be managed.
The include function is the standard way to declare classes:
include ntp
The require function declares the class and makes it a dependency of the code container where it is declared:
require ntp
The resource-like syntax declares the class and applies resource-like behavior. Resource-like class declarations require that you declare a given class only one time.
class {'ntp':}

For details about class usage and syntax, see Classes.

Variable examples

Variable assigned an array value

A variable being assigned a set of values as an array.

$package_list = ['ntp', 'apache2', 'vim-nox', 'wget']

For details about assigning values to variables, see Variables.

Variable assigned a hash value

A variable being assigned a set of values as a hash.

$myhash = { key => { subkey => 'b' } }

For details about assigning values to variables, see Variables.

Interpolated variable

A built-in variable provided by the primary server being interpolated into a double-quoted string.

...
content => "Managed by puppet server version ${serverversion}"

For details about built-in variables usage and syntax, see Facts and built-in variables. For information about strings and interpolation, see Strings.

Conditional statement examples

if statement, using expressions and facts

An if statement, whose conditions are expressions that use facts provided by the agent.
if $is_virtual {
  warning( 'Tried to include class ntp on virtual machine; this node might be misclassified.' )
}
elsif $operatingsystem == 'Darwin' {
  warning( 'This NTP module does not yet work on our Mac laptops.' )
else {
  include ntp
}

For details about if statements, see Conditional statements and expressions.

if statement, with in expression

An if statement using an in expression.
if 'www' in $hostname {
  ...
}

For details about if statements, see Conditional statements and expressions.

Case statement

A case statement.
case $operatingsystem {
  'RedHat', 'CentOS': { include role::redhat  }
  /^(Debian|Ubuntu)$/:{ include role::debian  }
  default:            { include role::generic }
}

For details about case statements, see Conditional statements and expressions.

Selector statement

A selector statement being used to set the value of the $rootgroup variable.
$rootgroup = $osfamily ? {
    'RedHat'          => 'wheel',
    /(Debian|Ubuntu)/ => 'wheel',
    default            => 'root',
}

For details about selector statements, see Conditional statements and expressions.

Node examples

Node definition

A node definition or node statement is a block of Puppet code that is included only in matching nodes’ catalogs. This allows you to assign specific configurations to specific nodes.

node 'www1.example.com' {
  include common
  include apache
  include squid
}

Node names in node definitions can also be given as regular expressions.

node /^www\d+$/ {
  include common
}

For details about node definition usage and syntax, see Node definitions.