Roles in Chef and perform environment specific configuration

Last updated on Nov 29 2021
Satyen Sahu

Table of Contents

Roles in Chef and perform environment specific configuration

Chef – Roles

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 list 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_list "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_list": [
   "role[web_servers]"
]

Saving updated run_list on node server

Run the Chef-Client

user@server:~$ sudo chef-client
...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 list and role-specific attribute settings.
  • Every node that has a role in its run list will have the role’s run list exacted into its own.
  • All the recipes in the role’s run list 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 list.
  • Running Chef client on a node having the role in its run list will execute all the recipes listed in the role.

Chef – Environment

Chef helps in performing environment specific configuration. It is always a good idea to have a separate environment for development, testing, and production.

Chef enables grouping nodes into separate environments to support an ordered development flow.

Creating an Environment

Creation of environment on the fly can be done using the knife utility. Following command will open a Shell’s default editor, so that one can modify the environment definition.

vipin@laptop:~/chef-repo $ knife environment create book {
   "name": "book",
   "description": "",
   "cookbook_versions": {
   },
   "json_class": "Chef::Environment",
   "chef_type": "environment",
   "default_attributes": {
   },
   "override_attributes": {
   }
}

Created book

Testing a Created Environment

vipin@laptop:~/chef-repo $ knife environment list
_default
book

List Node for All Environments

vipin@laptop:~/chef-repo $ knife node list
my_server

_default Environment

Each organization will always start with at least a single environment called default environment, which is always available to the Chef server. A default environment cannot be modified in anyway. Any kind of changes can only be accommodated in the custom environment that we create.

Environment Attributes

An attribute can be defined in an environment and then used to override the default settings in the node. When the Chef client run takes place, then these attributes are compared with the default attributes that are already present in the node. When the environment attributes take precedence over the default attributes, Chef client will apply these settings and values when the Chef client run takes place on each node.

An environment attribute can only be either default_attribute or override_attribute. It cannot be a normal attribute. One can use default_attribute or override_attribute methods.

Attribute Type

Default − A default attribute is always reset at the start of every Chef client run and have the lowest attribute precedence.

Override − An override attribute is always reset at the start of every Chef client run and has a higher attribute precedence than default, force_default and normal. An override attribute is most often defined in the recipe but can also be specified in an attribute file for a role or for an environment.

Order of Applying an Attribute

image1 8

So, this brings us to the end of blog. This Tecklearn ‘Roles in Chef and perform environment specific configuration’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Chef and build a career in DevOps domain, then check out our interactive, Continuous Delivery using Chef Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/continuous-delivery-using-chef/

Continuous Delivery using Chef Training

About the Course

Tecklearn has specially designed this Continuous Delivery using Chef Training Course to advance your skills for a successful career in this domain. The course will cover different components of Chef and how they are used in software development operations. The course consists of important concepts like Continuous Delivery (CD) using Chef, Chef Framework, How Chef Works, Chef Advantages and Chef Installation. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Chef.

Why Should you take Continuous Delivery using Chef Training?

  • The average salary for a Senior Development Operations (DevOps) Engineer with Chef skills is $118, 435. – PayScale.com
  • Airbnb, Facebook, Slack, Shopify, Digital Ocean & many other MNC’s worldwide use Chef across industries.
  • According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps

  • What is Software Development
  • Software Development Life Cycle
  • Why DevOps?
  • What is DevOps?
  • DevOps Lifecycle
  • DevOps Tools
  • Benefits of DevOps
  • How DevOps is related to Agile Delivery
  • DevOps Implementation

Continuous Delivery using Chef

  • Continuous Delivery
  • What is Chef
  • Chef Framework
  • How Chef Works
  • Chef Advantages
  • Chef Installation
  • Hands on

Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "Roles in Chef and perform environment specific configuration"

Leave a Message

Your email address will not be published. Required fields are marked *