Recipes, Resources & Cookbooks
Resources — Declarative Building Blocks
A resource describes desired state, not the steps to get there. Chef's providers figure out the actual commands for the specific OS/package manager.
package 'nginx' do
action :install
end
template '/etc/nginx/nginx.conf' do
source 'nginx.conf.erb'
variables(port: node['nginx']['port'])
notifies :restart, 'service[nginx]' # only restarts if the file actually changed
end
service 'nginx' do
action [:enable, :start]
endIdempotency
Running the same recipe repeatedly produces the same result with no errors — package 'nginx' do action :install end does nothing if nginx is already installed. This lets Chef Client run on a schedule without breaking an already-converged system.
Cookbook Structure
my_cookbook/
recipes/default.rb
attributes/default.rb
templates/nginx.conf.erb
files/
metadata.rb
# Chef Supermarket — public repo of shareable community cookbooks,
# often preferred over writing common software configs from scratchAttributes & Precedence
# attributes/default.rb
default['nginx']['port'] = 80
# Precedence (lowest to highest):
# default < force_default < normal < override < force_override < automatic
# 'automatic' attributes come from Ohai — collected system facts
# (node['platform'], node['ipaddress']) recipes can referenceKeep your own version of these notes — editable, searchable, and organised by your stack.
Start free