Most Useful Chef Commands

Share via:

Dear Readers,

In this article,we will see Most useful Chef Commands.

SNO 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 ktexperts-cookbook
6 Create a recipe chef generate recipe <recipe name> chef generate recipe ktexperts-recipe
7 Verify ruby script chef exec ruby -c <cookbook name>/recipes/<recipe name> chef exec ruby -c ktexperts-cookbook/recipes/ktexperts-recipe.rb
8 Run chef-client Locally sudo chef-client -zr “recipe[<cookbook name>::<recipe name]” sudo chef-client -zr “recipe[ktexperts-cookbook::ktexperts-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[ktexperts-apache-cookbook::ktexperts-apache-recipe],recipe[ktexperts-cookbook::ktexperts-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[ktexperts-apache-cookbook],recipe[ktexperts-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 ktexperts-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[ktexperts-apache-cookbook::ktexperts-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/ktexperts-web.rb
name ‘ktexperts-web’
description “ktexperts web server role”
run_list “recipe[<cookbook name>::<recipe name]”,”recipe[<cookbook name>::<recipe name]”,”recipe[<cookbook name>]”
run_list “recipe[ktexperts-apache-cookbook::ktexperts-apache-recipe]”,”recipe[ktexperts-apache-cookbook::ktexperts-sample-recipe]”,”recipe[ktexperts-apache-cookbook]”
26 Upload role to chef server knife role from file roles/<role-name> knife role from file roles/ktexperts-web.rb
27 Attach node to Role knife node run_list set <node-name> “role[<role-name>]” knife node run_list set node1 “role[ktexperts-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 ktexperts-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 ktexperts-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 ‘ktexperts-file1’ do
content ‘Ktexperts 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
end

file ‘/var/www/html/index.html’ do
content “Ktexperts is a knowledge sharing platform”
action :create
end

service ‘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”ramesh”
append true
end
8 Create user,group and file user “Rammy”
group “ktexperts”
file “/rammyfile”
9 Execute Linux commands execute “run a script” do
command <<-EOH
mkdir /ktexpertsdir
touch /ktexpertsfile
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(/ktfile1 /ktfile2 /ktfile3).each do |p|
file p do
action :create
end
end
4 Create directories %w(/ktdir1 /ktdir2 /ktdir3).each do |path|
directory path do
action :create
end
end
5 Create groups %w(ktexperts1 ktexperts2 ktexperts3).each do |grp|
group grp do
action :create
end
end
6 Add users to single group group ‘ktexperts1’ do
action :modify
members [‘Ramesh’,’Ajay’,’Vinod’]
append true
end

Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates

KTEXPERTS is always active on below social media platforms.

 

Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment