
Chef is a powerful automation tool used to streamline infrastructure configuration and management. Whether you’re managing a fleet of servers or setting up consistent environments across your development and production stacks, Chef offers a declarative, scalable solution.
In this guide, we’ll help you understand how Chef’s configuration environments work and how to manage them effectively on Ubuntu 24.04 or later.
🧠 What Is a Chef Environment?
In Chef, an environment is a way to map nodes (servers) to specific cookbook versions. This is useful when you want to maintain different stages like:
- Development
- Staging
- Production
Environments let you:
- Control cookbook versions per environment
- Maintain stability in production
- Enable testing and validation in dev/stage before deployment
✅ Prerequisites
Before proceeding, make sure you have:
- A fresh Ubuntu 24.04+ system
- Chef Workstation installed
- Knife configured to communicate with your Chef Server
- Basic understanding of cookbooks and nodes
Step 1: Create a New Chef Environment
You can create an environment in two ways: manually via a JSON file or using the Knife command.
Option 1: JSON File
Create a file called production.json
:
jsonCopyEdit{
"name": "production",
"description": "Production environment",
"cookbook_versions": {
"apache": "= 5.2.1",
"mysql": "= 8.1.0"
},
"default_attributes": {},
"override_attributes": {}
}
Then upload it with Knife:
bashCopyEditknife environment from file production.json
Option 2: Knife Command
You can also create an environment directly:
bashCopyEditknife environment create staging
This opens an editor where you can define the environment name, description, and cookbook versions.
Step 2: Assign a Node to an Environment
Once your environments are defined, you can assign a node to one using Knife:
bashCopyEditknife node environment set node-name production
You can verify the environment:
bashCopyEditknife node show node-name
Look for the Environment:
field in the output.
Step 3: Control Cookbook Versions by Environment
One of the key benefits of environments is version locking. For instance:
- Development can run the latest cookbook
- Production can stay on a stable, tested version
Update the cookbook_versions
section in your environment file as needed, then re-upload it using knife environment from file
.
Step 4: Apply Environments in Your Workflow
To fully leverage environments:
- Use them in CI/CD pipelines
- Test cookbook changes in
dev
before promoting toprod
- Ensure version consistency across nodes
🔐 Security & Best Practices
- Avoid hardcoding credentials in environments
- Use encrypted data bags or secrets management tools
- Regularly review and clean unused environments
🚀 Bonus Tip: Use Berkshelf or Policyfiles
While environments are powerful, Policyfiles (a newer alternative) offer more deterministic control of your node’s run list and cookbook versions. Consider transitioning if you need better reproducibility.
Summary
Understanding and using Chef Environments is essential for any infrastructure automation workflow. On Ubuntu 24.04+, Chef integrates smoothly and offers all the tools you need to manage configuration at scale.
Need DevOps Help?
At GreenHost, we don’t just host—we help you automate. Talk to us about managed Chef deployments and infrastructure automation consulting tailored for your stack.