Chef Pocketbook

Chef Pocket Book — Uplatz

50 deep-dive flashcards • Wide layout • Fewer scrolls • 20+ Interview Q&A • Readable DSL examples

Section 1 — Infrastructure as Code with Chef

1) What is Chef?

Chef is a powerful automation tool for configuration management that transforms infrastructure into code. Using Ruby-based DSL, Chef enables consistent deployment, scaling, and compliance across servers and environments. It’s ideal for managing complex fleets, cloud-native or hybrid setups, and promoting DevOps culture with repeatable infrastructure patterns.

# Example Chef recipe
package 'nginx'
service 'nginx' do
  action [:enable, :start]
end

2) Key Concepts: Node, Recipe, Cookbook

Node: Any target machine Chef manages. Recipe: A collection of resources that describes system states. Cookbook: A structured unit that contains recipes, templates, files, attributes, and metadata.

cookbooks/
  webserver/
    recipes/default.rb
    attributes/default.rb
    templates/default/index.html.erb

3) Chef Infra vs Chef Habitat

Chef Infra automates infrastructure provisioning and config management. Chef Habitat focuses on packaging applications with their dependencies for portability across environments. Habitat helps with DevOps pipeline integration and continuous delivery.

4) Chef Server, Workstation, and Client

Chef Workstation is where cookbooks are authored and tested. Chef Server is the central hub that stores policies and distributes them. Chef Client runs on each node and pulls configuration from the server, applying the state defined in the recipes.

knife bootstrap  --ssh-user ubuntu --sudo --identity-file ~/.ssh/id_rsa --node-name web-node

5) Resource Types in Chef

Chef resources represent pieces of infrastructure like package, file, service, user, cron, etc. They have properties and actions. Use not_if and only_if for conditional execution.

file '/etc/motd' do
  content 'Welcome to Uplatz Node!'
  mode '0644'
  owner 'root'
  group 'root'
end

6) Attributes & Node Data

Attributes let you control recipe behavior based on environment, role, or platform. You can set default, override, and automatic attributes. Use node['attribute_name'] to access.

default['webserver']['port'] = 8080

7) Environments, Roles, and Data Bags

Environments help segregate configurations (e.g., dev, staging, prod). Roles group common configurations. Data Bags store global config or sensitive data (encrypted if needed).

knife environment create staging
knife role create db-server
knife data bag create secrets db_password

8) Chef Supermarket & Community Cookbooks

Chef Supermarket is a central repository of reusable community cookbooks. Use knife supermarket install or berkshelf to fetch them. Always review versions and dependencies.

knife supermarket install nginx

9) Testing: Test Kitchen & InSpec

Test Kitchen allows you to run and test cookbooks in isolated VMs or containers. InSpec validates infrastructure compliance through human-readable tests.

kitchen test
# InSpec example
describe port(80) do
  it { should be_listening }
end

10) Q&A — “Why use Chef in modern DevOps?”

Answer: Chef brings repeatability, traceability, and version control to infrastructure. It reduces human error, speeds up provisioning, and supports multi-cloud and hybrid environments. Combined with CI/CD and policy-as-code tools, Chef strengthens compliance and operational agility in complex systems.