Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 37 ยท Getting Started

Chef Workstation and Tools

Tour the Chef toolbox: Chef Workstation, chef-cli, knife, Cookstyle, Test Kitchen and local mode, with real commands shown read-only and a runnable command-line stand-in.

You write Chef code on your own computer, the workstation, and then send it to nodes. Chef bundles the tools you need into one installer called Chef Workstation. This lesson tours those tools, shows the real commands (read-only, because they need a full installation) and gives you a small Ruby stand-in for argument handling so you can practice reading command-line style input.

What Chef Workstation contains

Installing Chef Workstation gives you, in one package:

  • chef (the chef command, also called chef-cli): generates cookbooks, recipes and other files.
  • Chef Infra Client, so you can run recipes locally.
  • knife: talks to a Chef Infra Server (upload cookbooks, list nodes, bootstrap new machines).
  • Test Kitchen (kitchen): builds a temporary VM or container, runs your cookbook and checks the result. It is covered in the testing chapter of this course.
  • Cookstyle: a linter that finds style problems and deprecated patterns in cookbooks.
  • InSpec: a compliance and testing language used to verify the result.
  • Berkshelf: a dependency manager for cookbooks.

You do not have to memorize this list. What matters is the job each tool does.

Generating a cookbook

The chef generate family creates files with the right layout. These commands are read-only examples:

Terminal
chef generate cookbook webserver
cd webserver
chef generate recipe ssl
chef generate template nginx.conf
chef generate attribute default

The first command creates a folder webserver with metadata.rb, recipes/default.rb, a kitchen.yml, a test folder and a README. Every Chef project starts this way, and Cookbook Structure walks through each file.

Running a recipe without a server

You do not need a server to try Chef. Local mode runs Chef Infra Client against local files:

Terminal
sudo chef-client --local-mode --runlist 'recipe[webserver]'

--local-mode (short form -z) starts a tiny in-memory Chef server on your machine, so cookbooks, roles and data bags are read from disk. It is excellent for learning and for quick tests. For repeatable and isolated testing, Test Kitchen is better, because it works in a throwaway machine and never touches your own system.

Terminal
kitchen create    # build the test machine
kitchen converge  # run the cookbook
kitchen verify    # run the tests
kitchen destroy   # throw the machine away

knife: your remote control

knife is the command-line tool for the server and its nodes. A few real commands, shown read-only:

Terminal
knife cookbook upload webserver
knife node list
knife node show web1.example.com
knife bootstrap 203.0.113.10 --ssh-user ubuntu --node-name web1 --run-list 'recipe[webserver]'
knife search node 'platform:ubuntu'

Notice the pattern knife <object> <verb>: knife node list, knife role show, knife cookbook upload. Once you know the objects (cookbook, node, role, environment, data bag, client) and the verbs (list, show, create, delete, upload), you can guess most commands.

Linting with Cookstyle

Cookstyle reads your Ruby files and reports style and correctness problems. It can also automatically fix many of them:

Terminal
cookstyle .
cookstyle -a .

Run it before every commit. It catches the use of resources or properties that have been removed from Chef, which saves surprises after an upgrade.

Stand-in: a tiny knife-style parser

Real tools have the shape tool object verb arguments. The stand-in below parses lines like that and dispatches to Ruby methods. It is not knife, but writing it teaches the pattern and shows how option flags are read.

Ruby Stand-in: parse knife-style commands

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 key move is object, verb, *args = line.split, which uses Ruby's splat to capture the rest of the line. The same destructuring idea helps you in any Ruby script.

Where the tools meet

Ruby Stand-in: which tool does what?

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.

Check which versions you installed with chef --version, and keep Chef Workstation reasonably current. Old Cookstyle versions miss the newest deprecations.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which tool talks to the Chef Infra Server to upload cookbooks?

    knife is the server and node management tool, for example knife cookbook upload.
  2. 2. What does chef-client --local-mode do?

    Local mode (-z) needs no real server: cookbooks, roles and data bags are read from the local disk.
  3. 3. Why is Test Kitchen safer than local mode for testing a cookbook that installs packages?

    Kitchen creates and destroys a test VM or container, so experiments never change your workstation.
  4. 4. What does Cookstyle do?

    Cookstyle is a RuboCop-based linter specialised for Chef cookbooks.

Summary

  • Chef Workstation bundles chef-cli, Chef Infra Client, knife, Test Kitchen, Cookstyle, InSpec and Berkshelf.
  • chef generate creates cookbooks; knife manages the server; kitchen tests; cookstyle lints.
  • Local mode runs recipes without a server; Test Kitchen runs them in a disposable machine.
  • The command pattern tool object verb makes knife easy to guess.

Next: Ruby for Chef