Roles in Chef

Share via:

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

  1. Lauch 2 Nodes “Chef-Node-1” and “Chef-Node-2”.
  2. Bootstrap a 2 Nodes.
  3. Create a Role “ktexperts-web.rb”.
  4. Upload role to chef server.
  5. Attache 2 Nodes to Role.
  6. Upload cookbook “ktexperts-apache-cookbook” to chef server.
  7. Verify the Web content of 2 Nodes.
  8. Modify the recipe”ktexperts-apache-recipe.rb”.
  9. Upload cookbook “ktexperts-apache-cookbook” to chef server.
  10. Verify the Web content of 2 Nodes.
  11. Run all recipes inside the cookbook “ktexperts-apache-cookbook”.
  12. 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.

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

Switch to root user

Go to 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.

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.

 Verify bootstrap nodes

Verify 2 Nodes in graphically

3. Create a Role “ktexperts-web.rb”

Note

:wq! — to quit.

4. Upload role to chef server

Verify Roles 

5. Attache 2 Nodes to Role

Attache Node 1 “Chef-Node-1” to Role 

Verify Run List of Node 1 “chef-Node-1”

Attache Node 2 “Chef-Node-2” to Role 

Verify Run List of Node 2 “chef-Node-2”

6. Upload cookbook “ktexperts-apache-cookbook” to chef server

Verify cookbook

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

Note

:wq! — to quit.

9. Upload cookbook “ktexperts-apache-cookbook” to chef server

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.

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 

Modify role “ktexperts-web.rb” 

Upload role to chef server

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

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.

Upload cookbook “ktexperts-apache-cookbook” to chef server

Verify web content and robo file in Node1 & Node2

Go inside node 1 “Chef-Node-1” through putty

Switch to root user

Verify the file “robofile”

To see the content of file “robofile”

Verify Node 2

Go inside Node 2 “Chef-Node-2” through putty

Switch to root user

Verify the file “robofile”

To see the content of file “robofile”

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

To see the list of recipes in cookbook “ktexperts-apache-cookbook”

To see the list of recipes in cookbook “ktexperts-cookbook”

Add  ktexperts-apache-recipe.rb and ktexperts-sample-recipe.rb inside the default.rb (ktexperts-apache-cookbook)

Note

:wq! — to quit.

Add  ktexperts-recipe.rb and ktexperts1-recipe.rb inside the default.rb (ktexperts-cookbook)

Note

:wq! — to quit.

Modify role “ktexperts-web.rb”

Upload role to chef server

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

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.

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

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

Note

:wq! — to quit.

Upload 2 cookbooks to chef server

Verify cookbooks

To see the list of cookbooks

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.

Follow us on 

Website :  www.ktexperts.com

Facebook Page : KTexperts

Linkedin Page : KT EXPERTS

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...

One thought on “Roles in Chef

Add Comment