Catalog / Chef Cheatsheet
Chef Cheatsheet
A comprehensive cheat sheet for Chef, covering essential concepts, commands, resources, and best practices for infrastructure automation and configuration management.
Core Concepts & Architecture
Key Components
Chef Server |
Central repository for cookbooks, roles, environments, and node metadata. The heart of the Chef infrastructure. |
Chef Workstation |
Local machine used to develop, test, and upload cookbooks to the Chef Server. Your development environment. |
Chef Client |
Agent installed on each node (server) that pulls configuration information from the Chef Server and applies it to the node. |
Chef Infra Client |
Command-line interface (CLI) to run the Chef Infra Client (formerly just Chef Client) on a node. |
Chef Automate |
Provides visibility and control over your Chef infrastructure. Includes features like compliance scanning and reporting. |
Ohai |
A tool within the Chef client that gathers system configuration data (e.g., OS, network, CPU) and makes it available as node attributes. |
Chef Workflow
|
Chef Objects
Cookbooks |
The fundamental unit of configuration in Chef. Contain recipes, attributes, and other resources that define how to configure a system. |
Recipes |
Contain instructions (resources) that specify how to configure a specific aspect of a node. Written in Ruby. |
Resources |
Represent a desired state for a system component (e.g., a file, a package, a service). Chef provides a wide range of built-in resources. |
Attributes |
Variables that define aspects of a node’s configuration. Used to customize cookbooks for different nodes or environments. |
Roles |
A way to group and apply cookbooks, recipes, and attributes to nodes based on their function (e.g., web server, database server). |
Environments |
Define different stages of your infrastructure (e.g., development, staging, production). Allow you to apply different configurations to nodes in different environments. |
Knife Command Reference
Node Management
|
Lists all nodes registered with the Chef Server. |
|
Displays the attributes and run list for a specific node. |
|
Opens the node data in your default editor for modification. |
|
Deletes a node from the Chef Server. |
|
Adds a role to a node’s run list. |
|
Removes a recipe from a node’s run list. |
Cookbook Management
|
Generates a basic cookbook structure. |
|
Uploads a cookbook to the Chef Server. |
|
Downloads a cookbook from the Chef Server. |
|
Lists all cookbooks available on the Chef Server. |
|
Shows details for a specific cookbook version. |
|
Deletes a cookbook from the Chef Server. |
Role and Environment Management
|
Creates a new role. |
|
Creates a role from a JSON file. |
|
Displays details for a role. |
|
Creates a new environment. |
|
Creates an environment from a JSON file. |
|
Displays details for an environment. |
Common Chef Resources
File Management
Creates or modifies a file with specified content, ownership, and permissions. |
Copies a file from the cookbook’s |
Generates a file from a template (ERB) file in the cookbook’s |
Creates a directory with specified ownership and permissions. |
Package and Service Management
Installs, upgrades, or removes a package on the node. |
Manages a system service, including starting, stopping, enabling, and disabling. |
Execution and User Management
Executes a command on the node. |
Creates, modifies, or deletes a user account on the node. |
Creates, modifies, or deletes a group on the node. |
Chef Best Practices
Cookbook Structure
A well-structured cookbook is essential for maintainability and reusability. Common directories include:
|
Idempotency
Ensure that your recipes are idempotent, meaning that running them multiple times has the same effect as running them once. Use |
This ensures the file is only created if it doesn’t already exist. |
Testing
Use testing tools like ChefSpec and Test Kitchen to thoroughly test your cookbooks before deploying them to production. ChefSpec provides unit testing capabilities, while Test Kitchen allows you to test your cookbooks in a virtualized environment. |
ChefSpec Example:
|
Attribute Management
Use attributes to externalize configuration values and make your cookbooks more flexible. Define default attributes in |
Attribute precedence:
|