Jump to: navigation, search



  • Organizations
  • Environments
  • node - An ordered list of roles and/or recipes that are run in the exact orde
  • resource - package, template, service, user, group
  • recipe - collection of resources, defined using patterns (resource names, attribute-value pairs, and actions)
  • cookbook - A cookbook is the fundamental unit of configuration and policy distribution. A cookbook defines a scenario and contains everything that is required to support that scenario:
  * Recipes that specify the resources to use and the order in which they are to be applied
  * Attribute values
  * File distributions
  * Templates
  * Extensions to Chef, such as libraries, definitions, and custom resources
  • run list - A run-list defines all of the information necessary for Chef to configure a node into the desired state
  * An ordered list of roles and/or recipes that are run in the exact order
  • roles -Each role consists of zero (or more) attributes and a run-list. Each node can have zero (or more) roles assigned to it.
  • search


hosted chef:

download the started kit

getchef client

chef server


install chef-client on Amazon AWS linux API (debian)

url -L | sudo bash
curl -L | bash /dev/stdin -v 11.4.4

install (bootstrap) on remote node:

knife bootstrap -x root -P Ni92nf24nd -N module2

install (bootstrap) a remote note with a role and environment:

knife bootstrap -x root -P Ni92nf24nd -N module2 -r 'role[webserver]' -E production

cookbook recipe and run list

create a cookbook:

 knife cookbook create apache

example of recipes

package httpd do 
 action :install
service "httpd" do
 actopn [:enable, :start]
template "/var/www/html/index.html" do
 source "index.html.erb"
 mode "0644"

yum install

yum_package "java-1.7.0-openjdk-src.x86_64" do 
 action :install

upload cookbook to the chef server:

knife cookbook upload apache

upload all the cookbooks in one command:

knife upload cookbooks 

module2 server has a new run_list with the "apache" recepi

knife node run_list add module2 "recipe[apache]"

remove a run_list from a server

knife node run list remove module2 "recipe[apache]"

run chef-client on a node

sudo chef-client

cookbook version

compare current cookbook with server version

knife diff .\cookbooks\apache

change the file version in cookbook\apache\templates\metadata.erb

version        0.2.0

using Ruby variables - ohai attributes

OHAI command - all the info about the node - on the client

ohai hostname
ohahi memory

My Name is <%= node['hostname'] %>.

on the managment:

knife node show ip-172-30-0-201

-l to show long details

knife node show ip-172-30-0-201 -l

-a to see the attributes of a node

knife node show ip-172-30-0-201 -a hostname
knife node show ip-172-30-0-201 -a memory

custom Attributes

you can set attributes in:

  • roles
  • env
  • cookbook recipe
  • cookbook attribute file

cookbook attributes

in the cookbook attributes file

default['apache']['dir'] = "/etc/apache2"
default['apache']['greeting'] = "World"


Hello, <%= node['apache']['greeting'] %>.


more info at

role file (webserver.json)


update the role file in the server

knife role from file webserver.json

add role to a server

knife node run list add module3 "role[webserver]"

run script and script file

  • run script file
cookbook_file "/tmp/" do
 source ""
 mode 0755

execute "install my lib" do
 command "sh /tmp/"
  • run script
script "install_something" do
 interpreter "bash"
 user "root"
 cwd "/tmp"
 code <<-EOH
   #insert bash script
  • run python script
filename = ""
execute 'execute_file' do
 cwd '/home/peeyush'
 command "python #{filename}"

Environments in an Organization

  • each cookbook can have versions in the metadata.rb files
  • each environment defined the version of each cookbook
  • each node belong to a specific environment

Organization: Production, Development, Staging ...

knife environment show _default3

to see the apache cookbook versions

knife cookbook show apache

list of environments:

knife environment list

create an environment, with a specific version of cookbook:

in the chef-repo path create a new folder called environments and create a file called "dev.rb" in this file set:
name "dev"
description "development"
cookbook "apache", "= 0.2.0"

update the new environment to teh server:

knife environment from file dev.rb
knife environment list
knife cookbook show dev

In order to change the environment of a node you can change it from the Chef server web managment bootstrap a node with an environment

knife bootstrap -x root -P Ni92nf24nd -N module2 -r 'role[webserver]' -E production

edit environment of a node

knife node environment_set NODE_NAME ENVIRONMENT_NAME (options)


  • by any attribute, ohai and others:
knife search node "platform:centos"

knife ec2


sudo gem install knife ec2

in powershell:

gem install knife-ec2
gem install knife-windows

add the AWS account credentials to a file : add the following to the chef-repo/.chef/knife.rb file:

# Amazon AWS
knife[:aws_access_key_id] = "AWS_ACCESS_KEY_ID"
knife[:aws_secret_access_key] = "AWS_SECRET_ACCESS_KEY"
# knife[:aws_ssh_key_id] = "openvoip2"

list of servers

knife ec2 server list --region eu-west1

create ec2 instance in a vpc if the AWS cerdentails are updated in the knife.rb files:

 knife ec2 server create -I ami-6e7bd919 --region eu-west-1 --flavor t2.micro --subnet subnet-83293ac5
 knife ec2 server create -I ami-6e7bd919 -N MyEc2Instance -x ec2-user -r "role[webserver]" --region eu-west-1

run chef command on remote nodes by search creteria

 knife ssh "role:webserver" "sudo chef-client" -x ec2-user

chef supermarket

install community cookboob - chef client that runs chef-client as a service

knife cookbook site download chef-client

in the site you will see that there are dependencies - install them too :

knife cookbook site download cron
knife cookbook site download logrotate


tar -zxvf chef-client.x.x.tar.gz -C cookbooks
tar -zxvf cron.x.x.tar.gz -C cookbooks
tar -zxvf logrotate.x.tar.gz -C cookbooks
rm -rf *.gz


 knife upload cookbooks
Personal tools