This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Chef Configuration Management Cheat Sheet

A quick reference guide for Chef: knife CLI, chef-solo, recipe DSL resources, package installation, remote files, and conditional guards.

· Published

Chef is an infrastructure automation and configuration management framework written in Ruby and Erlang.

Installation & Standalone Solo

Install Chef client on target nodes using the official Omnitruck script:

# Install Chef client
curl -L https://omnitruck.chef.io/install.sh | sudo bash

# Verify installation
chef-solo -v

Execute a standalone cookbook run:

chef-solo -c solo.rb -j node.json

Knife CLI Commands

# Search supermarket registry
knife supermarket search mysql

# Download cookbook tarball
knife supermarket download nginx

# Bootstrap a remote node via SSH
knife bootstrap 192.168.1.50 -x root -P 'password' -N web01

Core Recipe DSL Resources

Packages & Services

package 'nginx' do
  action :install
end

service 'nginx' do
  action [:enable, :start]
  supports status: true, restart: true, reload: true
end

Remote File Downloads

remote_file '/usr/local/src/node.tar.gz' do
  source 'https://nodejs.org/dist/v18.0.0/node-v18.0.0.tar.gz'
  checksum 'sha256checksumhere...'
  mode '0644'
  action :create_if_missing
end

Execute Shell Commands

execute 'compile_app' do
  command './configure && make && make install'
  cwd '/usr/local/src/app'
  environment 'PATH' => "/usr/local/bin:#{ENV['PATH']}"
  creates '/usr/local/bin/app' # Guard: run only if binary does not exist
end

Conditional Guards (only_if & not_if)

execute 'migrate_database' do
  command 'rails db:migrate'
  cwd '/var/www/app/current'
  only_if { File.exist?('/var/www/app/current/Rakefile') }
  not_if  'pg_isready -h localhost'
end

Related topics