• Articles
  • Tutorials
  • Interview Questions

Chef Cheat Sheet

Chef User Handbook

Are you finding difficulty in remembering all the commands that you need in order to work with Chef? Don’t worry, guys, this Chef cheat sheet will give you a quick reference to the basics that you must know to get started with Chef.

Download a Printable PDF of This Cheat Sheet

Chef Cheat Sheet

We, at Intellipaat, are ready to support our learners in all possible ways, and that’s the reason we have created this handy reference, the Chef cheat sheet. This cheat sheet is designed for those who have already stepped forward into learning Chef but needs a more handy aid.

What is Chef?

This open-source tool, released by Opscode, is a config management technology developed primarily to automate infrastructure provisioning.

  • Chef is used to deploy and manage both on-premises servers and those hosted in the cloud.
  • It uses Ruby as its reference language.

Prepare yourself for any DevOps job interview by going through these Top DevOps Interview Questions and Answers!

Now, let’s move ahead to discuss the features of Chef.

  • Chef makes no assumptions; it gets the current status of the machine via certain mechanisms.
  • It is an excellent tool for integration with the cloud.
  • As it uses Ruby, it is easy for anyone to get into Chef with basic development experience.

Chef Terminology

Before proceeding toward its components and basic commands, let’s first understand the basic terms used in Chef.

  • Node: A managed machine that executes the configuration for the node when the client runs
  • Client: An authorized user in the Chef API
  • Cookbook: A collection of recipes, resources, attributes, and definitions to configure a service or an application.
  • Recipe: A list of resources to be added to a node. As it is written in Ruby, it gives us control over anything we would do in Ruby.

Become a master of DevOps by going through this online DevOps Training in London!

Chef Components

Now, let’s check out the important components used in Chef.

  • Knife: A system admin tool used to interact with the server to take cookbooks and custom config and load them into the server. Bootstrapping certain servers is also possible with this component.
    • Running knife- shows a list of commands that are supported.
  • Chef client: It runs on managed servers, gathers information about itself, syncs the cookbooks, and compiles the collection of resources and converges it with the machine state.
  • Web UI: A web-based interface that allows us to browse and edit cookbooks, nodes, and clients.
  • Server/API: The heart of the system that exposes a REST API that is used by others and manages the knife, web interfaces, and nodes.

Get 100% Hike!

Master Most in Demand Skills Now!

Chef Architecture

Chef basically works on a three-tier client–server model. Command-line utilities are uploaded to the server, and all nodes are registered with the server.

Chef has three main players in its architecture; they are Chef Workstation, Chef Server, and Chef Nodes.
Architecture

Chef Workstation

  • Configurations are developed and installed on the local machine.

    Chef Server

  • It is the center of the Chef setup.
  • Config files are uploaded here.
  • Some are hosted, and some are built on-premises.

    Chef Nodes

  • It is the end machine managed by the server.
  • It contains the client that sets communication between the server and the node.
  • Ohai is another component of Chef Node, which returns the current state of any node.

Learn more about DevOps in this DevOps Training in Sydney and excel in your career!

Run-list

A run-list provides recipes and roles required for a node. Unlike the other config management tools, it makes us define a relationship between resources. Hence, an ordered list is easier to understand and use.

  • Using knife to get the information about a node and the run-list:
$ knife node show s1.mydomain.com

Node Name: s1.mydomain.com

Environment:    _default

FQDN: s1.mydomain.com

IP: 1.2.3.4

Run List: role[common]

Roles: common

Recipes: chef-client, users::sysadmins, sudo

Platform: ubuntu 10.1

The above server has one run-list.

  • To add more roles using knife:
$ knife node run_list add s1.mydomain.com “role[profit]”

run_list:

role[common]

role[profit]

Cookbooks

A cookbook is a working unit of Chef that is used for configuration and policy distribution. It will define a scenario and will contain everything that is required to provide that scenario.

Knife cookbook create name [NAME] creates a new directory for the cookbook.

Metadata.rb

It is converted to JSON when it gets installed in the server and returns the name, version, and other properties. Most of the contents in it are for human use and are displayed on the interface.

The main parts are ‘version’ and ‘depends’ statements.

  • Version: Lets us set a specific version of a cookbook
  • Depends: Defines other cookbooks, which are required for this to work, includes a version number which is optional.

Interested in becoming a DevOps expert? Sign up for our DevOps Course in Toronto and excel in your career!

README.rdoc

It contains the documentation of the cookbook on how to use it, and this is useful when it is being shared with others. Markdown is supported while rdoc is the default.

Sharing Cookbooks

Since it is open source, it allows the sharing of cookbooks across the community. It supports the downloading and sharing of cookbooks on http://community.opscode.com/ where we can store, rate, and search for the shared cookbooks.

Testing Cookbooks

We need to test the cookbook to make sure that it doesn’t break down during production. How do we test a cookbook? Let’s check out the steps to test a cookbook.

  • Install the cookbook
 example@localmach:~/chef-repo $ knife cookbook site install <cookbook name>
  • Run the test command:
 example@localmach:~/chef-repo $ knife cookbook test VTest
checking ntp
Running syntax check on ntp
Validating ruby files
Validating templates
  • Break something in it and test again:
example@localmach:~/chef-repo $ subl cookbooks/VTest/recipes/default.rb
...
[ node['ntp']['varlibdir']
node['ntp']['statsdir'] ].each do |ntpdir|
directory ntpdir do
owner node['ntp']['var_owner']
group node['ntp']['var_group']
mode 0755
end
End
  • Run the test command once again and check the output
  • Limitation: Only runs a syntax check on Ruby and .erb files
    • We have to run ChefSpec and test kitchen to have the complete test done

Resources

Resouces are Ruby objects with code behind them to config the system. Each resource contains providers that tell the system how to run it.

Log

  • It prints the logging message at the specified level.
  • We need to specify the level we want the message to be logged with the level parameter.

Package

  • Providers allow a single package resource to be used on most UNIX-based OS.
  • The default action for a package is ‘install,’ e.g.: package “autoconf”
  • It is possible to specify the version here.
package “cucumber” do

version “0.9.4”

provider Chef::Provider::Package::Rubygems

action :install

End

Interested in getting an industry-recognized certification in DevOps? Enroll in Intellipaat’s DevOps Course in Bangalore now!

Files, Directories, and Templates

It provides a file, remote file, and cookbook file to manage files and a resource to manage directories.

Directories

  • Create, remove, and manage directory permissions
  • Owner and group will be kept default for the client, usually the root.
  • Defaults can make cookbooks more concise although they shouldn’t be confusing.

Files

  • Allow us to manage permissions and ownership of the files on the node.
  • To retrieve a file from the URL or cookbook, use remote_file or cookbook_file. They have a backup attribute that defines how many backfiles exist upon changing the content
    • remote_file: It has the source parameter that contains the URL of the file to transfer.
      • It contains an optional checksum that uses SHA-256.
      • Actions are limited to create and create_if_missing.
    • cookbook_file: It is the same as the remote file, but here files will be retrieved from the files/directory structure.

Templates

  • It supports text-based config files using ERB.
  • Ruby code is wrapped in brackets. Things that are not parsed are not executed as Ruby code.
  • Templates for complex configs can be created.
  • Just as in a cook_book file resource, source and nodes are set. Add variables attribute that will assign an array. The array will be made available in variable @ nameservers.

DevOps Engineers are among the highest paid professionals in the technology domain? Join the DevOps Training in Hyderabad and be one!

Commands

Kitchen Commands

  • Kitchen list
  • Kitchen create
  • Kitchen destroy
  • Kitchen login<instance name>

Knife Commands

Function Command
To get the version Knife –version
To create a cookbook Knife cookbook create <cookbook name>
To download a cookbook Knife cookbook download <cookbook_name > <version>
To list the cookbooks on the server Knife cookbook list
To use Chef supermarket Knife cookbook site list
To get the list of all client nodes Knife client list
To add a recipe to the run-list for a node Knife node run_list add module2 “recipe[apache]
To remove an item from the run-list Knife node run_list remove module2 “recipe[apache]

become a devops architect

Other Useful Commands

The help command:

knife -h

The command to search for a node in Linux:

knife search node “OS:linux”

The command to run on a node as a convergence:

chef-client

The command to show the environment:

knife environment list -w

The command to delete the environment:

knife environment delete dev

The command to show the knife environment:

knife environment show dev

We have covered all the basics of Chef in this Chef cheat sheet. If you want to start learning DevOps in-depth, then check out the DevOps Certification designed by Intellipaat.

Not only will you get to learn and implement Chef with step-by-step guidance and support from us but you will also get to learn other important topics in DevOps such as Docker, Git/GitHub, Ansible, Jenkins, Puppet, Selenium, Nagios, and many more.

With Intellipaat, you will also get 24/7 technical support from us, which will help you with any of your queries in the respective technologies throughout the certification period. So, why wait? Check out the training program and enroll today!

We hope this tutorial helps you gain knowledge of DevOps course online. If you are looking to learn DevOps course in a systematic manner with expert guidance and support then you can enroll to our DevOps Training.

Course Schedule

Name Date Details
DevOps Certification 23 Nov 2024(Sat-Sun) Weekend Batch View Details
30 Nov 2024(Sat-Sun) Weekend Batch
07 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Technical Research Analyst - Big Data Engineering

Abhijit is a Technical Research Analyst specialising in Big Data and Azure Data Engineering. He has 4+ years of experience in the Big data domain and provides consultancy services to several Fortune 500 companies. His expertise includes breaking down highly technical concepts into easy-to-understand content.