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.

Watch this video on DevOps Tutorial for Beginners


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.

Prepare yourself for the Top DevOps Interview Questions And Answers!
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.

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.

Become Master of DevOps by going through this online DevOps training in London.

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.


Learn more about DevOps in this DevOps training in Sydney to get ahead in your career!

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]

If you have any doubts or Queries related to DevOps, get it clarifies from DevOps Experts on DevOps Community.

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.

Interested in becoming DevOps Expert? Click here to learn more in this DevOps Course in Toronto!

Watch this interesting video on DevOps Training

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

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

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.

Now, you know the most important skill set of a DevOps Engineer. But, do you know, DevOps Engineers are among the highest paid professionals in the technology domain? so join DevOps training in Hyderabad!

Commands

  • Kitchen commands:

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

FUNCTIONCOMMANDS
Get versionKnife –version
Create cookbookKnife cookbook create <cookbook name>
Download cookbookKnife cookbook download <cookbook_name > <version>
List cookbooks on the serverKnife cookbook list
Use chef supermarketKnife cookbook site list
Getting list of all client nodesKnife client list
Add recipe to runlist for nodeKnife node run_list add module2 “recipe[apache]
Remove item from the runlistKnife 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

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

Leave a Reply

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