Plain Ruby with Chef DSL and Ruby Gems with Recipes

Last updated on Nov 12 2021
Satyen Sahu

Table of Contents

Plain Ruby with Chef DSL and Ruby Gems with Recipes

Chef – Plain Ruby with Chef DSL

In Chef, if one needs to create simple recipes one can use resources available in Chef, such as templates, remote_file, and services. However as the recipes become elaborate, one needs advanced techniques, such as conditional statements to execute parts of the recipe on condition. This is the power of mixing plain Ruby with Chef Domain Specific Language (DSL).

How to Use It?

Start Chef Shell on any of the node in the client mode to be able to access the Chef server.
user@server:~$ sudo chef-shell --client
loading configuration: /etc/chef/client.rb
Session type: client
...TRUNCATED OUTPUT...
run `help' for help, `exit' or ^D to quit.
Ohai2u user@server!
Chef>

Basic Conditions with Chef DSL

Sort nodes by name using plain Ruby.

chef > nodes.sort! {|a,b| a.name <=> b.name }
=> [node[alice],node[server]]
Loop through the nodes, printing their operating system.
chef > nodes.each do |n|
   chef > puts n['os']
   chef ?>
end 
linux
windows
=> [node[server], node[alice]]

Install multiple Ruby gems using an array, a loop, and string expansion to construct the gem names.

chef > %w{ec2 essentials}.each do |gem|
chef > gem_package "knife-#{gem}"
chef ?> end   => ["ec2", "essentials"]

Working Method

Chef recipes are Ruby files, which gets evaluated in the context of Chef run. They can contain plain Ruby code such as if statement and loops as well as Chef DSL elements such as resources.

Inside the recipe, one can simply declare Ruby variables and assign values to it.

Chef – Ruby Gems with Recipes

Recipes are the key building blocks of cookbook which is basically Ruby code. It is possible to use all of the Ruby language features inside the Chef recipe. Most of the time Ruby build in functionality is enough but sometimes one might need to use additional Ruby gems. For example, if one needs to access MySQL database from the recipe itself.

Chef recipe has the capability to get the required Ruby gems in order to use them inside the very same recipe.

Using iptable Gem in the Given Recipe

Step 1 − Edit the default recipe of the cookbook and install the gem to be used inside the recipe.

vipin@laptop:~/chef-repo $ subl
cookbooks/my_cookbook/recipes/default.rb
chef_gem 'ipaddress'
require 'ipaddress'
ip = IPAddress("192.168.0.1/24")
Chef::Log.info("Netmask of #{ip}: #{ip.netmask}")

Step 2 − Upload the modified cookbook to the Chef server.

vipin@laptop:~/chef-repo $ knife cookbook upload my_cookbook
Uploading my_cookbook [0.1.0]

Step 3 − Running Chef client to see the output.

user@server $ sudo chef-client
TRUNCATED OUTPUT...
[2013-01-18T14:02:02+00:00] INFO: Netmask of 192.168.0.1:
255.255.255.0
TRUNCATED OUTPUT

Working Method

Chef run steps consist of the compilation phase, where it compiles all the resources and an execution phase where Chef runs the resource providers to converge the node to the desired state. If one needs any particular Ruby gem inside the cookbook, one needs to install the gem during the complication phase.

The chef_gem resource will exactly do the same, and in Chef, Omnibus is the only way to work. Its main function is to make gems available to Chef itself.

So, this brings us to the end of blog. This Tecklearn ‘Plain Ruby with Chef DSL and Ruby Gems with Recipes’ 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 "Plain Ruby with Chef DSL and Ruby Gems with Recipes"

Leave a Message

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