Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 37 ยท Getting Started

What Is Chef?

Learn what Chef is, why teams use configuration management, what idempotent and declarative mean, and how a Ruby stand-in lets you practice the core ideas here.

Chef is a configuration management and automation tool. You describe how a server should look (which packages are installed, which files exist, which services run), and Chef makes the server match that description. Because the description is code, you can store it in Git, review it, test it and run it on one machine or ten thousand.

Before Chef and tools like it, administrators logged in to each server and typed commands by hand. Two servers that were "the same" slowly drifted apart, and nobody could say exactly how a server had been built. Chef replaces that manual work with infrastructure as code.

What runs on this site. Chef Infra Client, the Chef Infra Server and Test Kitchen cannot run in your browser or on our code runner, because they need a real operating system with packages, services and root rights. So the real Chef code in these lessons is shown read-only, and we only show its output when we are certain of it. To let you practice the ideas, many lessons include a small, clearly labelled stand-in written in plain Ruby that you can run and change. A stand-in is a teaching model, not Chef itself.

The problem Chef solves

Imagine you must set up a web server. By hand you would install nginx, write a config file, create a directory and start the service. Do that on five servers and you will make a mistake on one of them. Do it again in six months after a security patch and you will not remember the steps.

Configuration management gives you:

  • Repeatability. The same code produces the same result every time.
  • Documentation. The code is the description of the server.
  • Speed. A new server is ready in minutes.
  • Safety. Changes can be reviewed, tested and rolled back like any other code.
  • Consistency at scale. Every node follows the same rules.

Declarative and idempotent

Two words appear in every Chef discussion.

Declarative means you state the end result, not the steps. You write "package nginx should be installed", not "run apt-get install nginx". Chef decides what, if anything, has to be done.

Idempotent means you can run the same code many times and the result is the same. If nginx is already installed, Chef does nothing. If it is missing, Chef installs it. Running the code a second time changes nothing.

Here is a real Chef recipe. It is read-only here, because a package cannot be installed in a browser.

Ruby
package 'nginx' do
  action :install
end

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

Read it as two statements about the desired state: the nginx package is installed, and the nginx service is enabled and running.

A stand-in: desired state in plain Ruby

The next program is a stand-in (not Chef). It keeps a pretend server in a hash and a tiny package method that checks the current state before acting. Run it and look at the second pass.

Ruby Stand-in: idempotent converge (not real Chef)

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 first run changes the server, the second changes nothing. That second run is idempotence in action. Real Chef does the same check for every resource: it compares the current state with the desired state and only acts when they differ.

Counting what changed

A useful side effect of idempotence is that you can report what changed. Chef prints "Updated 3 resources" at the end of a run. If the number is zero, the node already matched its code. Let us extend the stand-in to count changes.

Ruby Stand-in: counting updated resources

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 the third run changes the file, because we asked for new content. Chef behaves exactly this way: change the code, run again, and only the difference is applied.

Where Chef fits

Chef is one of several tools in the same family. Ansible uses YAML and pushes changes over SSH, Puppet uses its own declarative language, and Terraform focuses on creating cloud infrastructure rather than configuring operating systems. Chef's recipes are written in Ruby, which gives you a full programming language when you need loops, conditions and helper code. That power is Chef's trademark, and you will use it throughout this course. If you want to build your Ruby skills first, see the Ruby tutorial.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. What does it mean that Chef is declarative?

    Declarative code states what the server should look like. Chef compares that with reality and performs only the necessary steps.
  2. 2. A recipe is run twice and the second run changes nothing. This is called:

    Idempotent code reaches the same result no matter how often it runs. The second run finds the state already correct.
  3. 3. Why do the lessons use Ruby stand-ins?

    Packages, services and root access do not exist in the browser runner. Stand-ins model the ideas and are always labelled.

Summary

  • Chef turns server setup into code that is repeatable, reviewable and testable.
  • Recipes are declarative: you describe the desired state.
  • Chef is idempotent: it only acts when the current state differs from the desired state.
  • The real tool cannot run here, so Chef code is read-only and Ruby stand-ins model the ideas.

Next: Chef Vocabulary and Workflow