The trifecta: package, file, and service

Package, file, service: Learn it, live it, love it. Even if this is the only Puppet you know, you can get a lot done.

Copy
package { 'openssh-server':
  ensure => installed,
}

file { '/etc/ssh/sshd_config':
  source  => 'puppet:///modules/sshd/sshd_config',
  owner   => 'root',
  group   => 'root',
  mode    => '0640',
  notify  => Service['sshd'], # sshd restarts whenever you edit this file.
  require => Package['openssh-server'],
}

service { 'sshd':
  ensure     => running,
  enable     => true,
}

package

Manages software packages.

Attribute Description Notes
name The name of the package, as known to your packaging system. Defaults to title.
ensure Whether the package should be installed, and what version to use. Allowed values:
  • present

  • latest (implies present)

  • Any version string (implies present)

  • absent

  • purged

    purged ensures absent, and deletes configuration files and dependencies, including those that other packages depend on. Provider-dependent.

source Where to obtain the package, if your system’s packaging tools don’t use a repository.  
provider Which packaging system to use (such as Yum or Rubygems), if a system has more than one available.  

file

Manages files, directories, and symlinks.

Attribute Description Notes
ensure Whether the file should exist, and what it should be. Allowed values:
  • file

  • directory

  • link (symlink)

  • present (anything)

  • absent

path The full path to the file on disk. Defaults to title.
owner By name or UID.  
group By name or GID.  
mode Must be specified exactly. Does the right thing for directories.  

For normal files:

source Where to download contents for the file. Usually a puppet:/// URL.
content The file’s desired contents, as a string. Most useful when paired with templates, but you can also use the output of the file function.

For directories:

source Where to download contents for the directory, when recurse => true.
recurse Whether to recursively manage files in the directory.
purge Whether unmanaged files in the directory should be deleted, when recurse => true.

For symlinks:

target The symlink target. (Required when ensure => link.)

Other notable attributes:

  • backup

  • checksum

  • force

  • ignore

  • links

  • recurselimit

  • replace

service

Manages services running on the node. As with packages, some platforms have better tools than others, so read the relevant documentation before you begin.

You can make services restart whenever a file changes with the subscribe or notify metaparameters. For more info, see Relationships and ordering.

Attribute Description Notes
name The name of the service to run. Defaults to title.
ensure The desired status of the service. Allowed values:
  • running (or true)

  • stopped (or false)

enable Whether the service should start on boot. Doesn’t work on all systems.  
hasrestart Whether to use the init script’s restart command instead of stop+start. Defaults to false.
hasstatus Whether to use the init script’s status command. Defaults to true.

Other notable attributes:

If a service has a bad init script, you can work around it and manage almost anything using the status, start, stop, restart, pattern, and binary attributes.