Technical skill

Chef

Keep a computer close to a written plan

Imagine setting up the same folders, programs, and services on several computers. Chef Infra helps bring each computer to a written plan: it checks what is there and changes items that do not match. A recipe is a set of instructions, and a resource describes one item to manage, such as a file or service. This example asks Chef to manage one small text file:

file '/tmp/chef-demo.txt' do
  content 'Managed by Chef'
  mode '0644'
  action :create
end

The file resource creates the path if it is missing and updates its content or mode if they differ from the recipe. Once the requested state is present, another run leaves the file alone. This repeatable behavior is called idempotence. A shell command may run every time unless a guard checks whether the work is needed. For example, a guard can check for a marker in a generated file before letting a command run again.

How Chef applies a recipe

Chef recipes use Ruby-based syntax, but each resource describes an end state. Chef Infra Client reads a run-list, the list of recipes to apply, then compiles them into a resource collection and converges the computer toward the declarations. A cookbook groups recipes with related templates, files, attributes, and metadata.

The example writes to /tmp/chef-demo.txt, so running it changes the local computer. Review resource paths and actions before applying a cookbook outside a disposable test environment. A resource's provider carries out its declared state. A package resource can keep a package installed, for instance, while a service resource can keep a service running. Use execute when no resource models the operation; add a guard if the command should run only when a condition is met.

Preview before changing the computer

With the recipe saved at cookbooks/demo/recipes/default.rb, run the demo recipe in local mode and ask Chef for a why-run preview:

chef-client --local-mode --override-runlist demo --why-run

Local mode uses chef-zero and a local cookbook repository. Chef searches for cookbooks using its configuration and working directory. Why-run reports changes the client expects to make without applying normal resource actions. Some results are inferred rather than executed, so a preview can miss behavior that appears during a real converge. Test the cookbook in an environment like the target before removing --why-run and applying it.

A client-server run gets configuration and cookbook data from Chef Infra Server instead. Resources run in run-list and recipe order. Notifications can trigger a follow-up resource when another changes. Guards such as not_if and only_if skip work when a condition is already true. Use them when one action depends on another, rather than making the same command run on every client run.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset PostgreSQL DBA preview
image descriptionPostgreSQL DBA
by Artem Naumenko (fb)
1630 users
Skillset DevOps engineer preview
image descriptionDevOps engineer
by Infinite Education
1211 users
Skillset DevOps preview
image descriptionDevOps
by Federico Cordone
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful