Roles in Chef
Dear Readers ,
In this article,we will see different types of Roles in Chef.
Roles
Roles in Chef are a logical way of grouping nodes.
Typical cases are to have roles for web servers, database servers, and so on.
One can set custom run list for all the nodes and override attribute value within roles.
How It Works
Define a role in a Ruby file inside the roles folder of Chef repository.
A role consists of a name and a description attribute.
A role consists of role-specific run list and role-specific attribute settings.
Every node that has a role in its run list will have the role’s run list exacted into its own.
All the recipes in the role’s run list will be executed on the node.
The role will be uploaded to Chef server using the knife role from file command.
The role will be added to the node run list.
Running Chef client on a node having the role in its run list will execute all the recipes listed in the role.
Implementation Steps
- Lauch 2 Nodes “Chef-Node-1” and “Chef-Node-2”.
- Bootstrap a 2 Nodes.
- Create a Role “ktexperts-web.rb”.
- Upload role to chef server.
- Attache 2 Nodes to Role.
- Upload cookbook “ktexperts-apache-cookbook” to chef server.
- Verify the Web content of 2 Nodes.
- Modify the recipe”ktexperts-apache-recipe.rb”.
- Upload cookbook “ktexperts-apache-cookbook” to chef server.
- Verify the Web content of 2 Nodes.
- Run all recipes inside the cookbook “ktexperts-apache-cookbook”.
- Run all recipes from all cookbooks.
1. Launch 2 New Nodes “Chef-Node-1” and “Chef-Node-2”
Check Availability Zone of your workstation
we can see the Availability Zone “ap-south-1a” of your workstation.
Note
Workstation is in ap-south-1a AZ.
we create 2 nodes in the same Availability Zone “ap-south-1a”.
we need to open SSH and HTTP ports.
we need to specify some commands in the user data to automate chef-client while launching instance.
Note
For every one minute,crontab will run the chef-client automatically.
Create 2 Nodes “Chef-Node-1” and “Chef-Node-2”
Launch Linux EC2 Instance
Check below link to Launch Linux EC2 Instance.
We can see the 2 nodes “Chef-Node-1” and “Chef-Node-” which was created earlier.
2. Bootstrap a 2 Nodes
Bootstrap a Node 1 “Chef-Node-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/ |
Switch to root user
1 2 3 |
[ec2-user@ip-172-31-42-243 ~]$ sudo su [root@ip-172-31-42-243 ec2-user]# |
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]# |
Run bootstrap command to bootstrap a node 1 “Chef-Node-1”
we need specify private IP key file of chef-node-1 in bootstrap command.
Copy private IP of chef-node-1
Specify username,private IP(chef-node-1) and pem key “chef.pem” (chef-node-1) in the bootstrap command.
Note
Two actions will be done while bootstrapping.
Adding node to chef server.
Installing chef package.
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 60 61 62 63 64 65 66 67 68 69 70 71 |
[root@ip-172-31-42-243 chef-repo]# knife bootstrap 172.31.32.143 --ssh-user ec2-user --sudo -i chef.pem -N chef-Node-1 --ssh-user: This flag is deprecated. Use -U/--connection-user instead. Connecting to 172.31.32.143 The authenticity of host '172.31.32.143 ()' can't be established. fingerprint is SHA256:xfVYnS8TUeTBrq0HahZZMrbmMdlFumqHbZ46vIs/ong. Are you sure you want to continue connecting ? (Y/N) Y Connecting to 172.31.32.143 Creating new client for chef-Node-1 Creating new node for chef-Node-1 Bootstrapping 172.31.32.143 [172.31.32.143] -----> Installing Chef Omnibus (stable/15) downloading https://omnitruck.chef.io/chef/install.sh to file /tmp/install.sh.12553/install.sh trying wget... [172.31.32.143] el 7 x86_64 Getting information for chef stable 15 for el... downloading https://omnitruck.chef.io/stable/chef/metadata?v=15&p=el&pv=7&m=x86_64 to file /tmp/install.sh.12558/metadata.txt [172.31.32.143] trying wget... [172.31.32.143] sha1 27c8caeb7fcbab3642d3a2c320d2f880a6cf8541 sha256 9cb48fed74779b261a03c34178e375bbbf27860db3641ef5b392f7b1e439414e url https://packages.chef.io/files/stable/chef/15.6.10/el/7/chef-15.6.10-1.el7.x86_64.rpm version 15.6.10 [172.31.32.143] [172.31.32.143] downloaded metadata file looks valid... [172.31.32.143] downloading https://packages.chef.io/files/stable/chef/15.6.10/el/7/chef-15.6.10-1.el7.x86_64.rpm to file /tmp/install.sh.12558/chef-15.6.10-1.el7.x86_64.rpm [172.31.32.143] trying wget... [172.31.32.143] Comparing checksum with sha256sum... [172.31.32.143] Installing chef 15 installing with rpm... [172.31.32.143] warning: [172.31.32.143] /tmp/install.sh.12558/chef-15.6.10-1.el7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY [172.31.32.143] Preparing... [172.31.32.143] ######################################## [172.31.32.143] [172.31.32.143] Updating / installing... chef-15.6.10-1.el7 [172.31.32.143] # [172.31.32.143] # [172.31.32.143] # [172.31.32.143] # [172.31.32.143] # [172.31.32.143] # [172.31.32.143] [172.31.32.143] Thank you for installing Chef Infra Client! For help getting started visit https://learn.chef.io [172.31.32.143] Starting the first Chef Infra Client Client run... [172.31.32.143] +---------------------------------------------+ ✔ 2 product licenses accepted. +---------------------------------------------+ [172.31.32.143] Starting Chef Infra Client, version 15.6.10 [172.31.32.143] [172.31.32.143] resolving cookbooks for run list: [] [172.31.32.143] [172.31.32.143] Synchronizing Cookbooks: [172.31.32.143] [172.31.32.143] Installing Cookbook Gems: Compiling Cookbooks... [2020-01-02T17:40:28+00:00] WARN: Node chef-Node-1 has an empty run list. [172.31.32.143] Converging 0 resources [172.31.32.143] [172.31.32.143] [172.31.32.143] [172.31.32.143] Running handlers: [172.31.32.143] [172.31.32.143] Running handlers complete [172.31.32.143] Chef Infra Client finished, 0/0 resources updated in 06 seconds [172.31.32.143] |
Bootstrap a Node 2 “Chef-Node-2”
Run bootstrap command to bootstrap a node 1 “Chef-Node-1”
we need specify private IP key file of chef-node-2 in bootstrap command.
Copy private IP of chef-node-2
Specify username,private IP(chef-node-2) and pem key “chef.pem” (chef-node-2) in the bootstrap command.
Note
Two actions will be done while bootstrapping.
Adding node to chef server.
Installing chef package.
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
[root@ip-172-31-42-243 chef-repo]# knife bootstrap 172.31.42.17 --ssh-user ec2-user --sudo -i chef.pem -N chef-Node-2 --ssh-user: This flag is deprecated. Use -U/--connection-user instead. Connecting to 172.31.42.17 The authenticity of host '172.31.42.17 ()' can't be established. fingerprint is SHA256:MWrNEr0gQisFAlCrCooHtOYrpB37IFVrgRixmaFTybw. Are you sure you want to continue connecting ? (Y/N) Y Connecting to 172.31.42.17 Creating new client for chef-Node-2 Creating new node for chef-Node-2 Bootstrapping 172.31.42.17 [172.31.42.17] -----> Installing Chef Omnibus (stable/15) downloading https://omnitruck.chef.io/chef/install.sh to file /tmp/install.sh.12623/install.sh trying wget... [172.31.42.17] el 7 x86_64 Getting information for chef stable 15 for el... downloading https://omnitruck.chef.io/stable/chef/metadata?v=15&p=el&pv=7&m=x86_64 to file /tmp/install.sh.12628/metadata.txt [172.31.42.17] trying wget... [172.31.42.17] sha1 27c8caeb7fcbab3642d3a2c320d2f880a6cf8541 sha256 9cb48fed74779b261a03c34178e375bbbf27860db3641ef5b392f7b1e439414e url https://packages.chef.io/files/stable/chef/15.6.10/el/7/chef-15.6.10-1.el7.x86_64.rpm version 15.6.10 [172.31.42.17] [172.31.42.17] downloaded metadata file looks valid... [172.31.42.17] downloading https://packages.chef.io/files/stable/chef/15.6.10/el/7/chef-15.6.10-1.el7.x86_64.rpm to file /tmp/install.sh.12628/chef-15.6.10-1.el7.x86_64.rpm [172.31.42.17] trying wget... [172.31.42.17] Comparing checksum with sha256sum... [172.31.42.17] Installing chef 15 installing with rpm... [172.31.42.17] warning: [172.31.42.17] /tmp/install.sh.12628/chef-15.6.10-1.el7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY [172.31.42.17] Preparing... [172.31.42.17] ######################################## [172.31.42.17] [172.31.42.17] Updating / installing... chef-15.6.10-1.el7 [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] # [172.31.42.17] [172.31.42.17] Thank you for installing Chef Infra Client! For help getting started visit https://learn.chef.io [172.31.42.17] Starting the first Chef Infra Client Client run... [172.31.42.17] +---------------------------------------------+ ✔ 2 product licenses accepted. +---------------------------------------------+ [172.31.42.17] Starting Chef Infra Client, version 15.6.10 [172.31.42.17] [172.31.42.17] resolving cookbooks for run list: [] [172.31.42.17] [172.31.42.17] Synchronizing Cookbooks: [172.31.42.17] [172.31.42.17] Installing Cookbook Gems: Compiling Cookbooks... [2020-01-02T17:51:26+00:00] WARN: Node chef-Node-2 has an empty run list. [172.31.42.17] Converging 0 resources [172.31.42.17] [172.31.42.17] [172.31.42.17] [172.31.42.17] Running handlers: [172.31.42.17] [172.31.42.17] Running handlers complete [172.31.42.17] Chef Infra Client finished, 0/0 resources updated in 11 seconds [172.31.42.17] |
Verify bootstrap nodes
1 2 3 |
[root@ip-172-31-42-243 chef-repo]# knife node list chef-Node-1 chef-Node-2 |
Verify 2 Nodes in graphically
3. Create a Role “ktexperts-web.rb”
1 2 3 4 |
[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::ktexperts-apache-recipe]" |
Note
:wq! — to quit.
4. 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 |
Verify Roles
1 2 |
[root@ip-172-31-42-243 chef-repo]# knife role list ktexperts-web |
5. Attache 2 Nodes to Role
Attache Node 1 “Chef-Node-1” to Role
1 2 3 |
[root@ip-172-31-42-243 chef-repo]# knife node run_list set chef-Node-1 "role[ktexperts-web]" chef-Node-1: run_list: role[ktexperts-web.rb] |
Verify Run List of Node 1 “chef-Node-1”
1 2 3 4 5 6 7 8 9 10 11 |
run_list: role[ktexperts-web.rb] [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: 13.234.113.158 Run List: role[ktexperts-web] Roles: ktexperts-web Recipes: ktexperts-apache-cookbook::ktexperts-apache-recipe Platform: amazon 2 Tags: |
Attache Node 2 “Chef-Node-2” to Role
1 2 3 |
[root@ip-172-31-42-243 chef-repo]# knife node run_list set chef-Node-2 "role[ktexperts-web]" chef-Node-2: run_list: role[ktexperts-web] |
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.235.99.106 Run List: role[ktexperts-web] Roles: ktexperts-web Recipes: ktexperts-apache-cookbook::ktexperts-apache-recipe Platform: amazon 2 Tags: |
6. Upload cookbook “ktexperts-apache-cookbook” to chef server
1 2 3 |
[root@ip-172-31-42-243 chef-repo]# knife cookbook upload ktexperts-apache-cookbook Uploading ktexperts-apache-cookbook [0.1.0] Uploaded 1 cookbook. |
Verify cookbook
1 2 |
[root@ip-172-31-42-243 chef-repo]# knife cookbook list ktexperts-apache-cookbook 0.1.0 |
Verify cookbook graphically
7. Verify the Web content of 2 Nodes
Verify Node 1 “Chef-Node-1”
Copy the IPV4 Public IP of node 1″Chef-Node-1″
Search IPV4 Public IP in browser
We can able to see the content of node 1 “Chef-Node-1”
Verify Node 2 “Chef-Node-2”
Copy the IPV4 Public IP of node 2″Chef-Node-2″
Search IPV4 Public IP in browser
We can able to see the content of node 2 “Chef-Node-2”
8. Modify the recipe”ktexperts-apache-recipe.rb”
Open existing recipe “ktexperts-apache-recipe.rb” and write script to change the content of apache web server
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 chef-repo]# vi cookbooks/ktexperts-apache-cookbook/recipes/ktexperts-apache-recipe.rb # # Cookbook:: ktexperts-apache-cookbook # Recipe:: ktexperts-apache-recipe # # Copyright:: 2019, The Authors, All Rights Reserved. package 'httpd' do action :install end file '/var/www/html/index.html' do content "wwww.ktexperts.com && Knowledge sharing platform && Learn new things" action :create end service 'httpd' do action [ :enable, :start ] end |
Note
:wq! — to quit.
9. Upload cookbook “ktexperts-apache-cookbook” to chef server
1 2 3 |
[root@ip-172-31-42-243 chef-repo]# knife cookbook upload ktexperts-apache-cookbook Uploading ktexperts-apache-cookbook [0.1.0] Uploaded 1 cookbook. |
10. Verify the Web content of 2 Nodes
Verify Node 1 “Chef-Node-1”
Copy the IPV4 Public IP of node 1″Chef-Node-1″
Search IPV4 Public IP in browser
We can able to see the content of node 1 “Chef-Node-1”
Verify Node 2 “Chef-Node-2”
Copy the IPV4 Public IP of node 2″Chef-Node-2″
Search IPV4 Public IP in browser
We can able to see the content of node 2 “Chef-Node-2”
11. Run all recipes inside the cookbook “ktexperts-apache-cookbook”
To see the list of recipes in cookbook “ktexperts-apache-cookbook”
We can 3 recipes are being presented in the cookbook.
1 2 3 4 5 |
[root@ip-172-31-42-243 chef-repo]# tree cookbooks/ktexperts-apache-cookbook/recipes/ cookbooks/ktexperts-apache-cookbook/recipes/ ├── default.rb ├── ktexperts-apache-recipe.rb └── ktexperts-sample-recipe.rb |
Note
If we want to run multiple recipes from same cookbook,we have to add remaining recipes inside the default recipe.
Add ktexperts-apache-recipe.rb and ktexperts-sample-recipe.rb inside the default.rb
1 2 3 4 5 6 7 8 9 |
[root@ip-172-31-42-243 chef-repo]# vi cookbooks/ktexperts-apache-cookbook/recipes/default.rb # # Cookbook:: ktexperts-apache-cookbook # Recipe:: default # # Copyright:: 2019, The Authors, All Rights Reserved. include_recipe "ktexperts-apache-cookbook::ktexperts-apache-recipe" include_recipe "ktexperts-apache-cookbook::ktexperts-sample-recipe" |
Modify role “ktexperts-web.rb”
1 2 3 4 |
[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]" |
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 |
Modify the recipe”ktexperts-apache-recipe.rb”
Open existing recipe “ktexperts-apache-recipe.rb” and write script to change the content of apache web server
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 chef-repo]# vi cookbooks/ktexperts-apache-cookbook/recipes/ktexperts-apache-recipe.rb # # Cookbook:: ktexperts-apache-cookbook # Recipe:: ktexperts-apache-recipe # # Copyright:: 2019, The Authors, All Rights Reserved. package 'httpd' do action :install end file '/var/www/html/index.html' do content "wwww.ktexperts.com && Knowledge sharing platform && Learn new things" action :create end service 'httpd' do action [ :enable, :start ] end |
Note
:wq! — to quit.
Modify the recipe”ktexperts-sample-recipe.rb “
Open existing recipe “ktexperts-sample-recipe.rb ” and write script to update the machine/node information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[root@ip-172-31-42-243 chef-repo]# vi cookbooks/ktexperts-apache-cookbook/recipes/ktexperts-sample-recipe.rb # Cookbook:: ktexperts-apache-cookbook # Recipe:: ktexperts-sample-recipe # # Copyright:: 2019, The Authors, All Rights Reserved. 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 |
Upload cookbook “ktexperts-apache-cookbook” to chef server
1 2 3 |
[root@ip-172-31-42-243 chef-repo]# knife cookbook upload ktexperts-apache-cookbook Uploading ktexperts-apache-cookbook [0.1.0] Uploaded 1 cookbook. |
Verify web content and robo file in Node1 & Node2
Go inside node 1 “Chef-Node-1” 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: Fri Jan 3 07:42:59 2020 from 123.201.77.53 __| __|_ ) _| ( / Amazon Linux 2 AMI ___|\___|___| https://aws.amazon.com/amazon-linux-2/ [ec2-user@ip-172-31-32-143 ~]$ |
Switch to root user
1 2 3 |
[ec2-user@ip-172-31-32-143 ~]$ sudo su [root@ip-172-31-32-143 ec2-user]# |
Verify the file “robofile”
1 2 3 |
[root@ip-172-31-32-143 ec2-user]# ls / bin dev home lib64 media opt robofile run srv tmp var boot etc lib local mnt proc root sbin sys usr |
To see the content of file “robofile”
1 2 3 4 5 6 |
[root@ip-172-31-32-143 ec2-user]# cat /robofile This is to get Attributes HOSTNAME: ip-172-31-32-143 IPADDRESS: 172.31.32.143 CPU: 2400.131 MEMORY: 1007272kB[ |
Verify Node 2
Go inside Node 2 “Chef-Node-2” 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: Fri Jan 3 07:48:42 2020 from 123.201.77.53 __| __|_ ) _| ( / Amazon Linux 2 AMI ___|\___|___| https://aws.amazon.com/amazon-linux-2/ [ec2-user@ip-172-31-42-17 ~]$ |
Switch to root user
1 2 3 |
[ec2-user@ip-172-31-42-17 ~]$ sudo su [root@ip-172-31-42-17 ec2-user]# |
Verify the file “robofile”
1 2 3 |
[root@ip-172-31-42-17 ec2-user]# ls / bin dev home lib64 media opt robofile run srv tmp var boot etc lib local mnt proc root sbin sys usr |
To see the content of file “robofile”
1 2 3 4 5 6 7 |
[root@ip-172-31-42-17 ec2-user]# cat /robofile This is to get Attributes HOSTNAME: ip-172-31-42-17 IPADDRESS: 172.31.42.17 CPU: 2400.222 MEMORY: 1007272kB[ |
Verify the Web content of 2 Nodes
Node 1 “Chef-Node-1”
Copy the IPV4 Public IP of node 1″Chef-Node-1″
Search IPV4 Public IP in browser
We can able to see the content of node 1 “Chef-Node-1”
Node 2 “Chef-Node-2”
Copy the IPV4 Public IP of node 2″Chef-Node-2″
Search IPV4 Public IP in browser
We can able to see the content of node 2 “Chef-Node-2”
12. Run all recipes from all cookbooks
To see the list of cookbooks
1 2 |
[root@ip-172-31-42-243 chef-repo]# ls cookbooks/ chefignore ktexperts-apache-cookbook ktexperts-cookbook starter |
To see the list of recipes in cookbook “ktexperts-apache-cookbook”
1 2 3 4 5 |
[root@ip-172-31-42-243 chef-repo]# tree cookbooks/ktexperts-apache-cookbook/recipes/ cookbooks/ktexperts-apache-cookbook/recipes/ ├── default.rb ├── ktexperts-apache-recipe.rb └── ktexperts-sample-recipe.rb |
To see the list of recipes in cookbook “ktexperts-cookbook”
1 2 3 4 5 |
[root@ip-172-31-42-243 chef-repo]# tree cookbooks/ktexperts-cookbook//recipes/ cookbooks/ktexperts-cookbook//recipes/ ├── default.rb ├── ktexperts1-recipe.rb └── ktexperts-recipe.rb |
Add ktexperts-apache-recipe.rb and ktexperts-sample-recipe.rb inside the default.rb (ktexperts-apache-cookbook)
1 2 3 4 5 6 7 8 9 |
[root@ip-172-31-42-243 chef-repo]# vi cookbooks/ktexperts-apache-cookbook/recipes/default.rb # # Cookbook:: ktexperts-apache-cookbook # Recipe:: default # # Copyright:: 2019, The Authors, All Rights Reserved. include_recipe "ktexperts-apache-cookbook::ktexperts-apache-recipe" include_recipe "ktexperts-apache-cookbook::ktexperts-sample-recipe" |
Note
:wq! — to quit.
Add ktexperts-recipe.rb and ktexperts1-recipe.rb inside the default.rb (ktexperts-cookbook)
1 2 3 4 5 6 7 8 9 |
[root@ip-172-31-42-243 chef-repo]# vi cookbooks/ktexperts-cookbook/recipes/default.rb # # Cookbook:: ktexperts-cookbook # Recipe:: default # # Copyright:: 2019, The Authors, All Rights Reserved. include_recipe "ktexperts-cookbook::ktexperts-recipe" include_recipe "ktexperts-cookbook::ktexperts1-recipe" |
Note
:wq! — to quit.
Modify role “ktexperts-web.rb”
1 2 3 4 |
[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]" |
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 |
Modify the recipe”ktexperts-apache-recipe.rb”
Open existing recipe “ktexperts-apache-recipe.rb” and write script to change the content of apache web server
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 chef-repo]# vi cookbooks/ktexperts-apache-cookbook/recipes/ktexperts-apache-recipe.rb # # Cookbook:: ktexperts-apache-cookbook # Recipe:: ktexperts-apache-recipe # # Copyright:: 2019, The Authors, All Rights Reserved. package 'httpd' do action :install end file '/var/www/html/index.html' do content "wwww.ktexperts.com && Knowledge sharing platform && Learn new things && Write Articles on DevOps" action :create end service 'httpd' do action [ :enable, :start ] end |
Note
:wq! — to quit.
Modify the recipe”ktexperts-sample-recipe.rb “
Open existing recipe “ktexperts-sample-recipe.rb ” and write script to update the system information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[root@ip-172-31-42-243 chef-repo]# vi cookbooks/ktexperts-apache-cookbook/recipes/ktexperts-sample-recipe.rb # Cookbook:: ktexperts-apache-cookbook # Recipe:: ktexperts-sample-recipe # # Copyright:: 2019, The Authors, All Rights Reserved. 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 |
Note
:wq! — to quit.
Modify the recipe”ktexperts-recipe.rb “
Open existing recipe “ktexperts-recipe.rb ” and write script to create user,group and file
1 2 3 4 5 |
[root@ip-172-31-42-243 chef-repo]# vi cookbooks/ktexperts-cookbook/recipes/ktexperts-recipe.rb user "ram" group "DevOps-ktexperts" file "/ktexperts-recipefile" |
Note
:wq! — to quit.
Modify the recipe”ktexperts1-recipe.rb “
Open existing recipe “ktexperts1-recipe.rb ” and write script to install tree package and create file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@ip-172-31-42-243 chef-repo]# vi cookbooks/ktexperts-cookbook/recipes/ktexperts1-recipe.rb # # Cookbook:: ktexperts-cookbook # Recipe:: ktexperts1-recipe # # Copyright:: 2019, The Authors, All Rights Reserved. package 'tree'do action :install end file '/ktexperts1-recipefile' do content 'This is my website' action :create owner 'root' group 'root' end |
Note
:wq! — to quit.
Upload 2 cookbooks to chef server
1 2 3 4 5 |
[root@ip-172-31-42-243 chef-repo]# knife cookbook upload --all Uploading ktexperts-apache-cookbook [0.1.0] Uploading ktexperts-cookbook [0.1.0] Uploading starter [1.0.0] Uploaded all cookbooks. |
Verify cookbooks
To see the list of cookbooks
1 2 3 4 |
[root@ip-172-31-42-243 chef-repo]# knife cookbook list ktexperts-apache-cookbook 0.1.0 ktexperts-cookbook 0.1.0 starter 1.0.0 |
Verify the Web content of 2 Nodes
Node 1 “Chef-Node-1”
Copy the IPV4 Public IP of node 1″Chef-Node-1″
Search IPV4 Public IP in browser
We can able to see the content of node 1 “Chef-Node-1”
Node 2 “Chef-Node-2”
Copy the IPV4 Public IP of node 2″Chef-Node-2″
Search IPV4 Public IP in browser
We can able to see the content of node 2 “Chef-Node-2”
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