DevOps Classroom Series – 31/Aug/2020

Ohai

  • Is installed as part of chef_client and also is available on Workstations.
  • The information collected by ohai can be used in cookbooks. This information is also referred as attribute (automatic)
    • Operating System
    • Network
    • Memory
    • Disk
    • CPU
    • Kernel
    • Hostnames
    • Virtualization
    • FQDN’s
  • The information collected by ohai can be used in the chef cookbooks using the syntax
node['attribute-name']
  • The information collected by ohai is a ruby hash

Multiple recipes in chef cookbook

  • To create a recipe in chef cookbook, we will be chef generator
chef generate --help
chef generate recipe --help
  • Now lets generate two recipes in this cookbook
chef generate recipe . ubuntu
chef generate recipe . redhat
  • Now move the stuff from default.rb in recipes to ubuntu.rb
  • Lets try to write resources for automating lamp installation on redhat following the documentation over here
  • Based on the above changes ubuntu.rb will be as shown below
#
# Cookbook:: .
# Recipe:: ubuntu
#
# Copyright:: 2020, The Authors, All Rights Reserved.
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
  • redhat.rb will have the following resources
#
# Cookbook:: .
# Recipe:: redhat
#
# Copyright:: 2020, The Authors, All Rights Reserved.


### Manual Commands
# sudo yum install httpd
# sudo yum install 

packages = %w(httpd php php-mysql php-fpm)

packages.each do |package_name|
    yum_package package_name do
        action :install
    end
end

  • Recipe calling conventions in chef. In chef to address recipe the pattern followed is <cookbook-name>::<recipe-name>
  • In the run_list we need to follow this naming convention. If you see just the cookbook name that is equivalent to <cookbook>::default
  • In the default.rb use attributes collected by ohai to make the decision to call the recipe
#
# Cookbook:: mylamp
# Recipe:: default
#
# Copyright:: 2020, The Authors, All Rights Reserved.

if node['platform'] == 'ubuntu'
    # call the ubuntu recipe
    include_recipe 'mylamp::ubuntu'
    
elsif node['platform'] == 'redhat'
    # call the redhat recipe
    include_recipe 'mylamp::redhat'
end


  • Change the version of the cookbook & upload using berks
  • We have two problems to be addressed
    1. Can’t we use a generic package which will decide whether it has to run apt or yum or dnf etc. Rather than being specific about package manager, let use a generic resource of chef which decides what package manager has to be used. This is package resource of chef Refer Here
  • Doing the changes with package resource in ubuntu.rb
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)

package packages do
    action :install
end
  • The recipe redhat.rb
packages = %w(httpd php php-fpm)

package packages do
    action :install
end
  • Chef gives some of the recipe dsl methods platform?(‘platform_name’) which is equal node[‘platform’]==<platform name>, so lets adopt this in default.rb
if platform?('ubuntu')
    # call the ubuntu recipe
    include_recipe 'mylamp::ubuntu'
    
elsif platform?('redhat')
    # call the redhat recipe
    include_recipe 'mylamp::redhat'
end
  • The following dsl methods are available
    • platform_family?()
    • reboot_pending?()
  • If you look at ubuntu.rb and redhat.rb the only thing changing is platforms variable value
  • If we add paramater/variable support to the cookbook then i can use one recipe rather than two. Parameters or variables in chef are called as attributes. We can create attributes in a cookbook
  • So lets check for generator method
chef generate --help
chef generate attribute --help
chef generate attribute . default
  • In attributes file the attribute definition has a certain syntax
default['attribute-name'] = 'attribute-value'
  • The best practice always have a cookbook name
default['cookbook-name']['attribute-name'] = ['attribute-value']
  • This can be used in recipes with the syntax
node['cookbook-name']['attribute-name']
  • so lets add the attributes in attributes\default.rb file
if platform?('ubuntu')
    default['mylamp']['packages'] = %w(apache2 php libapache2-mod-php php-mysql)
elsif platform?('redhat')
    default['mylamp']['packages'] = %w(httpd php php-fpm)
else
    default['mylamp']['packages'] = nil
end
  • So now i dont need two recipes, i will be creating one recipe called as lamp with following resources
if platform?('ubuntu')
    apt_update 'update ubuntu packages' do
        ignore_failure true
        action :update
    end
end


packages = node['mylamp']['packages']
package packages do
    action :install
end
  • The default.rb will have the following resources
include_recipe 'mylamp::lamp'

Leave a Reply

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

About learningthoughtsadmin