Lets create a cookbook to install lamp server on Ubuntu instances
- For this installation we need manual steps, Refer Here for detailed documentation.
- In this exercise we will not be installing mysql-server
- The manual steps which we will automate initially are
sudo apt-get update
sudo apt install apache2 -y
sudo apt install php libapache2-mod-php php-mysql -y
sudo apt install php-cli -y
- So lets create our cookbook
chef generate cookbook -b mylamp

- For developing cookbooks in chef, lets use visual studio with an chef extension installed

- The extension installed will help in developing cookbooks.

- Lets navigate to default.rb in recipes folder and start writing resources
- Now if we automate every step mentioned above in chef, the default.rb in recipes folder will be as shown below
#
# Cookbook:: mylamp
# Recipe:: default
#
# Copyright:: 2020, The Authors, All Rights Reserved.
# Manual Step: sudo apt-get update
apt_update 'update ubuntu packages' do
ignore_failure true
action :update
end
# sudo apt-get install apache2 -y
apt_package 'apache2' do
action :install
end
# sudo apt install php libapache2-mod-php php-mysql -y
apt_package 'php' do
action :install
end
apt_package 'libapache2-mod-php' do
action :install
end
apt_package 'php-mysql' do
action :install
end
# sudo apt install php-cli -y
apt_package 'php-cli' do
action :install
end
- Now lets upload the cookbook to chef server (This time using a tool called as berks)
# execute once after you create a cookbook
berks install
# use this command to upload cookbook (multiple times)
berks upload

- Now verify the cookbook in the chef server management ui

- Now we need to add the recipe to the run_list of the node

- Lets understand the term called as configuration drift. Configuration drift is a state where there is difference in the desired state mentioned in the cookbook and actual state on the node

- During convergence chef-server will send the recipes (and all the other required files to run recipes) in the run-list mentioned to the chef-node to be executed by chef-client
- For this series we are manually forcing convergence by executing
sudo chef-client on the node

- Now try to access http://<public-ip-node>

- While we writing this cookbook we have used the same resource with different values multiple times
apt_package 'apache2' do
action :install
end
# sudo apt install php libapache2-mod-php php-mysql -y
apt_package 'php' do
action :install
end
apt_package 'libapache2-mod-php' do
action :install
end
apt_package 'php-mysql' do
action :install
end
# sudo apt install php-cli -y
apt_package 'php-cli' do
action :install
end
- So now lets correlate this situation with some basic programming
- Approach 1
print('apache2')
print('php')
print('php-mysql')
- Approach 2
names = ['apache2', 'php', 'php-mysql']
for(name in names):
print(name)
- Generally we use approach 2 as writing number of lines of print doesn’t make sense
- So, should we not apply same approach for our recipe in chef
- In chef, if we have to use the better way, lets try to understand little bit of ruby.
- Next Steps:
- Just Enough Ruby for Chef. Refer Here for chef docs on Ruby.
- Test Driven Development of Chef cookbooks using test kitchen.