Introduction to Chef on Linux
Chef is a configuration automation platform that allows you to manage Linux servers consistently and reproducibly. In this article we will explore how to install Chef Workstation, create basic recipes, and apply changes to nodes using the chef-client.
Prerequisites
- A recent Linux distribution (Ubuntu 22.04, Debian 12, CentOS Stream 9)
- Sudo privileges
- Internet connection to download packages
Installing Chef Workstation
The first step is to download the official package from the Chef website. On Debian-based systems we can use wget and dpkg:
wget https://packages.chef.io/files/stable/chef-workstation/22.3.108/ubuntu/22.04/chef-workstation_22.3.108-1_amd64.deb sudo dpkg -i chef-workstation_22.3.108-1_amd64.deb
On Red Hat-based systems we use rpm:
wget https://packages.chef.io/files/stable/chef-workstation/22.3.108/el/9/chef-workstation-22.3.108-1.el9.x86_64.rpm sudo rpm -Uvh chef-workstation-22.3.108-1.el9.x86_64.rpm
After installation, verify the version with:
chef --version
Creating Your First Recipe
A Chef recipe is a Ruby file that declares the desired resources. We will create a simple recipe that installs and enables the nginx service.
# file: my_cookbook/recipes/nginx.rb package 'nginx' do action :install end service 'nginx' do action [:enable, :start] end
Save the file inside a cookbook named my_cookbook. Then upload the cookbook to the Chef Server or use chef-solo for local testing.
Running chef-client on Nodes
To apply the configuration to a Linux server, install the chef-client on the node and run:
sudo chef-client -z -o my_cookbook::nginx
The -z option allows running in local mode without needing a Chef Server. The node will consult the recipe and ensure that nginx is installed and running.
Advantages of Using Chef in Linux Environments
- Idempotency: applying the same recipe multiple times does not cause unwanted changes.
- Versioning: cookbooks can be versioned with Git, facilitating rollbacks.
- Scalability: manage from a few to thousands of nodes with the same definition.
- Cloud integration: plugins for AWS, Azure, GCP allow automatic provisioning and configuration of instances.
Testing with Test Kitchen
Before promoting a cookbook to production, it is advisable to validate it in isolated environments. Test Kitchen allows launching virtual machines or containers, applying the recipe, and verifying the state.
# .kitchen.yml
driver:
name: docker
provisioner:
name: chef_zero
platforms:
- name: ubuntu-22.04
suites:
- name: nginx
run_list:
- recipe::my_cookbook::nginx
attributes:
Run kitchen test and observe that the tests pass. This ensures that the recipe works on a clean image.
CI/CD Integration
Chef integrates easily with Jenkins, GitLab CI, or GitHub Actions pipelines. A simple example in GitHub Actions:
name: Chef CI
on: [push]
jobs:
test-cookbook:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Chef Workstation
run: |
wget https://packages.chef.io/files/stable/chef-workstation/22.3.108/ubuntu/22.04/chef-workstation_22.3.108-1_amd64.deb
sudo dpkg -i chef-workstation_22.3.108-1_amd64.deb
- name: Run syntax check
run: cookstyle
- name: Run Test Kitchen
run: kitchen test
This way each push triggers an automatic validation.
Conclusion
Chef positions itself as a robust tool for Linux infrastructure automation. Its code-based approach and active community make it a valid option for both small teams and large enterprises. By following the steps described in this post, you can begin managing your servers consistently and reduce the time spent on manual tasks.