DevOps Classroom Series – 23/Jan/2021

Optimizing Cookbooks/recipes using attributes, Notifications and gaurds

  • In Chef we can define parameters that can be used in the recipe by using attribute files
  • Chef attributes file is collection of attributes and each attribute represents specific detail.
  • There are two kinds of attributes
    • User defined attributes
    • System Collected attributes
  • Attributes are collected by chef system during every convergance and referred as ‘automatic attributes’
  • Attributes can be defined at various places, But lets restrict ourselves to attributes in attributes file Preview Preview
  • Now lets generate an attribute file called as default Preview
  • To create attribute in attribute file we can use ruby hash syntax and in this series we will restrict to only type of attributes default
  • Refer Here for the changes made
  • Made changes to conditional statement Refer Here
  • Now lets converge and verify if the cookbook is working as expected
  • We can use chef common attributes such as only_if and not_if to avoid writing ruby conditional statements Refer Here. Refer Here for usage of only_if gaurd
  • We have one more problem, whenever we converge apache server is getting restarted. It should only restart
    • in the case of new installation
    • When info.php is changed.
  • In chef we have a concept called as notifications where one resource can notify other resource to execute Refer Here for official docs
  • Lets implement notification functionality Refer Here
  • Notifies can notify only one resource. If you have to notify multiple resources, then use subscribes over notify Refer Here
file '/tmp/txt' do
  action :create
end

service 'apache'
   action :nothing
   subscribes :restart, 'file[/tmp/txt]'
end

service 'tomcat8'
  action :nothing
  subscribes :restart, 'file[/tmp/txt]'
end
  • In the lampserver i have deleted the unnecessary files Refer Here
  • Now we have only one problem the content of info.php is only one line so we are using content property. What if the content is a large file with multiple lines
file '/var/www/html/info.php' do
    content '<?php phpinfo(); ?>'
    action :create
    notifies :restart, "service[#{package_name}]"
end
  • We can solve above problem by creating file to be copied in the cookbook itself and use the file in cookbook to create file in remote node. Preview Preview
  • Lets generate a file called as info.php in chef cookbook. Refer Here for the changeset

Scenario: Configure Tomcat Server on ubuntu

  • Exercise: Configure tomcat server on ubuntu
  • Manual Steps:
sudo apt-get update
sudo apt install openjdk-8-jdk -y
sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
# For rest refer the page https://www.digitalocean.com/community/tutorials/install-tomcat-9-ubuntu-1804

  • Create a cookbook
  • Generate a recipe called as ubuntu_tomcat
  • Generate a attribute file called as default to store package name and username
  • Refer Here for the changeset
  • Refer Here for kitchen command line syntaxes.

Leave a Reply

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

About learningthoughtsadmin