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