Puppet Package, Service and File Resources: The Install, Configure, Run Pattern

Puppet Training Course 26 min min read Updated: Nov 17, 2026 Beginner
Puppet Package, Service and File Resources: The Install, Configure, Run Pattern
Beginner Topic 1 of 3

The Install, Configure, Run Pattern

Most Puppet code for a software product follows three steps: install the package, write the configuration, start the service. These are the package, file and service resource types.

puppet package { 'nginx': ensure => installed, } file { '/etc/nginx/nginx.conf': ensure => file, owner => 'root', group => 'root', mode => '0644', source => 'puppet:///modules/profile/nginx.conf', require => Package['nginx'], notify => Service['nginx'], } service { 'nginx': ensure => running, enable => true, }

The package Resource

AttributeValuesMeaning
ensureinstalled (or present)Install if missing, do not upgrade
latestAlways upgrade to the newest version (use carefully in production)
absent, purgedRemove the package (purged also removes config on Debian)
'1.24.0-1'Pin a specific version
providerapt, dnf, yum, gem, pip3, windows, chocolateyWhich tool installs it
install_optionsarrayExtra flags for the tool
namestringReal package name if different from the title
puppet package { 'git': ensure => installed } package { 'redis-server': ensure => '5:7.0.15-1', # pin one version } package { 'old-telnet': ensure => absent, } # Several at once package { ['curl', 'wget', 'unzip', 'vim']: ensure => installed, } # Language packages package { 'bundler': ensure => installed, provider => 'gem', } package { 'requests': ensure => installed, provider => 'pip3', }

Different package names per OS

puppet $web_pkg = $facts['os']['family'] ? { 'Debian' => 'apache2', 'RedHat' => 'httpd', default => fail('unsupported'), } package { $web_pkg: ensure => installed }
Idempotent upgrades: With ensure => installed, Puppet changes nothing once the package exists, which is the safest default. latest makes every run a possible upgrade, which can surprise you. Many teams pin versions or manage updates with a patch tool instead (see the Bolt lesson).

The service Resource

AttributeValuesMeaning
ensurerunning or stoppedCurrent state
enabletrue, false, maskStart at boot
hasrestart, hasstatustrueDoes the init script support these? (default is fine on systemd)
restart, start, stop, statuscommand stringCustom commands when the default does not work
providersystemd, windows...Normally detected automatically
puppet service { 'nginx': ensure => running, enable => true, } service { 'cups': ensure => stopped, enable => false, # not at boot either } service { 'myapp': ensure => running, enable => true, restart => '/bin/systemctl reload myapp', # reload rather than restart on refresh }

When a resource uses notify => Service['nginx'] and changes, the service is restarted (or runs your restart command). If the service is stopped, Puppet starts it.

The file Resource

AttributeMeaning
ensurefile, directory, link, absent, or present
pathThe full path (usually the title)
contentThe exact text (string or from epp())
sourceCopy from Puppet (puppet:///modules/mod/file) or another location. Do not use together with content
targetDestination of a symbolic link
owner, groupName or ID
modeString like '0644' or symbolic 'u=rw,go=r'. Always quote it
recursetrue to manage directory contents
purgeRemove files that Puppet does not manage (needs recurse)
forceAllow removing a directory
backupWhere changed files are saved ('.puppet-bak' or a filebucket)
replacefalse means create the file but never overwrite it
checksumHow to detect changes (sha256, mtime, ...)
show_diffInclude the diff in the log (hide for secrets)

Files, directories and links

puppet file { '/opt/myapp': ensure => directory, owner => 'myapp', group => 'myapp', mode => '0755', } file { '/opt/myapp/config.yml': ensure => file, owner => 'myapp', mode => '0640', content => "env: production\n", require => File['/opt/myapp'], # not strictly needed: Puppet auto-requires parent directories } file { '/usr/local/bin/app': ensure => link, target => '/opt/myapp/bin/app', } file { '/tmp/old-report.txt': ensure => absent, }
Autorequire: Puppet automatically makes a file depend on its parent directory and on the user that owns it, if those are in the same catalog. You usually do not need to add require for these.

Manage a whole directory

puppet file { '/etc/nginx/conf.d': ensure => directory, recurse => true, purge => true, # remove any file not managed by Puppet source => 'puppet:///modules/profile/nginx/conf.d', notify => Service['nginx'], }
purge deletes: With purge => true, every unmanaged file in that directory is removed. Test it in a lab first. Never use it on a directory such as /etc or /home.

Create only if missing

puppet file { '/etc/app/local.conf': ensure => file, replace => false, # create default, never overwrite user changes content => "# local overrides\n", }

Where source files live

puppet:///modules/profile/nginx.conf means: module profile, file files/nginx.conf. The path after the module name is relative to the files directory. Files in files/ are served by the Puppet server to the agent.

text profile/ files/ nginx.conf -> puppet:///modules/profile/nginx.conf nginx/conf.d/site.conf -> puppet:///modules/profile/nginx/conf.d/site.conf templates/ vhost.conf.epp

A Complete Example: Nginx Web Server

puppet class profile::nginx ( Integer[1,65535] $port = 80, String $document_root = '/var/www/site', ) { package { 'nginx': ensure => installed, } file { $document_root: ensure => directory, owner => 'www-data', mode => '0755', } file { '/var/www/site/index.html': ensure => file, content => "<h1>Hello from ${facts['networking']['hostname']}</h1>\n", require => File[$document_root], } file { '/etc/nginx/conf.d/site.conf': ensure => file, content => epp('profile/site.conf.epp', { 'port' => $port, 'root' => $document_root }), require => Package['nginx'], notify => Service['nginx'], } service { 'nginx': ensure => running, enable => true, require => Package['nginx'], } }

Inspecting Resources on a Machine

bash puppet resource package nginx # show state in Puppet language puppet resource service nginx puppet resource file /etc/hosts puppet resource user root puppet resource package # list all packages puppet resource service nginx ensure=stopped # change a resource right now (ad hoc)

Common Problems

SymptomLikely causeFix
Package not foundWrong name for the OS or repository not set upCheck with apt search or dnf search, add the repo first
Service starts then stopsConfig errorjournalctl -u name, validate config
File always changesContent differs each run (timestamps) or mode quoted wronglyRemove changing data, quote mode as a string
Could not find declared classSource path or module name wrongCheck files/ directory layout
Service not restartedMissing notifyAdd notify => Service[...]

Practice Tasks

  1. Install htop, tree and jq with one package resource and confirm with puppet resource package
  2. Manage /etc/motd with content, then change the file by hand and run Puppet to see it restored
  3. Create a directory /opt/demo with two files and a symlink in /usr/local/bin
  4. Write the nginx example above and prove that changing the template restarts nginx
  5. Use replace => false for a file and verify that your manual edits stay

Conclusion

Use package to install, file to configure and service to run, and connect them with require and notify. Quote file modes, avoid purge where unsure, and use puppet resource to inspect the current state.

Quick Summary: package ensure installed/latest/absent; service ensure+enable; file content/source/mode/recurse/purge; notify restarts services.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators