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
#
# 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
#
# 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

- login & check for java installation

- 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
Like this:
Like Loading...