Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 37 ยท Resources and Recipes

Chef Resources and Actions

Understand Chef resources deeply: types, name properties, properties, actions, default actions, providers, why resources are idempotent, and how to read the docs.

Resources are the heart of Chef. Everything a recipe does is expressed as resources, and the quality of your recipes depends on how well you understand them. This lesson goes beyond the syntax you met earlier: it explains what a resource really is (a declaration plus a provider), what properties and actions mean, and how to read a resource's documentation. A stand-in in Ruby lets you build a small resource system, since real Chef resources need a real operating system.

A resource is a declaration

When you write

Ruby
package 'nginx' do
  version '1.24.0'
  action :install
end

you are not running anything. You are declaring: "there is a package called nginx, I want version 1.24.0, and the action is :install". Chef later hands that declaration to a provider, the code that knows how to do the work on this platform. On Ubuntu the package resource uses apt, on Rocky it uses dnf, on Windows it uses another provider. You write one resource, Chef chooses the right provider. This is why recipes can be portable.

flowchart LR
  R["Resource declaration<br/>package 'nginx'"] --> P{"Provider<br/>chosen by platform"}
  P --> A["apt (Debian)"]
  P --> D["dnf (RHEL)"]
  P --> W["chocolatey (Windows)"]

One declaration, several providers.

Properties

Properties describe the desired state. A property has a name, a type and often a default. For the file resource some properties are content, owner, group and mode. Properties not given use their default, and some properties are required.

The name property is the one that the string after the resource type fills. For package it is package_name, for file it is path, for service it is service_name. That is why these two are the same:

Ruby
file '/etc/motd' do
  content 'Hello'
end

file 'motd file' do
  path '/etc/motd'
  content 'Hello'
end

The second form names the resource differently, which is useful when you manage the same path in two places or want a readable name for notifications.

Actions

An action says what to do. Resources can have several actions, and many accept an array. Typical examples:

Resource Common actions Default
package :install, :upgrade, :remove, :purge :install
file :create, :create_if_missing, :delete, :touch :create
service :enable, :disable, :start, :stop, :restart, :reload :nothing
directory :create, :delete :create

Note that service has the default action :nothing. A service resource with no action does not start or enable anything, a classic gotcha: always write action [:enable, :start].

The special action :nothing is useful for resources that should only run when notified by another resource, which Notifications and Subscribes explains.

Idempotence is built in

Each provider follows the same pattern for every action: load the current state, compare it with the desired state, and act only if they differ. If the file already has the content, :create does nothing and Chef reports the resource as "up to date". This is why you can run recipes again and again safely.

Stand-in: build a resource and provider

The stand-in implements the declaration/provider split. Declarations are plain data; providers are separate lambdas that compare and act. It is not Chef's code, only a model.

Ruby Stand-in: declarations and providers

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.

The second run reports "up to date" for everything. Changing the action to :remove in the third run uses the same provider code with another branch.

Several actions in one resource

An array of actions runs them in order. The next stand-in models service with a default of :nothing, and shows why [:enable, :start] matters.

Ruby Stand-in: service with a default of :nothing

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.

Reading the documentation

Every resource has a documentation page listing its syntax, actions, properties and examples. Look at three things: which property is the name property, what the default action is, and which properties are required. If you are unsure whether a property is available in your Chef version, check the "since version" notes there. In this course we cover the most useful ones in the next lessons: package, service and file, then directory, user, group and execute.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a Chef provider do?

    The resource is the declaration; the provider knows how to compare state and act on apt, dnf, Windows and so on.
  2. 2. What is the default action of the service resource?

    service defaults to :nothing, so you must give :enable and :start explicitly.
  3. 3. What is the name property for the file resource?

    The string after file fills the path property unless you set path separately.
  4. 4. A resource whose current state already matches the desired state is reported as:

    Providers do nothing when no change is needed, and Chef reports it as up to date.

Summary

  • A resource is a declaration; the provider performs the work for the platform.
  • Properties describe desired state; the name property comes from the string after the type.
  • Actions say what to do; know each resource's default (service defaults to :nothing).
  • Providers compare current and desired state, which makes runs idempotent.

Next: package, service and file