Chef Cookbooks and Recipes Basics and common Chef Resources

Chef Cookbooks and Recipes Basics and common Chef Resources

In this article you will know the basics about Chef Cookbooks and recipes.

Cookbook folder Example:
attributes
definitions
files
libraries
providers
recipes
resources
templates
chefignore "This folder will be ignored when upload to server"
spec "For unit testing"
test "For testing using framework called testkitchen"
CHANGELOG.md
metadata.rb "Have version, name, maintainer..etc"
README.md

Cookbook is a folder or a container which includes recipes. Recipes can include multiple recipes and also resources.

What is Chef-client

Each slave/node/agent server will have this client service which will be running every time interval to pull configuration/policy from the Chef server.

Create your first Chef cookbook

From Chef Workstation Server

knife cookbook create motd
Or
chef generate cookbook <name>

It will be created in /var/chef/cookbook/motd or your cookbook folder path and all the files will be created.

vi /var/chef/cookbooks/motd/recipes/default.rb

Sample (Below reciped will create motd file and put "welcome to Chef" as a content of the file:

Syntax usually is

ResourceType 'name' do
action :action
attribute "value"
end

Example:

file '/etc/motd' do
content 'welcome to Chef'
end

To check the syntax you can run

Knife cookbook test motd

Next step to upload the cookbook to server

Knife cookbook upload motd

 

Another recipe example, below will install the apache and run it.

package "httpd" do
action :install
end

service "httpd" do
action [:start, : enable]
end

 

What is Chef Run_List

Chef run_list is a list of recipes which are assigned for the node to run.

Add recipe to specific node run_list

knife node run_list set <node-name> 'recipe[apache]'

You can also go to the GUI and from there you will find a list to drag and drop recipes.

 

Common Chef Resources

  1. Package
  2. Service
  3. Directory
  4. File
  5. Bash
  6. Execute
  7. Cron
  8. Cookbook_File
  9. remote_file
  10. User
  11. Group

Package Actions
:install
:upgrade
:remove
:purge
:reconfig

Package Attributes
allow_downgrade
arch
flush_cache
options
source
version

 

Service Actions
:enable
:start
:disable
:stop
:restart
:reload

Service Attributes
init_command
priority
start_command
stop_command
restart_command
status_command
reload_command
supports

Directory Actions
:create
:delete

Directory Attributes
path
owner
group
mode
recursive

 

File Actions
:create
:delete
:create_if_missing
:touch

File Attributes
path
content
owner
group
mode

 

Bash Actions
:run
:nothing

Bash Attributes
code
command
creates
cwd
user
group
umask
environment
path
return
flags

 

Execute Actions
:run
:nothing

Execute Attribute
command
create
cwd
user
group
umask
environment
timeoutpath
return

 

 

Cron Actions
:create
:delete

Cron Attributes
command
day
hour
minutes
month
week
user
mailto

 

Cookbook_file Resource
:create
:delete
:create_if_missing
:touch

Cookbook_file Actions
path
content
owner
source
backup

What is Ohai in Chef ?

Ohai is a system information gathering tool which is installed by default with Chef, this tool is useful to query any of the system information which will be helpful inside cookbooks or to build if conditions...etc

Examples:

Ohai memory
ohai cpu
ohai memory/total
ohai cpu/0/mhz
ohai ipaddress

How to call any of the information inside the recipe?

Example:
node['ipaddress']
But to add it as a variable inside the Ruby recipe, use below syntax:
#{WhatEverHere}
Ex.:
#{node['ipaddress']}

Everytime Chef-Client runs it will run Ohai and gather all information about the node.

 

Cookbook Recipes Examples

# You can declare attributes/variable like below

package_name = "httpd"
service_name = "httpd"
doc_root = "/var/www/html/"

# You can have conditions like below, node["platform"] is a dynamic call using Chef Ohai "which is the service which collects data about nodes"

if node ["platform"] == "centos"
package_name = "httpd"
service_name = "httpd"
doc_root = "/var/www/html/"
elsif node ["platform"] == "ubuntu"
package_name = "apache2"
service_name = "apache2"
doc_root = "/var/www"
end

# Use the cookbook_file resource to transfer files from a sub-directory of COOKBOOK_NAME/files/ to a specified path

cookbook_file "/var/www/html/index.html" do
source "index.html"
mode "0644"
end

How to run Recipe from Node or locally?

chef-client -z -r "recipe[apache::install],recipe[apache::update]"

Above is running multiple recipe, use -z for local run and -o if you are calling from the node

 

How to run/call recipe from inside another recipe?

Simply open any recipe and include the other recipe you want to run.

For example you have a cookbook called Apache and you want to run Install recipe from the default recipe.

vi cookbook/apache/recipes/default.rb

include_recipe 'apache::install'

Now if you run the default recipe it will call and run the Install recipe from apache/recipes/install.rb.

 


Subscribe to
for video tutorials updates