How to setup a Chef Role for Node

How to setup a Chef Role for Node

chef_role_setup_for_node

Here in this article we will try to set up a role for the node and define runlist and attributes.

Test Environment

Hosted Chef Server – https://manage.chef.io/
desktop1.example.com    – Used for preparing cookbooks and recipes
server1.example.com – Managed using hosted chef server

What is Berksfile

A Berksfile is a Ruby file, in which sources, dependencies, and options may be specified.

Procedure

Step1: Create a Berksfile to locate and download cookbook from chef supermarket

Here in this step let us try to create a berksfile and download the dependency as defined in the file from the chef supermarket.

[root@desktop1 chefspace]# pwd
/home/student/Middleware/Source/chefspace
[root@desktop1 chefspace]# vi Berksfile
[root@desktop1 chefspace]# cat Berksfile
source 'https://supermarket.chef.io'
cookbook 'chef-client'
[root@desktop1 chefspace]# berks install
Resolving cookbook dependencies...
Fetching cookbook index from https://supermarket.chef.io...
Installing chef-client (8.1.2)
Installing compat_resource (12.19.0)
Installing cron (4.1.3)
Installing logrotate (2.1.0)
Installing ohai (5.1.0)
Installing windows (3.1.0)
[root@desktop1 chefspace]# ls -ltr ~/.berkshelf/cookbooks/
total 24
drwxr-xr-x. 4 root root 4096 Jun  6 05:22 compat_resource-12.19.0
drwxr-xr-x. 7 root root 4096 Jun  6 05:22 chef-client-8.1.2
drwxr-xr-x. 8 root root 4096 Jun  6 05:22 cron-4.1.3
drwxr-xr-x. 7 root root 4096 Jun  6 05:22 logrotate-2.1.0
drwxr-xr-x. 5 root root 4096 Jun  6 05:22 ohai-5.1.0
drwxr-xr-x. 7 root root 4096 Jun  6 05:22 windows-3.1.0

Step2: Upload the downloaded cookbook and its dependencies to Chef server

Now let’s upload these downloaded dependencies to our Hosted chef server as shown below.

[root@desktop1 chefspace]# berks upload
Uploaded chef-client (8.1.2) to: 'https://api.chef.io:443/organizations/sudhir-org'
Uploaded compat_resource (12.19.0) to: 'https://api.chef.io:443/organizations/sudhir-org'
Uploaded cron (4.1.3) to: 'https://api.chef.io:443/organizations/sudhir-org'
Uploaded logrotate (2.1.0) to: 'https://api.chef.io:443/organizations/sudhir-org'
Uploaded ohai (5.1.0) to: 'https://api.chef.io:443/organizations/sudhir-org'
Uploaded windows (3.1.0) to: 'https://api.chef.io:443/organizations/sudhir-org'

Step3: Create a role define run-list and schedule to run cookbook periodically

Roles enable you to focus on the function your node performs collectively rather than each of its individual components (its run-list, node attributes, and so on).

[root@desktop1 chefspace]# mkdir roles
[root@desktop1 chefspace]# vi roles/web.json
[root@desktop1 chefspace]# cat roles/web.json
{
   "name": "web",
   "description": "Web server role.",
   "json_class": "Chef::Role",
   "default_attributes": {
     "chef_client": {
       "interval": 300,
       "splay": 60
     }
   },
   "override_attributes": {
   },
   "chef_type": "role",
   "run_list": ["recipe[chef-client::default]",
                "recipe[chef-client::delete_validation]",
                "recipe[apache_httpd::default]"
   ],
   "env_run_lists": {
   }
}

Step4: Upload the role to Chef server

Here we will use “knife” utility to upload the role to our Hosted Chef server.

[root@desktop1 chefspace]# knife role from file roles/web.json
Updated Role web
[root@desktop1 chefspace]# knife role list
web

Step5: Set the nodes run list

Here let’s set the roles that needs to be executed on a managed node.

[root@desktop1 chefspace]# knife node run_list set rhel7 "role[web]"
rhel7:
  run_list: role[web]

Step6: Run the chef-client on your node remotely

[root@desktop1 chefspace]# knife ssh 'role:web' 'sudo chef-client' --ssh-user student --ssh-password 'r3dh@t1!' --attribute ipaddress
...
192.168.0.101 Running handlers:
192.168.0.101 [2017-06-06T05:47:59-04:00] INFO: Running report handlers
192.168.0.101 Running handlers complete
192.168.0.101 [2017-06-06T05:47:59-04:00] INFO: Report handlers complete
192.168.0.101 Chef Client finished, 0/18 resources updated in 14 seconds
192.168.0.101 [2017-06-06T05:47:59-04:00] INFO: Sending resource update report (run-id: e27001c2-f036-4dfd-9286-590d867dcd5b)

Step7: Check the status of the node

[root@desktop1 chefspace]# knife status 'role:web' --run-list
2 minutes ago, rhel7, ["role[web]"], redhat 7.0.
[root@desktop1 chefspace]# knife status 'role:web' --run-list
3 minutes ago, rhel7, ["role[web]"], redhat 7.0.
[root@desktop1 chefspace]# knife status 'role:web' --run-list
4 minutes ago, rhel7, ["role[web]"], redhat 7.0.
[root@desktop1 chefspace]# knife status 'role:web' --run-list
5 minutes ago, rhel7, ["role[web]"], redhat 7.0.
[root@desktop1 chefspace]# knife status 'role:web' --run-list
0 minutes ago, rhel7, ["role[web]"], redhat 7.0.

Hope you enjoyed reading this article. Thank you..