DevOps Classroom Series – 30/Aug/2020

Just Enough Ruby For Chef

  • Refer Here for ruby guide
  • For installing ruby Refer Here
  • Now launch interactive ruby using irb Preview
  • Variables: Ruby is a dynamically typed language so assign values to variables directly & ruby will decide the type
x = 1
y = 2.5
name = "Chef"
message = "Hello #{name}" 
  • Boolean value: true false
  • Arrays
tools = ['knife', 'ruby', 'berks']
tools[0]
  • Whitespace arrays:
%w(knife ruby berks chef)
  • Hash: ruby hash is data structure to hold key value pairs
complete_address = {
    'blockno' => '208-B',
    'building' => 'Nilgiri Block',
    'area' => 'Ameerpet',
    'landmark' => 'Metro Station'
}
  • To access values of hash use keys
complete_address['landmark']
  • if conditional statements
if <expression>

elsif <expresssion>

else

end
  • Example of if conditional statement
activity = "ConfigurationManagement"
if activity == "ConfigurationManagement"
   tool = "chef"
elsif activity == "InfraProvisioning"
   tool = "Terraform"
else
   tool = nil
end
  • Case statement also can be used
activity = "ConfigurationManagement"
case activity
when 'ConfigurationManagement'
  tool = "Chef"
when 'InfraProvisioning'
  tool = "Terraform"
else
  tool = nil
end
  • Loops with each in ruby (array)
tools = %w(knife chef berks)

tools.each do |tool|
  print(tool)
end
  • loops with while in ruby
tools = %w(knife chef berks)
tools_count = tools.length
index = 0
while index < tools_count do
  print(tools[index])
  index = index+1
end

  • Loops with hash in ruby
devops_hash = {
    "VCS" => 'git',
    "CM" => 'chef',
    "CICD" => 'jenkins',
    "InfraProvisioning" => 'Terraform'
}

devops_hash.each do |purpose,tool|
  print("${purpose} ==== ${tool}")
end
}
  • Using this knowledge lets try to correct the recipe used yesterday to install lamp server
#
# Cookbook:: mylamp
# Recipe:: default
#
# Copyright:: 2020, The Authors, All Rights Reserved.

# Manual Step: sudo apt-get update
apt_update 'update ubuntu packages' do
    ignore_failure true
    action :update
end

# sudo apt-get install apache2 -y
apt_package 'apache2' do
    action :install
end

# sudo apt install php libapache2-mod-php php-mysql -y
apt_package 'php' do
    action :install
end

apt_package 'libapache2-mod-php' do
    action :install
end

apt_package 'php-mysql' do
    action :install
end

# sudo apt install php-cli -y

apt_package 'php-cli' do
    action :install
end

  • This can be replaced with
#
# Cookbook:: mylamp
# Recipe:: default
#
# Copyright:: 2020, The Authors, All Rights Reserved.

# Manual Step: sudo apt-get update
apt_update 'update ubuntu packages' do
    ignore_failure true
    action :update
end

# assigning packages to be installed on ubuntu server to a variable
packages = %w(apache2 php libapache2-mod-php php-mysql)

# installing all the packages
packages.each do |package_name| 
    apt_package package_name do
        action :install
    end
end

  • When we write chef, there is a chance that you might do some syntax errors, since recipe is an rb file we can use ruby syntax checker
ruby -c <path-of-rb-file>

Preview

  • Now lets learn some more concepts here

    1. Inside a chef cookbook i cannot have two resources of same type with same name
      • Not allowed
      service 'apache2' do
         action :restart
      end
      ...
      ...
      service 'apache2' do
         action :stop
      end
      
    2. When we start writing chef cookbooks using -b/berks, it will not allow to upload the changes until and unless you change version in metadata.rb.
      • Without Changing version & uploading cookbook Preview
      • Changing version in metadata.rb & uploading cookbook Preview Preview
    3. run_list will automatically pickup the latest version of cookbook. Preview
  • So lets look at our development workflow

    • The workflow appears to be Preview
    • Problem with this workflow is there is chance that we upload some failure versions to server.
  • So if we try to test the cookbook before uploading to server, then the above problem can be resolved Preview

  • We achieve this in chef using Test Kitchen.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin