DevOps Classroom Series – 02/Sept/2020

Chef Resources

Common Functionality

* [Refer Here](https://docs.chef.io/resources/) for official docs
* Common Functionalities are available for all resources
  • Actions: The action :nothing
service 'apache2' do
    action :nothing
end
  • Properties:
    • ignore_failure: If this property is set to true chef will not stop executing on failure
    • retries: The number of attempts to catch exceptions and retry
    • retry_delay: The retry delay in seconds
    • sensitive:
  • Example with common properties
apt_update 'ubuntu packages' do
  ignore_failure true
  action :update
end
  • Guards: Every chef resource supports two blocks
    • only_if: The resource with this will be executed only if the condition matches
    apt_update 'ubuntu packages' do
        action :update
        only_if platform?('ubuntu')
    end
    
    • not_if: Executes the resource when the condition doesn’t match
  • Lets experiment this with lamp stack
  • Create a cookbook lampstack
chef generate -b lampstack
  • Now make changes to kitchen.yaml file to create two platforms
    • ubuntu 18.04
    • Redhat 8
  • Now execute
berks install
kitchen create
kitchen list

  • Now lets create a recipe for apache & php packages call this as lamp
chef generate recipe . lamp
  • Now add an attribute file default
chef generate attribute . default
  • Make changes to attributes\default.rb with
if platform?('ubuntu')
    default['lampstack']['apache']['package'] = 'apache2'
    default['lampstack']['apache']['php_package'] = %w(php libapache2-mod-php php-mysql php-cli)
elsif platform?('redhat')
    default['lampstack']['apache']['package'] = 'httpd'    
    default['lampstack']['apache']['php_package'] = %w(php php-mysqlnd php-fpm)
end

  • Make changes to recipes\lamp.rb
#
# Cookbook:: .
# Recipe:: lamp
#
# Copyright:: 2020, The Authors, All Rights Reserved.

apache_package = node['lampstack']['apache']['package']

php_packages = node['lampstack']['apache']['php_package']

apt_update 'ubuntu package update' do
    ignore_failure true
    action :update
    only_if { platform?('ubuntu') }
end


package apache_package do
    action :install
end

service apache_package do
    action :enable
end


package php_packages do
    action :install
end

service 'restart apache' do
    service_name  apache_package
    action :restart
end



  • Now make changes to recipes\default.rb

if platform?('ubuntu') || platform?('redhat')
    include_recipe 'lampstack::lamp'
else
   Chef::Application.fatal!('This cookbook supports redhat and ubuntu platforms')
end
  • Now lets converge our resources using kitchen
kitchen converge
  • Now execute kitchen list
  • Now the problem in the current recipe is we are writing the same resource service twice
  • Chef has a concept of Notifications.

Chef Notifications

  • Notification is a property on resource that listens to other resources in resource collection & takes action based on notification type.
  • Two kinds of notifications
    • notifies
    • subscribes
  • Refer Here
  • Now implementing notifications in lamp.rb will help to reuse service resource
#
# Cookbook:: .
# Recipe:: lamp
#
# Copyright:: 2020, The Authors, All Rights Reserved.

apache_package = node['lampstack']['apache']['package']

php_packages = node['lampstack']['apache']['php_package']

apt_update 'ubuntu package update' do
    ignore_failure true
    action :update
    only_if  { platform?('ubuntu') }
end


package apache_package do
    action :install
    notifies :enable, "service[#{apache_package}]"
end

service apache_package do
    action :nothing
end


package php_packages do
    action :install
    notifies :restart, "service[#{apache_package}]", :immediately
end

  • Lets add the last step of creating a file with below content in /var/www/html/info.php
<?php
phpinfo();
?>
  • Now the recipe will look like
#
# Cookbook:: .
# Recipe:: lamp
#
# Copyright:: 2020, The Authors, All Rights Reserved.

apache_package = node['lampstack']['apache']['package']

php_packages = node['lampstack']['apache']['php_package']

apt_update 'ubuntu package update' do
    ignore_failure true
    action :update
    only_if  { platform?('ubuntu') }
end


package apache_package do
    action :install
    notifies :enable, "service[#{apache_package}]"
end

service apache_package do
    action :nothing
end


package php_packages do
    action :install
    notifies :restart, "service[#{apache_package}]"
end

file '/var/www/html/info.php' do
    content '<?php phpinfo(); ?>'
    action :create
    notifies :restart, "service[#{apache_package}]", :immediately
end

  • Now converge and verify info.php at http://public-ip/info.php Preview Preview
  • Since cookbook is working now upload cookbook to chef server
berks upload
  • Destroy the test kitchen instances
kitchen destroy

Leave a Reply

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

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube