bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

Chef User Hand Book

Are you finding difficulty in remembering all the commands that you need in order to work with Chef? Guys don’t worry if you are a beginner and have no idea about how Chef works, this Chef cheat sheet will give you a quick reference of the basics that you must know to get started. While at Intellipaat, we support our learners with a handy reference, that’s the reason we have created this Cheat sheet. This cheat sheet is designed for the one who has already started learning about Chef but needs a handy reference sheet.

Chef Cheat Sheet

What is Chef?

It is open source and released by Opscode. It is a config management technology developed primarily to automate infrastructure provisioning.

  • It is used to deploy and manage servers on-premise or in the cloud.
  • It uses ruby as its reference language.

Now Discussing its features:

  • No assumptions are made, 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 a basic development experience.

Learn DevOps in 16 hrs from experts

Terminology

Before proceeding towards its components & basic commands, let’s understands the basic terminologies of Chef.

  • Node: A managed machine which executes the configuration for the node when the client runs.
  • Client: It is an authorized user in the chef API.
  • Cookbook: A collection of recipes, resources, attributes and definitions to configure a service or application.
  • Recipes: A list of resources to be added to a node, as it is written in ruby, so it gives you control of anything you would do in ruby.

Components

Now let’s check the important component used in chef.

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

Architecture

Chef basically works on a 3-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, which are Chef Workstation, Chef Server, Chef Nodes.

Architecture

  • Chef workstation:

    • Configurations are developed and installed on 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-premise.
  • Chef nodes:

    • End machines managed by the server.
    • Contains the client that sets communication between the server and node.
    • Ohai is another component of Chef Node which returns the current state of any node.

Wish to Learn DevOps? Click Here

Runlist

Runlist provides the recipes and the roles required for the node.Different from other config management tools that makes you define a relationship between each resource. Hence ordered list is easier to understand and use.

  • Using knife to get info about a node and the runlist:

$ 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 had one runlist.
  • To add more roles using knife:

$ knife node run_list add s1.mydomain.com “role[profit]”

run_list:

role[common]

role[profit]

Cookbooks

A working unit of chef which is used for configuration and policy distribution.

Cookbooks will define a scenario and will contain everything which is required to provision that scenario.

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

Metadata.rb

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

Main parts are version and depends statements.

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

README.rdoc

It contains the documentation of the cookbook on how to use it and 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 in the community.

It supports downloading and sharing of cookbook on http://community.opscode.com/ to store, rate, and search shared cookbooks.

Testing Cookbooks

Test the cookbook to make sure it doesn’t break down during production.

Steps:

  • Install cookbook:

 example@localmach:~/chef-repo $ knife cookbook site install <cookbook name>

  • Run the test commands:

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 test command again and check the output.
    • Limitation: Only runs a syntax check on ruby and .erb files.
      • Run ChefSpec and test kitchen to have a complete test done.

Resources

  • They are ruby objects with code behind them to config the system.
  • Each resource contains providers that tell the system how to run it.
    • LOG:
      • Prints the logging message at the specified level
      • specify the level you want the message to be logged with level parameter.
    • Packages:
      • Providers allow single package resource to be used on most UNIX- based OS.
      • Default action for a package is install. Eg: package “autoconf”
      • Specifying the version is possible

package “cucumber” do

version “0.9.4”

provider Chef::Provider::Package::Rubygems

action :install

End

Files, Directories and Templates

  • It provides file, remote file and cookbook file to manage files and resource to manage directories.
  • Directories:

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

    • Allows you to manage and 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 content.
      • Remote_file: It has the source parameter that contains the URL of the file to transfer.
        • Contains optional checksum that uses SHA-256.
        • Actions are limited to create and create_if_missing.
      • Cookbook_file: It is same as remote file, but files will be retrieved from files/directory structure.
    • Templates:
      • 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 node are set. Add variables attribute which assigns an array. The array will be made available in variable @ nameservers.
Become DevOps Certified in 16 hrs.
CLICK HERE

Commands

  • Kitchen commands:

    • Kitchen list
    • Kitchen create
    • Kitchen destroy
    • Kitchen login<instance name>
  • Knife commands:

FUNCTION COMMANDS
Get version Knife –version
Create cookbook Knife cookbook create <cookbook name>
Download cookbook Knife cookbook download <cookbook_name > <version>
List cookbooks on the server Knife cookbook list
Use chef supermarket Knife cookbook site list
Getting list of all client nodes Knife client list
Add recipe to runlist for node Knife node run_list add module2 “recipe[apache]
Remove item from the runlist Knife node run_list remove module2 “recipe[apache]
  • Other useful commands:

    • Help command: knife -h
    • Search for node which are linux: knife search node “OS:linux”
    • To run on node as convergence: chef-client
    • Ro show environment: knife environment list -w
    • To delete environment: knife environment delete dev
    • To show knife environment: knife environment show dev

Download a Printable PDF of this Cheat Sheet

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

Not only will you get to learn and implement Chef with a 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. You will also get 24*7 technical support to help you with any and all your queries, from the experts in the respective technologies here at intellipaat throughout the certification period. So, why wait? Check out the training program and enroll today!

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on Chef Cheat Sheet"

Leave a Message

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
top

Sales Offer

Sign Up or Login to view the Free Chef Cheat Sheet.