Resources & recipes
The declarative Ruby building blocks.
A resource is Chef’s declarative building block: a statement of a piece of desired state, with a type, a name, and properties. package, service, template, file, directory, user, execute — each maps to something on the system and knows how to bring it to the declared state on the platform it runs on. A recipe is an ordered collection of resources, and Chef converges them in the order written.
package 'nginx' doaction :installendtemplate '/etc/nginx/nginx.conf' dosource 'nginx.conf.erb'notifies :reload, 'service[nginx]' # only if this file changesendservice 'nginx' doaction [:enable, :start]end
Actions, and platform abstraction
Every resource has actions (install/remove, start/stop/enable, create/delete) and a sensible default. The package resource abstracts the platform: the same declaration installs via apt on Debian and yum/dnf on RHEL, because Chef picks the right provider from the node’s detected platform. This is what lets one cookbook target a mixed fleet, and it is why you declare package ‘nginx’ rather than shelling out to a specific package manager.