Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 37 ยท Resources and Recipes

directory, user, group and execute

Learn more core Chef resources: directory with recursive creation, user and group management, execute for commands, plus link and cron, with runnable stand-ins.

Real servers need more than packages and files. You need directories with the right owner, users and groups, symbolic links, scheduled jobs and, when nothing else fits, a command. This lesson covers the next set of everyday resources: directory, user, group, link, cron (or its modern cousin systemd_unit) and execute. Real code is read-only; stand-ins let you run the logic.

directory

Ruby
directory '/var/www/app' do
  owner 'deploy'
  group 'deploy'
  mode '0755'
  recursive true      # create missing parent directories too
end

recursive true is the equivalent of mkdir -p. Without it, Chef fails if /var/www does not exist. Note that owner, group and mode apply only to the last directory in the path, not to the parents it creates; for the parents, declare them with their own directory resources. To delete a tree use action :delete together with recursive true.

user and group

Ruby
group 'deploy'

user 'deploy' do
  group 'deploy'
  shell '/bin/bash'
  home '/home/deploy'
  manage_home true    # create the home directory
  comment 'Deploy user'
end

group 'docker' do
  members ['deploy']
  append true         # add without removing existing members
  action :modify
end

Create the group before the user that refers to it. append true on a group matters: without it Chef would replace the member list. Never put plain-text passwords in a recipe. Use a hashed password property, or better, SSH keys and a secrets store (see data bags).

Ruby
link '/opt/app/current' do
  to '/opt/app/releases/20240601'
end

cron 'nightly backup' do
  minute '30'
  hour '2'
  command '/usr/local/bin/backup.sh'
  user 'root'
end

execute 'reload systemd' do
  command 'systemctl daemon-reload'
  action :nothing
end

execute runs a command. It is the escape hatch: use it only when no dedicated resource exists. Because Chef cannot know whether a command has already done its job, an execute should almost always be protected by a guard (creates, only_if, not_if), the topic of the next lesson, or by action :nothing plus a notification.

Stand-in: directory with recursive and ownership

The model below builds a directory tree and reports what each resource changed, including the "parents are not chowned" rule. It is not real Chef.

Ruby Stand-in: directory recursive and owners

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 parents /var and /var/www end up owned by root (the defaults) while only the last element belongs to deploy. That is exactly the surprise to remember.

Stand-in: users, groups and membership

Ruby Stand-in: user and group with append

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 last line shows why append true is important: without it, the member list was replaced by only bob.

When to use execute

A short decision rule keeps recipes clean:

  1. Is there a dedicated resource (package, service, user, systemd_unit, git, remote_file)? Use it.
  2. If not, can a custom resource express it? See Custom Resources.
  3. Otherwise use execute, and always add a guard or action :nothing.

The stand-in below shows a guard with creates: the command runs only if a marker file is missing.

Ruby Stand-in: execute with a creates guard

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 second pair shows the danger: an unguarded execute runs on every Chef run, so it reports a change every 30 minutes forever.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does recursive true do on directory?

    It works like mkdir -p. Ownership settings still apply only to the final directory.
  2. 2. Why use append true when adding a member to an existing group?

    Without append the members property replaces the list.
  3. 3. An execute resource has no guard. What happens?

    Chef cannot know the command's effect, so it runs each time unless you add a guard or :nothing.
  4. 4. Which should come first in a recipe?

    The group must exist before a user can be placed in it.

Summary

  • directory needs recursive true to create parents, and ownership applies to the last path element only.
  • user and group manage accounts; create groups first and use append true.
  • execute is an escape hatch and needs a guard or action :nothing.
  • Prefer dedicated resources, then custom resources, then execute.

Next: Guards and Idempotence