Skip to main content

Command Palette

Search for a command to run...

Most Useful Chef Commands

Updated
5 min read
Most Useful Chef Commands
M

Hey, My name is Manish Maurya and I am a DevOps⚙️ Engineer who is deeply committed to improving the efficiency and effectiveness of software development and deployment processes. My extensive knowledge of cloud computing, containerization, and automation enables me to drive innovation and deliver high-quality results. I remain dedicated to continuously expanding my skills by staying up-to-date with the latest DevOps tools and methodologies, and I'm always looking for new ways to optimize workflows and improve software delivery.🤖 :)

USE

COMMAND

1

Download Chef package from chef official website

wget https://packages.chef.io/files/stable/chef-workstation/0.13.35/el/7/chef-workstation-0.13.35-1.el7.x86_64.rpm

2

Install Chef package

yum install chef-workstation-0.13.35-1.el7.x86_64.rpm -y

3

Verify Chef Pacakge

which chef

4

See the version

chef -v or chef –version

5

Create a cookbook

chef generate cookbook <cookbook name> chef generate cookbook manish-cookbook

6

Create a recipe

chef generate recipe <recipe name> chef generate recipe manish-recipe

7

Verify ruby script

chef exec ruby -c <cookbook name>/recipes/<recipe name> chef exec ruby -c manish-cookbook/recipes/manish-recipe.rb

8

Run chef-client Locally

sudo chef-client -zr “recipe[<cookbook name>::<recipe name]” sudo chef-client -zr “recipe[manish-cookbook::-recipe]”

9

Get IP Address from Ohai store

ohai ipaddress

10

Get hostname from Ohai store

ohai hostname

11

Get memory/total from ohai store

ohai memory/total

12

Get cpu/0/mhz from ohai store

ohai cpu/0/mhz

13

Run List (Each recipe from each cookbook only)

sudo chef-client -zr “recipe[<cookbook name>::<recipe name>],recipe<cookbook name>::<recipe name>] ” sudo chef-client -zr “recipe[manish-apache-cookbook::manish-apache-recipe],recipe[manish-cookbook::manish-recipe]”

14

Include_recipe (Run multiple recipes from same cookbook)

sudo chef-client -zr “recipe[<cookbook name>::default]” sudo chef-client -zr “recipe[ktexperts-apache-cookbook::default]”

15

Combine Run List and Include_recipe (Run any number of recipes from any number of cookbooks)

sudo chef-client -zr “recipe[<first cookbook name>],recipe[<second cookbook name>]” sudo chef-client -zr “recipe[manish-apache-cookbook],recipe[manish-cookbook]”

16

verify the connection between Workstation and Chef Server

knife ssl check (Be inside chef-repo)

17

Bootstrap a node

knife bootstrap <node-private IP> –ssh-user ec2-user –sudo -i <node-pem key> -N <Any node name> knife bootstrap 172.31.35.120 –ssh-user ec2-user –sudo -i chef.pem -N node1

18

Verify Nodes

knife node list

19

Upload cookbook to Chef Server

knife cookbook upload <cookbook name> knife cookbook upload manish-apache-cookbook

20

See the list of cookbooks

knife cookbook list

21

Attach recipe to node

knife node run_list set <node-name> “recipe[<cookbook name>::<recipe name>]”
knife node run_list set node1 “recipe[manish-apache-cookbook::manish-apache-recipe]”

22

Verify the recipe added to the node or not

knife node show <node-name> knife node show node1

23

Automate the chef-client (Add in node)

vi /etc/crontab root chef-client

24

Automate the chef-client (Add in user data)

#!/bin/bash
sudo sudo
yum update -y
echo “ root chef-client” >> /etc/crontab

25

Create a role

vi roles/manishweb.rb
name ‘manish-web’
description “manish web server role”
run_list “recipe[<cookbook name>::<recipe name]”,”recipe[<cookbook name>::<recipe name]”,”recipe[<cookbook name>]”
run_list “recipe[manish-apache-cookbook::manish-apache-recipe]”,”recipe[manish-apache-cookbook::manish-sample-recipe]”,”recipe[manish-apache-cookbook]”

26

Upload role to chef server

knife role from file roles/<role-name> knife role from file roles/manish-web.rb

27

Attach node to Role

knife node run_list set <node-name> “role[<role-name>]” knife node run_list set node1 “role[manish-web]”

28

Verify the node added to the roles or not

knife node show <node-name> knife node show node1

29

Upload all cookbooks to Chef Server

knife cookbook upload –all

30

See the list of Clients

knife client list

31

See the list of Roles

knife role list

32

Delete cookbooks from chef server

knife cookbook delete <cookbook name> knife cookbook delete manish-apache-cookbook

33

Delete nodes from chef server

knfie node delete <node name> knfie node delete <node1>

34

Delete roles from chef server

knife role delete <role name> knife role delete manish-web.rb

35

Delete clients from chef server

knife client delete <client name> knife client delete node1

Ruby Script for Single Resource

SNO

ACTION

SCRIPT

1

Create a file

file ‘monk-file1’ do
content ‘manishworld is a knowledge sharing platform’
action :create
end

2

Install package

package ‘tree’ do
action :install
end

3

Create a web server

package ‘tree’ do
action :install
endfile ‘/var/www/html/index.html’ do
content “momk is a knowledge sharing platform”
action :create
endservice ‘httpd’ do
action [ :enable, :start ]
end

4

Update the configuration information

file ‘/robofile’ do
content “This is to get Attributes
HOSTNAME: #{node[‘hostname’]}
IPADDRESS: #{node[‘ipaddress’]}
CPU: #{node[‘cpu’][‘0’][‘mhz’]}
MEMORY: #{node[‘memory’][‘total’]}”
owner ‘root’
group ‘root’
action :create
end

5

Create a user

user ‘ramesh’ do
action :create
end

6

Create a group

group “DevOps” do
action :create
end

7

Add user to group

group “DevOps” do
members”manish”
append true
end

8

Create user,group and file

user “Rammy”
group “monkworld”
file “/rammyfile”

9

Execute Linux commands

execute “run a script” do
command <<-EOH
mkdir /syaamdir
touch /raamfile
EOH
end

10

Download package from internet

remote_file “/home/ec2-user/chef-workstation-0.13.35-1.el7.x86_64.rpm” do
source “https://packages.chef.io/files/stable/chef-workstation/0.13.35/el/8/chef-workstation-0.13.35-1.el7.x86_64.rpm”
action
:create
end

Ruby Script for Multiple Resources

SNO

ACTION

SCRIPT

1

Install packages

%w(tree mysql httpd).each do |p|
package p do
action :install
end
end

2

Create users

%w(Ramesh Ajay Vinod).each do |p|
user p do
action :create
end
end

3

Create files

%w(/mtfile1 /mtfile2 /mtfile3).each do |p|
file p do
action :create
end
end

4

Create directories

%w(/mtdir1 /mtdir2 /mtdir3).each do |path|
directory path do
action :create
end
end

5

Create groups

%w(mtexperts1 ktexperts2 mtexperts3).each do |grp|
group grp do
action :create
end
end

6

Add users to single group

group ‘mtexperts1’ do
action :modify
members [‘Ramesh’,’Ajay’,’Vinod’]
append true
end

Thank you for giving your valuable time to read the above information.

More from this blog

MANISH KUMAR's blog

7 posts

I'm 24 year old MCA student and I want to make my career in Devops for which I am fully dedicated and I want to learn a lot new thing in this field..