Lets say that we want to have a couple of cookbooks
to be executed on the nodes. In a normal case we can write those cookbooks ,
upload them to the chef-server. Roles in Chef are way to execute multiple
cookbooks at one time on the chef-nodes.
Using Roles , we can logically group
machines. Lets say we have a group of machines that run web servers and we want
to run some cookbooks on machines that run web servers . in this case we will create
a role and set all the cookbooks that need to run on that role ( on those
specific set of machines running web servers ). Lastly we will define the
machine with that role. In this article we will see an example of using roles,
Create a role using in the
~chef-repo/cookbooks/roles
[root@chefdk roles]# knife role from file web_servers.rb
Updated Role web_servers
Now edit the role as,
[root@chefdk roles]# knife role edit web_servers
This will take you to the web_servers file opened in a vi
editor. Edit the content as below,
{
"name":
"web_servers",
"description": "This role contains nodes, which act as
web servers",
"json_class": "Chef::Role",
"default_attributes": {
"ntp": {
"ntpdate":
{
"disable": true
}
}
},
"override_attributes": {
},
"chef_type": "role",
"run_list": [
"recipe[httpd]"
],
"env_run_lists": {
}
}
In
the above snippet of code, I added the line "recipe[httpd]" in the run_list.
What iam trying to tell is that nodes which are assigned with the role
web_servers, execute this cookbook or receipe httpd. Iam editing the role to
define that cookbooks that need to be executed.Edit the Node by adding the role, run the command, knife
node edit firstnode
This
will open the node in an editor. Make changes as shown in the below content,
{
"name": "firstnode",
"chef_environment":
"_default",
"normal": {
"tags": [
]
},
"policy_name": null,
"policy_group": null,
"run_list": [
"role[web_servers]"
]
}
In
the above snippet, I have added the line “role[web_servers]”. Save it.
Once
this is done , we attached a role called “web_servers” to the node firstnode.
Previously we added the cookbooks that need to run as a part of role. Now lets
go to the chef-node and execute the command as,
[root@chefclient ~]# chef-client
[2018-09-04T04:55:04-04:00]
INFO: Forking chef instance to converge...
Starting
Chef Client, version 12.22.5
[2018-09-04T04:55:04-04:00]
INFO: *** Chef 12.22.5 ***
[2018-09-04T04:55:04-04:00]
INFO: Platform: x86_64-linux
[2018-09-04T04:55:04-04:00]
INFO: Chef-client pid: 23804
[2018-09-04T04:55:04-04:00]
INFO: The plugin path /etc/chef/ohai/plugins does not exist. Skipping...
[2018-09-04T04:55:07-04:00] INFO: Run List is
[role[web_servers]]
[2018-09-04T04:55:07-04:00] INFO: Run List
expands to [httpd]
[2018-09-04T04:55:07-04:00] INFO: Starting
Chef Run for firstnode
[2018-09-04T04:55:07-04:00]
INFO: Running start handlers
[2018-09-04T04:55:07-04:00]
INFO: Start handlers complete.
[2018-09-04T04:55:07-04:00]
INFO: HTTP Request Returned 404 Not Found:
[2018-09-04T04:55:07-04:00]
INFO: HTTP Request Returned 404 Not Found:
[2018-09-04T04:55:07-04:00]
INFO: Error while reporting run start to Data Collector. URL:
https://chefserver.nova.com/organizations/nova/data-collector Exception: 404 --
404 "Not Found" (This is
normal if you do not have Chef Automate)
resolving
cookbooks for run list: ["httpd"]
[2018-09-04T04:55:08-04:00]
INFO: Loading cookbooks [httpd@0.1.0]
Synchronizing
Cookbooks:
- httpd (0.1.0)
Installing
Cookbook Gems:
Compiling
Cookbooks...
[2018-09-04T04:55:08-04:00]
INFO: HTTP Request Returned 404 Not Found:
Converging
3 resources
Recipe:
httpd::default
* yum_package[httpd] action
install[2018-09-04T04:55:08-04:00] INFO: Processing yum_package[httpd] action
install (httpd::default line 7)
(up to date)
* service[httpd] action
enable[2018-09-04T04:55:09-04:00] INFO: Processing service[httpd] action enable
(httpd::default line 11)
(up to date)
* service[httpd] action
start[2018-09-04T04:55:10-04:00] INFO: Processing service[httpd] action start
(httpd::default line 11)
(up to date)
* cookbook_file[/var/www/html/index.html]
action create[2018-09-04T04:55:10-04:00] INFO: Processing
cookbook_file[/var/www/html/index.html] action create (httpd::default line 15)
(up to date)
[2018-09-04T04:55:10-04:00]
INFO: Chef Run complete in 2.486547636 seconds
Running
handlers:
[2018-09-04T04:55:10-04:00]
INFO: Running report handlers
Running
handlers complete
[2018-09-04T04:55:10-04:00]
INFO: Report handlers complete
Chef
Client finished, 0/4 resources updated in 06 seconds
We
can see from the output that the chef-client has ran the cookbooks that are
defined in the role.
No comments :
Post a Comment