chef roles

ROLES
A role is a way to define certain patterns and processes that exist across nodes in an organization as belonging to a single job function
Up until this point, we have assigned recipes to be run for each node. Instead of updating run_list for a node all we have to do is update a role on the server. Prevents us from having to manually touch all nodes that need the change. A role is essentially a listing of recipes and attributes that are to be executed on a node Instead of assigning a run list for each node we assign the node a role. A base role can be assigned inside of a role run_list
Role management with the knife:
Knife role create role_name Chef-repo/roles/rolename.rb
Knife role from chef-repo/roles/rolename.rb Knife role list –w
Knife role delete role_name
Create a Role
#mkdir roles
# ls
Apache2 chefignore httpd motd php roles starter
Navigate to the roles directory and create a file webserver.rb as follows:
#cat webserver.rb
name "webserver"
description "apache web server role"
run_list "recipe[apache]"
Now add this webserver.rb file to the run list as apache and local users and upload it:
#knife role from file webserver.rb
Updated Role webserver

Here webserver role has been created, in that role run list there is apache.
# knife node run_list add centosnode.zippyops.com "role[webserver]"
centosnode.zippyops.com:
run_list:
recipe[motd]
recipe[apache]
role[webserver]
Now run the chef-client command on the node:
# chef-client
Starting Chef Client, version 14.8.12
resolving cookbooks for run list: ["motd", "apache"]
Synchronizing Cookbooks:
- motd (0.1.0)
- apache (0.1.0)
- php (0.1.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 10 resources
Recipe: motd::default
* template[/etc/motd] action create (up to date)
Running handlers:
Running handlers complete
Chef Client finished, 0/11 resources updated in 10 seconds
Now the output of the apache recipe is added run list exactly.

Here webserver role created for the node and the inside of the webserver shows two recipes.
Relevant Blogs:
Recent Comments
No comments
Leave a Comment
We will be happy to hear what you think about this post