🐳

Chef

2 notes  •  Containers & Orchestration

Chef Command Reference

Essential Chef commands for managing cookbooks, recipes, and nodes.

Local Development Commands

# Apply a local recipe (recipe.rb in current directory)
chef-apply recipe.rb

# Run in local mode
chef-client --local-mode

# Check recipe syntax
chef exec ruby -c recipe.rb

# Generate a new cookbook
chef generate cookbook mycookbook

# Generate a new recipe inside a cookbook
chef generate recipe mycookbook myrecipe

# Run a specific recipe from a cookbook in local mode
chef-client -z -r "mycookbook::myrecipe"

Chef Client (Agent) Commands

# Run chef-client (pull from Chef Server)
chef-client

# Run with a specific runlist
chef-client -r "role[webserver]"

# Run with verbose output
chef-client -l debug

# Dry run (why-run mode)
chef-client --why-run

Knife Commands

# List nodes
knife node list

# Show node details
knife node show <node-name>

# Upload a cookbook
knife cookbook upload mycookbook

# List cookbooks
knife cookbook list

# Bootstrap a new node
knife bootstrap <ip> -U <user> -N <node-name> --sudo

# Run chef-client on a remote node
knife ssh 'name:<node>' 'sudo chef-client' -x ubuntu

Bootstrap a Chef Node with knife

Bootstrapping installs the Chef client on a remote node and registers it with the Chef Server. Run this command from the Chef workstation inside the chef-repo directory (which contains the .chef/knife.rb file).

Prerequisites

  • Chef workstation with knife configured (.chef/knife.rb)
  • SSH access to the target node
  • Target node reachable on the network

Bootstrap Command Syntax

knife bootstrap <HOST> -p <PORT> -U <SSH_USER> -N <NODE_NAME> --sudo

Example

cd ~/chef-repo
knife bootstrap 192.168.1.50 -p 22 -U ubuntu -N web-server-01 --sudo

Bootstrap with SSH Key

knife bootstrap 192.168.1.50 -U ubuntu -N web-server-01 --sudo \
  --ssh-identity-file ~/.ssh/id_rsa

Bootstrap with a Runlist

knife bootstrap 192.168.1.50 -U ubuntu -N web-server-01 --sudo \
  --run-list "role[base],recipe[nginx]"

Verify the Node Was Registered

knife node list
knife node show web-server-01

Notes

  • The .chef directory must contain knife.rb and the validator key.
  • Use -p to specify a non-standard SSH port.
  • The node name (-N) must be unique in Chef Server.