English 中文(简体)
Chef - Roles
  • 时间:2024-12-22

Chef - Roles


Previous Page Next Page  

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 pst for all the nodes and override attribute value within roles.

Create a Role

vipin@laptop:~/chef-repo $ subl roles/web_servers.rb 
name "web_servers" 
description "This role contains nodes, which act as web servers" 
run_pst "recipe[ntp]" 
default_attributes  ntp  => { 
    ntpdate  => { 
       disable  => true 
   } 
}

Once we have the role created, we need to upload to the Chef server.

Upload Role to Chef Server

vipin@laptop:~/chef-repo $ knife role from file web_servers.rb 

Now, we need to assign a role to a node called server.

Assign a Role to Node

vipin@laptop:~/chef-repo $ knife node edit server 
"run_pst": [ 
   "role[web_servers]" 
] 
Saving updated run_pst on node server 

Run the Chef-Cpent

user@server:~$ sudo chef-cpent 
...TRUNCATED OUTPUT... 
[2013-07-25T13:28:24+00:00] INFO: Run List is [role[web_servers]] 
[2013-07-25T13:28:24+00:00] INFO: Run List expands to [ntp] 
...TRUNCATED OUTPUT... 

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 pst and role-specific attribute settings.

    Every node that has a role in its run pst will have the role’s run pst exacted into its own.

    All the recipes in the role’s run pst 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 pst.

    Running Chef cpent on a node having the role in its run pst will execute all the recipes psted in the role.

Advertisements