Chef Cookbook Pratices

Cookbook Recipes

Example1:- Creating a file

chef-workstation server:

[root@chef-workstation recipes]# cd /opt/chef/chef-starter/chef-repo/cookbooks/cookbook1/recipes

[root@chef-workstation recipes]# vi default.rb

[root@chef-workstation recipes]#


[root@node1 chef]#

# Cookbook:: cookbook1

# Recipe:: default

#

# Copyright:: 2022, The Authors, All Rights Reserved.

 

file "/opt/file_creation" do

 action :create

end

[root@chef-workstation recipes]# knife cookbook upload cookbook1

Uploading cookbook1      [0.1.0]

Uploaded 1 cookbook.

 

Node-1 :

[root@node1 chef]# chef-client
Chef Infra Client, version 17.10.3
Patents: https://www.chef.io/patents
Infra Phase starting
Resolving cookbooks for run list: ["cookbook1"]
Synchronizing cookbooks:
  - cookbook1 (0.1.0)
Installing cookbook gem dependencies:
Compiling cookbooks...
Loading Chef InSpec profile files:
Loading Chef InSpec input files:
Loading Chef InSpec waiver files:
Converging 1 resources
Recipe: cookbook1::default
  * file[/opt/file_creation] action create
    - create new file /opt/file_creation
    - restore selinux security context

Running handlers:
Running handlers complete
Infra Phase complete, 1/1 resources updated in 16 seconds
[root@node1 chef]#

Verifying file creation:


[root@node1 chef]# ls -rlt /opt
total 0
drwxr-xr-x.  3 root root  65 Apr 25 12:13 java
drwxr-xr-x.  5 root root 103 Apr 25 15:25 maven
drwxr-xr-x. 10 root root 283 Apr 25 20:32 tomcat
drwxr-xr-x.  5 root root 121 May  5 20:51 chef
-rw-r--r--.  1 root root   0 May  7 12:15 file_creation
[root@node1 chef]#

----------------------------End of Example1---------------------------------------

Example2: Creating a directory

Chef-workstation server:

[root@chef-workstation opt]# cd /opt/chef/chef-starter/chef-repo/cookbooks/cookbook1/recipes
[root@chef-workstation recipes]# vi default.rb
[root@chef-workstation recipes]# cat default.rb
#
# Cookbook:: cookbook1
# Recipe:: default
#
# Copyright:: 2022, The Authors, All Rights Reserved.

directory "/opt/chef_dir_creation" do
action :create
end
[root@chef-workstation recipes]#
[root@chef-workstation recipes]# knife cookbook upload cookbook1
Uploading cookbook1      [0.1.0]
Uploaded 1 cookbook.
[root@chef-workstation recipes]# 


Node1: 

[root@node1 chef]# chef-client
Chef Infra Client, version 17.10.3
Patents: https://www.chef.io/patents
Infra Phase starting
Resolving cookbooks for run list: ["cookbook1"]
Synchronizing cookbooks:
  - cookbook1 (0.1.0)
Installing cookbook gem dependencies:
Compiling cookbooks...
Loading Chef InSpec profile files:
Loading Chef InSpec input files:
Loading Chef InSpec waiver files:
Converging 1 resources
Recipe: cookbook1::default
  * directory[/opt/chef_dir_creation] action create
    - create new directory /opt/chef_dir_creation
    - restore selinux security context

Running handlers:
Running handlers complete
Infra Phase complete, 1/1 resources updated in 19 seconds
[root@node1 chef]# ls -lrt /opt
total 0
drwxr-xr-x.  3 root root  65 Apr 25 12:13 java
drwxr-xr-x.  5 root root 103 Apr 25 15:25 maven
drwxr-xr-x. 10 root root 283 Apr 25 20:32 tomcat
drwxr-xr-x.  5 root root 121 May  5 20:51 chef
-rw-r--r--.  1 root root   0 May  7 12:15 file_creation
drwxr-xr-x.  2 root root   6 May  7 12:34 chef_dir_creation
[root@node1 chef]#

----------------------------End of Example2---------------------------------------


Example3: Creating a File with content

Chef-workstation server:

[root@chef-workstation recipes]#cd /opt/chef/chef-starter/chef-repo/cookbooks/cookbook1/recipes
[root@chef-workstation recipes]# vi default.rb
[root@chef-workstation recipes]# cat default.rb
#
# Cookbook:: cookbook1
# Recipe:: default
#
# Copyright:: 2022, The Authors, All Rights Reserved.

file "/opt/chef_dir_creation/chef_file" do
action :create
content "add the content to file chef_file"
end
[root@chef-workstation recipes]# knife cookbook upload cookbook1
Uploading cookbook1      [0.1.0]
Uploaded 1 cookbook.
[root@chef-workstation recipes]#


Node1: 

[root@node1 chef]# chef-client
Chef Infra Client, version 17.10.3
Patents: https://www.chef.io/patents
Infra Phase starting
Resolving cookbooks for run list: ["cookbook1"]
Synchronizing cookbooks:
  - cookbook1 (0.1.0)
Installing cookbook gem dependencies:
Compiling cookbooks...
Loading Chef InSpec profile files:
Loading Chef InSpec input files:
Loading Chef InSpec waiver files:
Converging 1 resources
Recipe: cookbook1::default
  * file[/opt/chef_dir_creation/chef_file] action create
    - create new file /opt/chef_dir_creation/chef_file
    - update content in file /opt/chef_dir_creation/chef_file from none to 3495e0
    --- /opt/chef_dir_creation/chef_file        2022-05-07 12:51:25.978217591 +0530
    +++ /opt/chef_dir_creation/.chef-chef_file20220507-4089-gkncyu      2022-05-07 12:51:25.976217591 +0530
    @@ -1 +1,2 @@
    +add the content to file chef_file
    - restore selinux security context

Running handlers:
Running handlers complete
Infra Phase complete, 1/1 resources updated in 15 seconds
[root@node1 chef]# cd /opt/chef_dir_creation/
[root@node1 chef_dir_creation]# ls -lrt
total 4
-rw-r--r--. 1 root root 33 May  7 12:51 chef_file
[root@node1 chef_dir_creation]# cat chef_file
add the content to file chef_file
[root@node1 chef_dir_creation]#

----------------------------End of Example3---------------------------------------

Example4: Deleting a file with Automatic chef-client run for every 2 mins on Node-1

Node1: 

"chef-client" - command save in chef.sh

[root@node1 chef_dir_creation]# vi chef.sh
[root@node1 chef_dir_creation]# chmod +x chef.sh
[root@node1 chef_dir_creation]# cat chef.sh
chef-client
[root@node1 chef_dir_creation]#

running this file using crontab , script " /opt/chef_dir_creation/chef_file.sh" will for every 2 mins

[root@node1 chef_dir_creation]# crontab -e
crontab: installing new crontab
[root@node1 chef_dir_creation]# crontab -l
*/2 * * * * /opt/chef_dir_creation/chef_file.sh
[root@node1 chef_dir_creation]# 

Chef-workstation server:

[root@chef-workstation recipes]# vi default.rb
[root@chef-workstation recipes]#
[root@chef-workstation recipes]# cat default.rb
#
# Cookbook:: cookbook1
# Recipe:: default
#
# Copyright:: 2022, The Authors, All Rights Reserved.

file "/opt/chef_dir_creation/chef_file" do
action :delete
end
[root@chef-workstation recipes]# knife cookbook upload cookbook1
Uploading cookbook1      [0.1.0]
Uploaded 1 cookbook.
[root@chef-workstation recipes]#

Verify "chef_file" will deleted automatically on Node-1: 

[root@node1 chef_dir_creation]# date
Sat May  7 13:27:01 IST 2022
[root@node1 chef_dir_creation]# ls -ltr
total 8
-rw-r--r--. 1 root root 33 May  7 12:51 chef_file
-rwxr-xr-x. 1 root root 12 May  7 13:22 chef.sh
[root@node1 chef_dir_creation]# date
Sat May  7 13:27:21 IST 2022
[root@node1 chef_dir_creation]# date
Sat May  7 13:28:26 IST 2022
You have new mail in /var/spool/mail/root
[root@node1 chef_dir_creation]# ls -ltr
total 4
-rwxr-xr-x. 1 root root 12 May  7 13:22 chef.sh

----------------------------End of Example4---------------------------------------

Example5: Adding Multiple cookbooks to Node-1.


[root@chef-workstation recipes]# knife node list
Node-1
Node-2
[root@chef-workstation recipes]# knife node show Node-1
Node Name:   Node-1
Environment: _default
FQDN:        node1
IP:          192.168.2.133
Run List:
Roles:
Recipes:
Platform:    centos 7.9.2009
Tags:

[root@chef-workstation recipes]# knife node run_list add Node-1 "recipe[cookbook1]"
 Node-1:
  run_list: recipe[cookbook1]
[root@chef-workstation recipes]#  knife node show Node-1
Node Name:   Node-1
Environment: _default
FQDN:        node1
IP:          192.168.2.133
Run List:    recipe[cookbook1]
Roles:
Recipes:
Platform:    centos 7.9.2009
Tags:

[root@chef-workstation recipes]# knife node run_list add Node-1 "recipe[cookbook2]","recipe[cookbook3]"
Node-1:
  run_list:
    recipe[cookbook1]
    recipe[cookbook2]
    recipe[cookbook3]

[root@chef-workstation recipes]# knife node show Node-1
Node Name:   Node-1
Environment: _default
FQDN:        node1
IP:          192.168.2.133
Run List:    recipe[cookbook1], recipe[cookbook2], recipe[cookbook3]
Roles:
Recipes:
Platform:    centos 7.9.2009
Tags:
[root@chef-workstation recipes]#

Removing multiple cookbooks from Node-1:

[root@chef-workstation recipes]# knife node run_list remove Node-1 "recipe[cookbook1]","recipe[cookbook2]","recipe[cookbook3]"
Node-1:
  run_list:

[root@chef-workstation recipes]# knife node show Node-1
Node Name:   Node-1
Environment: _default
FQDN:        node1
IP:          192.168.2.133
Run List:
Roles:
Recipes:
Platform:    centos 7.9.2009
Tags:
[root@chef-workstation recipes]#

----------------------------End of Example5---------------------------------------

Example6: Adding Multiple cookbooks to Multiple Nodes.


[root@chef-workstation recipes]# knife node list
Node-1
Node-2
[root@chef-workstation recipes]# vi node.sh
[root@chef-workstation recipes]# cat node.sh
for i in Node-1 Node-2
do
knife node run_list add $i "recipe[cookbook1]","recipe[cookbook2]"
done
[root@chef-workstation recipes]# knife node show Node-1
Node Name:   Node-1
Environment: _default
FQDN:        node1
IP:          192.168.2.133
Run List:
Roles:
Recipes:
Platform:    centos 7.9.2009
Tags:
[root@chef-workstation recipes]# knife node show Node-2
Node Name:   Node-2
Environment: _default
FQDN:        node2
IP:          192.168.2.128
Run List:    recipe[cookbook1], recipe[cookbook2]
Roles:
Recipes:
Platform:    centos 7.9.2009
Tags:
[root@chef-workstation recipes]# ./node.sh
Node-1:
  run_list:
    recipe[cookbook1]
    recipe[cookbook2]
Node-2:
  run_list:
    recipe[cookbook1]
    recipe[cookbook2]
[root@chef-workstation recipes]# knife node show Node-1
Node Name:   Node-1
Environment: _default
FQDN:        node1
IP:          192.168.2.133
Run List:    recipe[cookbook1], recipe[cookbook2]
Roles:
Recipes:
Platform:    centos 7.9.2009
Tags:
[root@chef-workstation recipes]# knife node show Node-2
Node Name:   Node-2
Environment: _default
FQDN:        node2
IP:          192.168.2.128
Run List:    recipe[cookbook1], recipe[cookbook2]
Roles:
Recipes:
Platform:    centos 7.9.2009
Tags:
[root@chef-workstation recipes]#

Adding cookbook3 to all Nodes in the server.

[root@chef-workstation recipes]# knife node list
Node-1
Node-2

[root@chef-workstation recipes]# vi all_node.sh
[root@chef-workstation recipes]# chmod +x all_node.sh
[root@chef-workstation recipes]# cat all_node.sh
for i in $(knife node list)
do
knife node run_list add $i "recipe[cookbook3]"
done
[root@chef-workstation recipes]# ./all_node.sh
Node-1:
  run_list:
    recipe[cookbook1]
    recipe[cookbook2]
    recipe[cookbook3]
Node-2:
  run_list:
    recipe[cookbook1]
    recipe[cookbook2]
    recipe[cookbook3]
[root@chef-workstation recipes]#

----------------------------End of Example6---------------------------------------

Example7: Installing Package on Node.


package "git" do
action :remove
end


[root@chef-workstation cookbooks]# chef generate cookbook git
Generating cookbook git
- Ensuring correct cookbook content
- Committing cookbook files to git

Your cookbook is ready. Type `cd git` to enter it.

There are several commands you can run to get started locally developing and testing your cookbook.

Why not start by writing an InSpec test? Tests for the default recipe are stored at:

test/integration/default/default_test.rb

If you'd prefer to dive right in, the default recipe can be found at:

recipes/default.rb

[root@chef-workstation cookbooks]# ls
chefignore  cookbook1  cookbook2  git  starter
[root@chef-workstation cookbooks]# cd git/recipes
[root@chef-workstation recipes]# vi default.rb
[root@chef-workstation recipes]# cat default.rb
#
# Cookbook:: git
# Recipe:: default
#
# Copyright:: 2022, The Authors, All Rights Reserved.

package "git" do
action :install
end

[root@chef-workstation recipes]#
[root@chef-workstation recipes]# knife cookbook upload git
Uploading git            [0.1.0]
Uploaded 1 cookbook.
[root@chef-workstation recipes]# knife node run_list add Node-1 "recipe[git]"
Node-1:
  run_list:
    recipe[cookbook1]
    recipe[cookbook2]
    recipe[cookbook3]
    recipe[git]
[root@chef-workstation recipes]# 

Node1: 

[root@node1 chef]# chef-client
Chef Infra Client, version 17.10.3
Patents: https://www.chef.io/patents
Infra Phase starting
Resolving cookbooks for run list: ["git"]
Synchronizing cookbooks:
  - git (0.1.0)
Installing cookbook gem dependencies:
Compiling cookbooks...
Loading Chef InSpec profile files:
Loading Chef InSpec input files:
Loading Chef InSpec waiver files:
Converging 1 resources
Recipe: git::default
  * yum_package[git] action install
    - install version 0:1.8.3.1-23.el7_8.x86_64 of package git

Running handlers:
Running handlers complete
Infra Phase complete, 1/1 resources updated in 37 seconds
You have new mail in /var/spool/mail/root
[root@node1 chef]#

Uninstallation rpm file on Node.

[root@chef-workstation recipes]# vi default.rb
[root@chef-workstation recipes]# cat default.rb
#
# Cookbook:: git
# Recipe:: default
#
# Copyright:: 2022, The Authors, All Rights Reserved.

package "git" do
action :remove
end
[root@chef-workstation recipes]#
[root@chef-workstation recipes]#  knife cookbook upload git
Uploading git            [0.1.0]
Uploaded 1 cookbook.
[root@chef-workstation recipes]#

Node1: 

[root@node1 chef]# chef-client
Chef Infra Client, version 17.10.3
Patents: https://www.chef.io/patents
Infra Phase starting
Resolving cookbooks for run list: ["git"]
Synchronizing cookbooks:
  - git (0.1.0)
Installing cookbook gem dependencies:
Compiling cookbooks...
Loading Chef InSpec profile files:
Loading Chef InSpec input files:
Loading Chef InSpec waiver files:
Converging 1 resources
Recipe: git::default
  * yum_package[git] action remove
    - remove package git

Running handlers:
Running handlers complete
Infra Phase complete, 1/1 resources updated in 18 seconds
[root@node1 chef]#

----------------------------End of Example7---------------------------------------

Example8: Installing Package httpd, server enable, service start, on Node.

[root@chef-workstation cookbooks]# chef generate cookbook httpd
Generating cookbook httpd
- Ensuring correct cookbook content
- Committing cookbook files to git

Your cookbook is ready. Type `cd httpd` to enter it.

There are several commands you can run to get started locally developing and testing your cookbook.

Why not start by writing an InSpec test? Tests for the default recipe are stored at:

test/integration/default/default_test.rb

If you'd prefer to dive right in, the default recipe can be found at:

recipes/default.rb

[root@chef-workstation cookbooks]# cd httpd/recipes/
[root@chef-workstation recipes]# vi default.rb
[root@chef-workstation recipes]# cat default.rb
#
# Cookbook:: httpd
# Recipe:: default
#
# Copyright:: 2022, The Authors, All Rights Reserved.

package "httpd" do
action :install
end

service "httpd" do
action :start
end

service "httpd" do
action :enable
end

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

file "/sai/chef1.html" do
action :create
content "Hi Friends, How are you?"
end
[root@chef-workstation recipes]#
[root@chef-workstation recipes]#
[root@chef-workstation recipes]#  knife cookbook upload httpd
Uploading httpd          [0.1.0]
Uploaded 1 cookbook.
[root@chef-workstation recipes]# 
[root@chef-workstation recipes]# knife node run_list add Node-1 "recipe[httpd]"
Node-1:
  run_list:
    recipe[httpd]
[root@chef-workstation recipes]# 
[root@chef-workstation recipes]# knife node show Node-1
Node Name:   Node-1
Environment: _default
FQDN:        node1
IP:          192.168.2.133
Run List:    recipe[httpd]
Roles:
Recipes:     git, git::default
Platform:    centos 7.9.2009
Tags:
[root@chef-workstation recipes]#

Node1: 



----------------------------End of Example8---------------------------------------

Example9: Installing RPM file on Node-1.

[root@chef-workstation cookbooks]# chef generate cookbook chefws
Generating cookbook chefws
- Ensuring correct cookbook content
- Committing cookbook files to git

Your cookbook is ready. Type `cd chefws` to enter it.

There are several commands you can run to get started locally developing and testing your cookbook.

Why not start by writing an InSpec test? Tests for the default recipe are stored at:

test/integration/default/default_test.rb

If you'd prefer to dive right in, the default recipe can be found at:

recipes/default.rb


[root@chef-workstation cookbooks]# cd chefws/recipes
[root@chef-workstation recipes]# vi default.rb
[root@chef-workstation recipes]#
[root@chef-workstation recipes]# cat default.rb
#
# Cookbook:: chefws
# Recipe:: default
#
# Copyright:: 2022, The Authors, All Rights Reserved.

package "/sai/chef-workstation-22.4.861-1.el7.x86_64.rpm" do
action :install
end
[root@chef-workstation recipes]#
[root@chef-workstation recipes]# knife node run_list add Node-1 "recipe[chefws]"
Node-1:
  run_list:
    recipe[chefws]
[root@chef-workstation recipes]# 
[root@chef-workstation recipes]# knife node show Node-1
Node Name:   Node-1
Environment: _default
FQDN:        node1
IP:          192.168.2.133
Run List:    recipe[chefws]
Roles:
Recipes:     httpd, httpd::default
Platform:    centos 7.9.2009
Tags:
[root@chef-workstation recipes]#
[root@chef-workstation recipes]# knife cookbook upload chefws
Uploading chefws         [0.1.0]
Uploaded 1 cookbook.
[root@chef-workstation recipes]# 


Node1: 





----------------------------End of Example9---------------------------------------

Example10: Creating users & group using databag , undefault recipe.


[root@chef-workstation chef-repo]# ls  -lrt
total 4
-rw-r--r--.  1 root root 2284 May  5 20:30 README.md
drwxr-xr-x.  2 root root   24 May  6 09:21 roles
drwxr-xr-x. 10 root root  140 May  9 17:40 cookbooks
[root@chef-workstation chef-repo]#
[root@chef-workstation chef-repo]# mkdir data_bags
[root@chef-workstation chef-repo]# cd data_bags/
[root@chef-workstation data_bags]# mkdir users groups
[root@chef-workstation chef-repo]# cd ../..
[root@chef-workstation chef-repo]# knife data bag create users
Created data_bag[users]
[root@chef-workstation chef-repo]#
[root@chef-workstation chef-repo]# knife data bag create groups
Created data_bag[groups]
[root@chef-workstation chef-repo]#

[root@chef-workstation chef-repo]# cat data_bags/users/sai.json
{
"id":"sai",
"comment":"Test-user",
"uid":2002,
"gid":0,
"home":"/home/sai",
"shell":"/bin/bash"
}

[root@chef-workstation chef-repo]# cat data_bags/users/advaith.json
{
"id":"advaith",
"comment":"group-user",
"uid":2003,
"gid":0,
"home":"/home/advaith",
"shell":"/bin/bash"
}

[root@chef-workstation chef-repo]# cat data_bags/groups/group1.json
{
"id":"group1",
"gid":2005,
"members":["sai","advaith"]
}

[root@chef-workstation chef-repo]#

[root@chef-workstation chef-repo]# knife data bag from file users sai.json advaith.json
Updated data_bag_item[users::sai]
Updated data_bag_item[users::advaith]
[root@chef-workstation chef-repo]#
[root@chef-workstation chef-repo]# knife data bag from file groups group1.json
Updated data_bag_item[groups::group1]
[root@chef-workstation chef-repo]#


[root@chef-workstation cookbooks]# chef generate cookbook users_group
Generating cookbook users_group
- Ensuring correct cookbook content
- Committing cookbook files to git

Your cookbook is ready. Type `cd users_group` to enter it.

There are several commands you can run to get started locally developing and testing your cookbook.

Why not start by writing an InSpec test? Tests for the default recipe are stored at:

test/integration/default/default_test.rb

If you'd prefer to dive right in, the default recipe can be found at:

recipes/default.rb

[root@chef-workstation cookbooks]# cd users_group/recipes/

[root@chef-workstation recipes]# cat default.rb
#
# Cookbook:: users_group
# Recipe:: default
#
# Copyright:: 2022, The Authors, All Rights Reserved.

search("users","*:*").each do |user_data|
user user_data["id"] do
comment user_data["comment"]
uid user_data["uid"]
gid user_data["gid"]
home user_data["home"]
shell user_data["shell"]
end
end

Non-Default recipe:

[root@chef-workstation recipes]# cat group.rb
search("groups","*:*").each do |group_data|
group group_data["id"] do
gid group_data["gid"]
members group_data["members"]
end
end
[root@chef-workstation recipes]#

[root@chef-workstation recipes]# knife node run_list add Node-1 "recipe[users_group]"
Node-1:
  run_list:
    recipe[users]
    recipe[users_group]
[root@chef-workstation recipes]# 

Note: group non-default recipe is note added with the above command

[root@chef-workstation recipes]# knife cookbook upload users_group
Uploading users_group    [0.1.0]
Uploaded 1 cookbook.
[root@chef-workstation recipes]#

Node1: 



In this above result only user are created but not group, because it is not added to run list with the above command 

To add non-default recipes , please use the below 2 methods:

Method1: 

[root@chef-workstation recipes]# knife node run_list add Node-1 "recipe[users_group]","recipe[users_group::group]"
Node-1:
  run_list:
    recipe[users_group]
    recipe[users_group::group]
[root@chef-workstation recipes]# knife node show Node-1
Node Name:   Node-1
Environment: _default
FQDN:        node1
IP:          192.168.2.133
Run List:    recipe[users_group], recipe[users_group::group]
Roles:
Recipes:     users_group, users_group::default
Platform:    centos 7.9.2009
Tags:
[root@chef-workstation recipes]# knife cookbook upload users_group
Uploading users_group    [0.1.0]
Uploaded 1 cookbook.
[root@chef-workstation recipes]#

Method2: 

[root@chef-workstation recipes]# cat default.rb
#
# Cookbook:: users_group
# Recipe:: default
#
# Copyright:: 2022, The Authors, All Rights Reserved.

search("users","*:*").each do |user_data|
user user_data["id"] do
comment user_data["comment"]
uid user_data["uid"]
gid user_data["gid"]
home user_data["home"]
shell user_data["shell"]
end
end
include_recipe "users_group::group"

Node1: 

Now group is also created 




----------------------------End of Example10---------------------------------------






Comments

Popular posts from this blog

To change the data directory location for PostgreSQL after installation

AWR

GIT