DevOps Classroom Series – 10/Nov/2021

Notifications

  • A resource may listen to another resource and then take action if the state of the resource being listened changes
file '/var/www/html/info.php' do
    content '<h1> hello </h1>'
end

service 'apache2' do
  action :nothing
  subscribes :restart, 'file[/var/www/html/info.php]', :immediately
end

file '/tmp/apache2' do
  action :nothing
  subscribes :touch, 'file[/var/www/html/info.php]', :immediately
end

Activity: Installing tomcat9 on ubuntu 20.04

  • Refer Here for the manual steps
  • Lets execute manual steps on the machine created using test kitchen
  • Create a new cookbook tomcat9
  • Initially lets automate till tomcat installation and enabling tomcat service
  • Improvements:
    • Writing all the resources in one recipe is not a good idea. So lets create different recipes.
    • Lets start by using attributes
  • Best Practices:
    • default.rb in recipes should just have resources to fail if they are running on unsupported platform or platform family and it should call other recipes to automate the deployment
  • Recipe name => Generally recipe name is <cookbook-name>::<recipe-name-with-out-extension>. If we donot pass recipe name chef will assume it to be default <cookbook-name> => <cookbook-name>::default
  • Lets generate a recipe for java installation
  • Attributes so far
if platform?('ubuntu')
  default['tomcat9']['java_package'] = 'openjdk-11-jdk'
end
  • recipe => java
#
# Cookbook:: tomcat9
# Recipe:: java
#
# Copyright:: 2021, The Authors, All Rights Reserved.

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

package node['tomcat9']['java_package'] do
  action :install
end

  • recipe => default
#
# Cookbook:: tomcat9
# Recipe:: default
#
# Copyright:: 2021, The Authors, All Rights Reserved.

include_recipe 'tomcat9::java'
  • Refer Here for the changes done
  • Now lets try to converge to check if the java 11 is getting installed or not
    • kitchen converge Preview
    • login & check for java installation Preview
  • Next step to be automated
sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat

 -m, --create-home
  -d, --home-dir /opt/tomcat
 -U, --user-group   create a group with the same name as the user => tomcat

 -s, --shell SHELL             login shell of the new account /bin/false

  • Refer Here for the user resource
  • For this lets create a new recipe configure
  • Refer Here for the changes done
  • Now add this recipe to default.rb in recipes
include_recipe 'tomcat9::java'
include_recipe 'tomcat9::configure'
  • Fixed the group not existing issue and added configure to be included from default in this changeset Refer Here
  • Exercise: Try to automate the steps which we have done for ubuntu in this cookbook to make it work for centos7 following the documentation Refer Here

Leave a Reply

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

Please turn AdBlock off
Floating Social Media Icons by Acurax Wordpress Designers

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