Chef in Linux: Automate your infrastructure with confidence

Introduction to Chef in Linux

Chef is a configuration automation platform that allows to manage Linux servers consistently and reproducible. In this article we will explore how to install Chef Workstation, create basic recipes and apply changes to nodes using the chef-client.

Previous requirements

  • A recent Linux distribution (Ubuntu 22.04, Debian 12, CentOS Stream 9)
  • Access with sudo privileges
  • Internet connection to download packages

Chef Workstation installation

The first step is to download the official package from Chef's site. In 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.desudo dpkg -i chef-workstation _ 22.3.108-1 _ amd64.deb

In Red Hat based systems rpm is used:

wget https: / / packages.chef.io / files / stable / chef-workstation / 22.3.108 / el / 9 / chef-workstation-22.3.108-1.el9.x86 _ 64.rpmsudo rpm -Uvh chef-workstation-22.3.108-1.el9.x86 _ 64.rpm

After installation, check the version with:

chef --version

Creating your first recipe

A recipe in Chef is a Ruby file that declares the desired resources. We will create a simple recipe that will install and activate the nginx service.

# file: my _ cookie / recipes / nginx.rbpackage 'nginx' do action: installendservice 'nginx' do action [: enable,: start] end

Save the file inside a cookie called my _ cookie. Then upload the cookie to Chef Server or use chef- only for local testing.

Running chef-client in nodes

To apply the configuration to a Linux server, install the chef-client in the node and run:

sudo chef-client -z -o my _ cookie:: nginx

The -z option allows you to run in local mode without the need for a Chef Server. The node shall 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.
  • Versioned: cookies can be versed 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 to supply and configure instances automatically.

Test Kitchen

Before promoting a cookie to production, it is recommended to validate it in isolated environments. Test Kitchen allows you to launch virtual machines or containers, apply the recipe and verify the status.

# .kitchen.ymldriver: name: dockerproviser: name: chef _ zeroplatforms: - name: ubuntu-22.04suites: - name: nginx run _ list: - recipe:: my _ cookie:: nginx attributes:

Run kitchen test and watch the tests pass. This ensures that the recipe works in a clean image.

Integration with CI / CD

Chef easily integrates with Jenkins, GitLab CI or GitHub Actions. A simple example in GitHub Actions:

name: Chef Cion: [push] jobs: test-cookie: 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: cookie - name: Run Test Kitchen run: kitchen test

This way each push triggers an automatic validation.

Conclusion

Chef is positioned as a robust tool for the automation of Linux infrastructure. Its code-based approach and active community make it a valid option for both small and large companies. By following the steps described in this post, you can start to manage your servers consistently and reduce the time spent on manual tasks.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish