Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Resources and Recipes

package, service and file Resources

Master the three most used Chef resources: package (install, version, remove), service (enable, start, restart) and file (content, owner, mode), including cookbook_file and template.

If you learn only three resources, learn package, file and service. Together they cover the most common server job: install software, give it a configuration file, and keep it running. This lesson shows each with its important properties and actions as real, read-only Chef code, then uses runnable stand-ins to explore the behavior (idempotence, modes, restart logic) in plain Ruby.

package

package installs, upgrades or removes software using the platform's package manager.

Ruby
package 'nginx'                       # install, any version

package 'nginx' do
  version '1.24.0-2ubuntu7'           # pin an exact version
  action :install
end

package 'old-tool' do
  action :remove
end

package %w(git curl vim)              # an array installs several in one go

Important actions: :install (default), :upgrade (move to the newest), :remove, :purge (remove and delete config on platforms that support it), :lock/:unlock (pin on some platforms). The name differs between families (apache2 on Debian, httpd on RHEL), so use a variable chosen from platform_family, as in the earlier Ruby lesson.

action :upgrade makes every run potentially change the node when a new version is published. For production, pin versions or use :install and upgrade deliberately.

file

file manages a single file's existence, content, owner and permissions.

Ruby
file '/etc/motd' do
  content "Managed by Chef\n"
  owner 'root'
  group 'root'
  mode '0644'
end

file '/tmp/old.log' do
  action :delete
end

file '/var/lib/app/.initialized' do
  action :create_if_missing
end

Remember the three key behaviors: :create (default) writes the file if it is missing or different, :create_if_missing never overwrites, and :delete removes it. Always write mode as a string such as '0644': an unquoted 0644 would be read by Ruby as an octal number and can confuse readers and tools.

Larger files do not belong in content. For those you use cookbook_file (copy a static file from the cookbook) or template (render a dynamic file, see the templates chapter).

service

service manages a running system service (systemd, upstart, Windows services).

Ruby
service 'nginx' do
  action [:enable, :start]
end

service 'nginx' do
  supports restart: true, reload: true, status: true
  action :nothing                      # only acts when notified
end

Actions: :enable, :disable, :start, :stop, :restart, :reload, :nothing. Chef checks the current state: :start does nothing when the service is already running. A :restart always restarts, so you rarely write it directly; instead a configuration file notifies the service to restart only if the file changed (notifications cover this).

Stand-in: the file resource

Here is a small Ruby model of file with :create, :create_if_missing and :delete and with change reporting. It is not real Chef.

Ruby Stand-in: the file resource

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Notice that changing only the mode counts as a change. Real Chef also reports a diff of the old and new content, which is very helpful when debugging.

Stand-in: package then service ordering

Because resources run in order, a configuration file must be written before the service starts, and the package must be installed before both. The stand-in below checks a dependency list and shows what happens when the order is wrong.

Ruby Stand-in: why order matters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

In real life the failure message would be something like "service nginx does not exist". Put dependencies first and the recipe reads like a build script.

A complete mini recipe

Combine the three to a typical pattern, read-only:

Ruby
package 'nginx'

file '/etc/nginx/conf.d/site.conf' do
  content "server { listen 80; }\n"
  notifies :reload, 'service[nginx]'
end

service 'nginx' do
  action [:enable, :start]
end

The notifies line reloads nginx only when the config file actually changed. Notice how little code expresses a lot of intent.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you install git, curl and vim with one package resource?

    package accepts an array of names and installs them together.
  2. 2. What does file ... action :create_if_missing do when the file exists?

    create_if_missing only creates a file that does not exist yet.
  3. 3. Why should mode be written as '0644' (a string)?

    The convention is a quoted string, so the permissions are explicit and unambiguous.
  4. 4. Which service setup avoids restarting nginx on every run?

    A notification restarts or reloads the service only when the configuration changed.

Summary

  • package installs, upgrades or removes software; pin versions for production.
  • file manages content, owner and mode; :create_if_missing never overwrites.
  • service enables and starts; remember its default action is :nothing.
  • Order resources: package, then configuration, then service.

Next: directory, user, group and execute