Multiple Resources in Chef
Dear Readers,
In this article,we will see Multiple Resources in Chef.
we can create multiple resource at once and convert the code to infrastructure in nodes.
I am going to install multiple packages,create multiple file and directories,groups and users in 2 nodes by run single command (chef-client).
Steps to Follow
- Open your workstation Linux terminal through putty.
- Switch to root user.
- Go to chef-repo.
- Create a new cookbook “ktexperts-multi-resources” inside chef-repo/cookbooks.
- Create a new recipe “ktexperts-multi-resources” inside the cookbook “ktexperts-multi-resources”.
- Go to chef-repo directory.
- Modify role “ktexperts-web.rb”.
- Upload role to chef server.
- Verify run list of Node 1 “chef-Node-1”.
- Verify run list of Node 2 “chef-Node-2”.
- Open the recipe “ktexperts-multi-resources.rb” and write to create multiple resources.
- Upload cookbook “ktexperts-multi-resources.rb” to chef server.
- Go inside the Node 1 “Chef-Node-1” and Verify all resources.
- Go inside the Node 1 “Chef-Node-2” and Verify all resources
1. Open your workstation Linux terminal through putty
1 2 3 4 5 6 7 8 9 |
Using username "ec2-user". Authenticating with public key "imported-openssh-key" Last login: Fri Dec 27 09:02:29 2019 from 124.123.103.5 __| __|_ ) _| ( / Amazon Linux 2 AMI ___|\___|___| https://aws.amazon.com/amazon-linux-2/ |
2. Switch to root user
1 2 3 |
[ec2-user@ip-172-31-42-243 ~]$ sudo su [root@ip-172-31-42-243 ec2-user]# |
3. Go to chef-repo
1 2 3 |
[root@ip-172-31-42-243 ec2-user]# cd chef-repo/ [root@ip-172-31-42-243 chef-repo]# |
4. Create a new cookbook “ktexperts-multi-resources” inside chef-repo/cookbooks
Go to cookbooks
1 2 3 |
[root@ip-172-31-42-243 chef-repo]# cd cookbooks/ [root@ip-172-31-42-243 cookbooks]# |
Using below command to create cookbook
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@ip-172-31-42-243 cookbooks]# chef generate cookbook ktexperts-multi-resources Hyphens are discouraged in cookbook names as they may cause problems with custom resources. See https://docs.chef.io/ctl_chef.html#chef-generate-cookbook for more information. Generating cookbook ktexperts-multi-resources - Ensuring correct cookbook content Your cookbook is ready. Type `cd ktexperts-multi-resources` to enter it. There are several commands you can run to get started locally developing and testing your cookbook. Type `delivery local --help` to see a full list of local testing commands. 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 |
Verify the list of components of the cookbook “ktexperts-multi-resources”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[root@ip-172-31-42-243 cookbooks]# tree ktexperts-multi-resources/ ktexperts-multi-resources/ ├── CHANGELOG.md ├── chefignore ├── kitchen.yml ├── LICENSE ├── metadata.rb ├── Policyfile.rb ├── README.md ├── recipes │ └── default.rb ├── spec │ ├── spec_helper.rb │ └── unit │ └── recipes │ └── default_spec.rb └── test └── integration └── default └── default_test.rb 7 directories, 11 files |
5. Create a new recipe “ktexperts-multi-resources” inside the cookbook “ktexperts-multi-resources”
Go to cookbook “ktexperts-multi-resources”
1 2 |
[root@ip-172-31-42-243 cookbooks]# cd ktexperts-multi-resources/ [root@ip-172-31-42-243 ktexperts-multi-resources]# |
Run below command to create a new recipe
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@ip-172-31-42-243 ktexperts-multi-resources]# chef generate recipe ktexperts-multi-resources Recipe: code_generator::recipe * directory[/home/ec2-user/chef-repo/cookbooks/ktexperts-multi-resources/spec/unit/recipes] action create (up to date) * cookbook_file[/home/ec2-user/chef-repo/cookbooks/ktexperts-multi-resources/spec/spec_helper.rb] action create_if_missing (up to date) * template[/home/ec2-user/chef-repo/cookbooks/ktexperts-multi-resources/spec/unit/recipes/ktexperts-multi-resources_spec.rb] action create_if_missing - create new file /home/ec2-user/chef-repo/cookbooks/ktexperts-multi-resources/spec/unit/recipes/ktexperts-multi-resources_spec.rb - update content in file /home/ec2-user/chef-repo/cookbooks/ktexperts-multi-resources/spec/unit/recipes/ktexperts-multi-resources_spec.rb from none to c98167 (diff output suppressed by config) * directory[/home/ec2-user/chef-repo/cookbooks/ktexperts-multi-resources/test/integration/default] action create (up to date) * template[/home/ec2-user/chef-repo/cookbooks/ktexperts-multi-resources/test/integration/default/ktexperts-multi-resources_test.rb] action create_if_missing - create new file /home/ec2-user/chef-repo/cookbooks/ktexperts-multi-resources/test/integration/default/ktexperts-multi-resources_test.rb - update content in file /home/ec2-user/chef-repo/cookbooks/ktexperts-multi-resources/test/integration/default/ktexperts-multi-resources_test.rb from none to b6a16b (diff output suppressed by config) * template[/home/ec2-user/chef-repo/cookbooks/ktexperts-multi-resources/recipes/ktexperts-multi-resources.rb] action create - create new file /home/ec2-user/chef-repo/cookbooks/ktexperts-multi-resources/recipes/ktexperts-multi-resources.rb - update content in file /home/ec2-user/chef-repo/cookbooks/ktexperts-multi-resources/recipes/ktexperts-multi-resources.rb from none to 765fb2 (diff output suppressed by config) |
Verify recipes inside the cookbook
1 2 3 4 5 6 |
[root@ip-172-31-42-243 ktexperts-multi-resources]# tree recipes/ recipes/ ├── default.rb └── ktexperts-multi-resources.rb 0 directories, 2 files |
6. Go to chef-repo directory
1 2 |
[root@ip-172-31-42-243 ktexperts-multi-resources]# cd ../.. [root@ip-172-31-42-243 chef-repo]# |
7. Modify role “ktexperts-web.rb”
1 2 3 4 5 |
[root@ip-172-31-42-243 chef-repo]# vi roles/ktexperts-web.rb name 'ktexperts-web' description "ktexperts web server role" #run_list "recipe[ktexperts-apache-cookbook]","recipe[ktexperts-cookbook]" run_list "recipe[ktexperts-multi-resources::ktexperts-multi-resources]" |
8. Upload role to chef server
1 2 |
[root@ip-172-31-42-243 chef-repo]# knife role from file roles/ktexperts-web.rb Updated Role ktexperts-web |
To see the list of Nodes
[root@ip-172-31-42-243 chef-repo]# knife node list
1 2 |
chef-Node-1 chef-Node-2 |
9. Verify run list of Node 1 “chef-Node-1”
1 2 3 4 5 6 7 8 9 10 |
[root@ip-172-31-42-243 chef-repo]# knife node show chef-Node-1 Node Name: chef-Node-1 Environment: _default FQDN: ip-172-31-32-143.ap-south-1.compute.internal IP: 3.6.92.207 Run List: role[ktexperts-web] Roles: ktexperts-web Recipes: ktexperts-multi-resources::ktexperts-multi-resources Platform: amazon 2 Tags: |
10. Verify run list of Node 2 “chef-Node-2”
1 2 3 4 5 6 7 8 9 10 |
[root@ip-172-31-42-243 chef-repo]# knife node show chef-Node-2 Node Name: chef-Node-2 Environment: _default FQDN: ip-172-31-42-17.ap-south-1.compute.internal IP: 13.232.56.56 Run List: role[ktexperts-web] Roles: ktexperts-web Recipes: ktexperts-multi-resources::ktexperts-multi-resources Platform: amazon 2 Tags: |
11. Open the recipe “ktexperts-multi-resources.rb” and write to create multiple resources
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
[root@ip-172-31-42-243 chef-repo]# vi cookbooks/ktexperts-multi-resources/recipes/ktexperts-multi-resources.rb # # Cookbook:: ktexperts-multi-resources # Recipe:: ktexperts-multi-resources # # Copyright:: 2020, The Authors, All Rights Reserved. #### TO INSTALL MULTIPLE PACKAGES ### %w(tree mysql httpd).each do |p| package p do action :install end end #### TO CREATE MULTIPLE USERS ### %w(Ramesh Ajay Vinod).each do |p| user p do action :create end end #### TO CREATE MULTIPLE FILES ### %w(/ktfile1 /ktfile2 /ktfile3).each do |p| file p do action :create end end #### TO CREATE MULTIPLE DIRECTORIES ### %w(/ktdir1 /ktdir2 /ktdir3).each do |path| directory path do action :create end end #### TO CREATE MULTIPLE GROUPS ### %w(ktexperts1 ktexperts2 ktexperts3).each do |grp| group grp do action :create end end #### TO ADD MULTIPLE USERS TO SINGLE GROUP ### group 'ktexperts1' do action :modify members ['Ramesh','Ajay','Vinod'] append true end #### TO 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 |
12. Upload cookbook “ktexperts-multi-resources.rb” to chef server
1 2 3 |
[root@ip-172-31-42-243 chef-repo]# knife cookbook upload ktexperts-multi-resources Uploading ktexperts-multi-resources [0.1.0] Uploaded 1 cookbook. |
Verify cookbooks
To see the list of cookbooks
1 2 |
[root@ip-172-31-42-243 chef-repo]# knife cookbook list ktexperts-multi-resources 0.1.0 |
13. Go inside the Node 1 “Chef-Node-1” and Verify all resources
Connect Node 1 “Chef-Node-1” Linux Terminal through Putty
1 2 3 4 5 6 7 8 9 10 11 |
Using username "ec2-user". Authenticating with public key "imported-openssh-key" Last login: Tue Jan 7 07:54:41 2020 from 103.110.146.96 __| __|_ ) _| ( / Amazon Linux 2 AMI ___|\___|___| https://aws.amazon.com/amazon-linux-2/ 8 package(s) needed for security, out of 17 available Run "sudo yum update" to apply all updates. |
Switch to root user
1 2 |
[ec2-user@ip-172-31-46-59 ~]$ sudo su [root@ip-172-31-46-59 ec2-user]# |
Verify Packages
1 2 3 4 5 6 |
[root@ip-172-31-46-59 ec2-user]# which tree /bin/tree [root@ip-172-31-46-59 ec2-user]# which mysql /bin/mysql [root@ip-172-31-46-59 ec2-user]# which httpd /sbin/httpd |
Verify Users
1 2 3 4 |
[root@ip-172-31-46-59 ec2-user]# tail -3 /etc/passwd Ramesh:x:1001:1001::/home/Ramesh:/bin/bash Ajay:x:1002:1002::/home/Ajay:/bin/bash Vinod:x:1003:1003::/home/Vinod:/bin/bash |
Verify files and directories
1 2 |
[root@ip-172-31-46-59 ec2-user]# ls / bin boot dev etc home kt2file2 ktdir1 ktdir2 ktdir3 ktfile1 ktfile3 lib lib64 local media mnt opt proc root run sbin srv sys tmp usr var |
Verify groups
1 2 3 4 |
[root@ip-172-31-46-59 ec2-user]# tail -3 /etc/group ktexperts1:x:1004:Ramesh,Ajay,Vinod ktexperts2:x:1005: ktexperts3:x:1006: |
Verify package
1 2 |
[root@ip-172-31-46-59 ec2-user]# ls chef-workstation-0.13.35-1.el7.x86_64.rpm |
14. Go inside the Node 2 “Chef-Node-2” and Verify all resources
Connect Node 1 “Chef-Node-2” Linux Terminal through Putty
1 2 3 4 5 6 7 8 9 10 11 |
Using username "ec2-user". Authenticating with public key "imported-openssh-key" Last login: Tue Jan 7 07:54:41 2020 from 103.110.146.96 __| __|_ ) _| ( / Amazon Linux 2 AMI ___|\___|___| https://aws.amazon.com/amazon-linux-2/ 8 package(s) needed for security, out of 17 available Run "sudo yum update" to apply all updates. |
Switch to root user
1 2 |
[ec2-user@ip-172-31-4-151 ~]$ sudo su [root@ip-172-31-4-151 ec2-user]# |
Verify Packages
[root@ip-172-31-4-151 ec2-user]# which tree
1 2 3 4 5 |
/bin/tree [root@ip-172-31-4-151 ec2-user]# which mysql /bin/mysql [root@ip-172-31-4-151 ec2-user]# which httpd /sbin/httpd |
Verify Users
[root@ip-172-31-4-151 ec2-user]# tail -3 /etc/passwd
1 2 3 |
Ramesh:x:1001:1001::/home/Ramesh:/bin/bash Ajay:x:1002:1002::/home/Ajay:/bin/bash Vinod:x:1003:1003::/home/Vinod:/bin/bash |
Verify files and directories
1 2 3 |
[root@ip-172-31-4-151 ec2-user]# ls / bin boot dev etc home kt2file2 ktdir1 ktdir2 ktdir3 ktfile1 ktfile3 lib lib64 local media mnt opt proc root run sbin srv sys tmp usr var Verify groups |
[root@ip-172-31-4-151 ec2-user]# tail -3 /etc/group
1 2 3 |
ktexperts1:x:1004:Ramesh,Ajay,Vinod ktexperts2:x:1005: ktexperts3:x:1006: |
Verify package
1 2 |
[root@ip-172-31-4-151 ec2-user]# ls chef-workstation-0.13.35-1.el7.x86_64.rpm |
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