Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 37 ยท Projects and Practice

Chef Practice Set and Interview Questions

Practice what you learned about Chef templates, data bags, roles, environments, Policyfiles, testing and operations with exercises, runnable solutions and interview Q and A.

Reading about configuration management is one thing; being able to reason about it under pressure is another. This final lesson of the second half gives you exercises with solutions you can run (as plain Ruby models, since Chef itself cannot run here), a set of short interview-style questions with answers, and a list of habits that separate maintainable cookbooks from fragile ones.

Try each exercise before you open the solution or run the code.

Exercise 1: write a constraint

You have cookbook versions 3.0.9, 3.1.0, 3.1.4, 3.2.0, 4.0.0. Write constraints that select:

  1. exactly 3.1.4
  2. any 3.x version at least 3.1.0
  3. only patch updates of 3.1
  4. anything below 4.0.0
Ruby Exercise 1 solution: check your constraints

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.

Compare the four lines. ~> 3.1 takes everything from 3.1.0 up to, but not including, 4.0.0, while ~> 3.1.0 stops before 3.2.0.

Exercise 2: who wins?

For each case, name the winning value of node['app']['mode'].

  1. Cookbook attribute file: default['app']['mode'] = 'a'; role default: 'b'.
  2. Role default 'b'; environment default 'c'.
  3. Role override 'd'; environment override 'e'.
  4. Environment default 'c'; a normal attribute 'n' on the node.
  5. A recipe sets node.default['app']['mode'] = 'r'; a role override is 'd'.
Ruby Exercise 2 solution: precedence in code

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 answers are b, b, e, n and d. If any of these surprised you, reread the precedence lesson and look at the table.

Exercise 3: design questions (no code)

  1. A template produces blank lines in a YAML file. What do you change?
  2. You need a list of 200 users on all servers. Which Chef object holds it, and how does the recipe loop over it?
  3. A node in production is running an old nginx cookbook version although a new one is uploaded. Give three possible reasons.
  4. Your load balancer config reloads every run even though nothing changed. Name two likely causes.
  5. You want exactly the same code on staging and production. Which Chef feature gives you that guarantee?

Possible answers: (1) use -%> and trim mode, check indentation; (2) a data bag, with data_bag('users').each plus data_bag_item or search; (3) the environment pins an exact version, the node is in a different environment, the new cookbook is frozen with a lower version, or the run has not happened yet; (4) the template output is not stable (unsorted search results, a timestamp), or a resource always notifies; (5) Policyfiles with a lock file pushed to both policy groups.

Interview questions

What is idempotence and why does Chef care? Running a resource twice leaves the system in the same state as running it once. Chef compares the desired and the actual state and acts only on differences, so repeated runs are safe and cheap.

Difference between compile and converge? Compile evaluates recipe Ruby and builds the resource collection; converge executes the resources in order. Plain Ruby runs at compile time, so use guards or ruby_block for converge-time logic.

When do you use a role, an environment, a data bag, an attribute? Role: what kind of server it is. Environment: which stage, version pins and stage settings. Data bag: shared records such as users. Attribute: configurable defaults of a cookbook.

Explain attribute precedence in one minute. Types rank default < normal < override < automatic, with force variants at the extremes. Within default, attribute file < recipe < environment < role. Within override, attribute file < recipe < role < environment. Hashes are deep merged.

How do you keep secrets out of version control? Encrypted data bags or Chef Vault with the key distributed separately, or an external secrets manager through the secret helper. Mark resources sensitive.

What does ~> 2.3 mean? Version 2.3.0 or higher, below 3.0.0.

What are Policyfiles? One file that defines the run list, cookbook sources and attributes; chef install resolves versions into a lock file that is pushed to policy groups. Everything tested is exactly what runs.

How do you test a cookbook? Cookstyle for style and deprecations, ChefSpec for fast unit tests of the resource collection, Test Kitchen with InSpec to converge on real instances and verify the state, plus a second converge to check idempotence.

How does Chef scale to thousands of nodes? Interval and splay to spread runs, Policyfiles to avoid solving on the server, small node objects, selective search with filter_result, and a tiered or high-availability server layout with monitoring.

A run failed halfway. What do you check? The resource, action and exception in the error; the phase (compile, converge, API); -l debug; --why-run; reproduce in Kitchen; fix, add a test.

Exercise 4: find the bug

This cookbook does too much and has idempotence problems. Fix it mentally, then check the model:

Ruby
execute 'download app' do
  command 'curl -o /opt/app.tar.gz https://example.com/app.tar.gz'
end

execute 'extract app' do
  command 'tar xzf /opt/app.tar.gz -C /opt'
  notifies :restart, 'service[app]', :immediately
end

service 'app' do
  action :start
end

Problems: both execute resources run every time (no guard), so the service restarts on every run; downloading with curl ignores checksums; a remote_file resource with a checksum and ark-style or archive_file extraction would be idempotent. A fix:

Ruby
remote_file '/opt/app.tar.gz' do
  source 'https://example.com/app.tar.gz'
  checksum 'ab12...'
  notifies :extract, 'archive_file[/opt/app.tar.gz]', :immediately
end

archive_file '/opt/app.tar.gz' do
  destination '/opt/app'
  action :nothing
  notifies :restart, 'service[app]', :delayed
end

service 'app' do
  action [:enable, :start]
end
Ruby Model: guarded versus unguarded steps

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 unguarded design downloads, extracts and restarts the service on all five runs. The guarded design acts once, and again only when the upstream file really changes.

Habits of maintainable cookbooks

  • Small cookbooks with one job; wrapper cookbooks for composition.
  • Defaults in attributes, formatting in templates, decisions in recipes and helpers.
  • Prefer built-in resources to execute; if you must use execute, add creates, not_if or only_if.
  • Pin versions in production, and test every change on real instances.
  • Keep secrets out of Git and logs.
  • Write for the next person: short recipes, clear names and comments only where Chef cannot say it.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which constraint selects 3.1.0, 3.1.4 but not 3.2.0?

    ~> 3.1.0 allows patch updates only, so 3.2.0 is excluded.
  2. 2. Role default 'b' versus environment default 'c': which wins?

    Role defaults rank above environment defaults.
  3. 3. Why does an unguarded execute resource cause repeated restarts?

    Without a guard, execute reports a change on every run, and the notification fires each time.
  4. 4. Which feature best guarantees that staging and production run identical cookbook code?

    The lock file records exact versions and content hashes, and policy groups deliver that same revision.
  5. 5. What proves a cookbook is idempotent in Test Kitchen?

    If the second converge reports no updates, the recipe leaves a converged system unchanged.

Summary

  • You practiced writing version constraints, resolving attribute precedence, designing with roles, environments and data bags, and finding idempotence bugs.
  • Interview answers boil down to a few ideas: idempotence, compile versus converge, precedence, locking, layered testing and spread-out runs.
  • Guards, built-in resources and delayed notifications keep runs quiet and safe.

Next: Back to the Chef tutorial