Monday, August 7, 2017

Using Roles

Using roles 

Roles group nodes with similar configurations.Typical cases are using roles for web servers,database servers and so on.


You can set custom run lists for all nodes in your roles and override attribute values from within your roles.

how to create a simple role.

For the following examples,I assume that you have a node named server and that you have atleast one cookbook  (ntp cookbook) registered with your Chef server.

How to do it?

Create a role: 

local@workstation:~/chef-repo  $ subl roles/web_servers.rb
name  "web_servers"
description "This role contains nodes,which acts as web servers"
run_list "recipe[ntp]"
default_attribute 'ntp' => {
'ntpdate' => {
'disable' => true
}
}

2) Upload the role on the Chef server:
local@workstation:~/chef-repo  $ knife role from  web_servers.rb
Updated Role web_server

3) Assign the role to a node called server:
local@workstation:~/chef-repo  $ knife node run_list add server 'role[web_servers]'
server:
run_list: role[web_servers]

4)log in to your node and run the Chef client:
local@workstation:~/chef-repo  $  sudo chef-client

..TRUNCATED OUTPUT.....

INFO: Run List is [role[web_servers]]
INFO:  Run List expands to [ntp]
INFO:  Starting Chef Run for server
..TRUNCATED OUTPUT.....


How it works?

You define a role in a Ruby (or a JSON) file inside the roles folder of your Chef repository.A role consists of a nameattribute and a description attribute. Additionally, a role usually contains a role-specific run list and role-specific attribute settings.

Every node with a role in its run list will have the role's run list expanded into its own.This means that all the recipes (and roles) that are in the role's run list will be executed on your nodes.

You need to upload your role to your Chef server by using knife role  from file command.
Only then should you add the role to your node's run list.

Running the Chef client on a node having your role in its run_list will execute all the recipes listed in the role.

The attributes you define in your role will be merged with attributes from environment and cookbooks according to the precedence rules described at
https://docs.chef.io/roles.html#attribute-precedence.
























No comments:

Post a Comment