What is Chef in devops? How its works ? A hands-on demo for beginner

In today’s fast-paced IT world, efficient configuration management is paramount to ensure the smooth operation and scalability of infrastructure. Several automation tools exist to make this task easier, and among them are Ansible, CFEngine, Puppet, and Chef. In this article, we will focus on Chef, a robust and widely-used configuration management tool. We will provide an overview of Chef’s architecture and then dive into a hands-on demonstration of how to set up a basic configuration using Chef.

Understanding Chef’s Architecture

Before we start our hands-on demo, let’s briefly explore the architecture of Chef to better understand how it works.

Key Concepts:

  1. Cookbooks: Cookbooks are containers for recipes. Each cookbook can contain multiple recipes, and these recipes are essentially sets of instructions written in Ruby code. They dictate how to configure a specific aspect of your infrastructure.
  2. Recipes: Recipes are the heart of Chef. They provide step-by-step instructions for configuring individual resources on a server. You can think of them as scripts that Chef uses to bring your infrastructure to the desired state.
  3. Nodes: Nodes are the servers or systems that you want to manage with Chef. Each node runs the Chef client, which communicates with the Chef server to fetch and apply configurations.
  4. Chef Server: The Chef server is the central hub that stores cookbooks, node data, and configuration information. It manages authentication and distributes configurations to nodes.
  5. Chef Client: The Chef client is installed on nodes and runs as a daemon. It periodically checks for updates from the Chef server and applies them to ensure that the server’s configuration matches the desired state.

Environments and Attributes:

Chef also allows you to define different environments for your infrastructure, such as development, QA, and production. Each environment can have its set of attributes, which are variables you can use to customize configurations based on the environment.

Hands-On Demonstration: Setting Up Tomcat and a Hello World Application

In this demonstration, we’ll create a simple Chef cookbook to install the Apache Tomcat server and deploy a basic “Hello World” application on an Ubuntu virtual machine using Vagrant and VirtualBox. Here are the prerequisites:

  • VirtualBox
  • Vagrant
  • Chef Workstation (installed on your local machine)

Step 1: Create a Cookbook

  1. Open your terminal and navigate to a directory where you’d like to create your Chef cookbook. Run the following command to create a new cookbook named hello_world:
chef generate cookbook hello_world
  1. Change into the hello_world directory:
cd hello_world
  1. Inside the recipes directory, edit the default.rb file. This is where we’ll define our recipe to install Tomcat and deploy the “Hello World” application. Replace the content with the following code:
# recipes/default.rb

# Install Tomcat
package 'tomcat7' do
  action :install

# Create the directory for the Hello World app
directory '/var/www/hello_world' do
  owner 'tomcat7'
  group 'tomcat7'
  mode '0755'
  recursive true
  action :create

# Deploy the Hello World index.html file
template '/var/www/hello_world/index.html' do
  source 'index.html.erb'
  owner 'tomcat7'
  group 'tomcat7'
  mode '0644'
  action :create

# Start and enable the Tomcat service
service 'tomcat7' do
  action [:enable, :start]

Step 2: Create the Index HTML Template

  1. Create a new file named index.html.erb in the templates/default directory with the following content:
<!-- templates/default/index.html.erb -->
    <title>Hello World</title>
    <h1>Hello, World!</h1>

Step 3: Upload the Cookbook to Chef Server

  1. Make sure you’re in the hello_world cookbook directory.
  2. Use the following command to upload the cookbook to your Chef Server (replace <YOUR_CHEF_SERVER_URL> and <YOUR_ORGANIZATION> with your Chef Server URL and organization name):
knife cookbook upload hello_world -o .

Step 4: Create a Vagrantfile

  1. Create a Vagrantfile in the root directory of your project with the following content:
# Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.network "private_network", type: "dhcp"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"

  config.vm.provision "chef_solo" do |chef|
    chef.cookbooks_path = ["cookbooks", "site-cookbooks"]
    chef.add_recipe "hello_world"

Step 5: Provision the Virtual Machine

  1. Open your terminal and navigate to your project directory.
  2. Run the following command to create and provision the virtual machine using Vagrant:
vagrant up

Vagrant will create a new virtual machine, apply the Chef cookbook, and configure Tomcat along with the “Hello World” application.

Step 6: Access the Hello World Page

  1. Once the provisioning is complete, SSH into the virtual machine:
vagrant ssh
  1. Open a web browser on your host machine and enter the following URL to access the “Hello World” page:

You should see the “Hello, World!” message displayed.

Congratulations! You’ve successfully set up a basic Chef cookbook to install Tomcat and deploy a “Hello World” application. This demonstration provides a glimpse into how Chef can be used for configuration management and automation in your infrastructure.


Chef is a powerful configuration management tool that allows you to automate and streamline your infrastructure provisioning and management tasks. In this hands-on demonstration, you’ve learned how to create a Chef cookbook to set up a Tomcat server and deploy a simple web application. This is just the beginning of what Chef can do; it can be used to manage complex configurations, enforce compliance, and maintain infrastructure at scale. Explore more Chef resources and experiment with different recipes and cookbooks to fully harness its capabilities in your environment.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top