Installing apache server on ubuntu
- Launch a ubuntu VM
- Login into the VM and execute the following commands
sudo apt update
sudo apt install apache2 -y
- Verify the installation by navigating to public IP
- Verify the installation from command line
Lets do this in Chef
-
Now lets try to create a cookbook in chef which automates the above activity
-
We need a cookbook:
- Navigate to chef-repo\cookbooks folder
- Lets create a chef cookbook Refer Here
- I will be creating a cookbook for installing apache using the following
chef generate cookbook myapache -b --chef-license accept - Navigate to chef-repo\cookbooks folder
-
A new directory will be created. will have some files and folders
-
Chef is developed using ruby language. Chef has its own DSL (Domain Specific Language). If required we can directly use ruby in chef cookbooks.
-
Launch the cookbook in visual studio code, install chef extension
-
Now execute the command
berks install -
In Chef cookbook there is already a recipe created called as default (recipes\default.rb)
-
In chef recipe we will be using resources to automate the manual steps
-
The resources in chef will have following syntax
<resource type> 'name' do
property1 value1
...
propertyn valuen
action <action to acheive>
end
- In recipes\default.rb
apt_update 'ubuntu packages' do
action :update
end
# sudo apt install apache2 -y
apt_package 'apache2' do
action :install
end
- Now upload the recipe to the chef server
berks upload
-
Once the cookbook is uploaded, we need to specify to chef server on which nodes should the recipes be executed.
-
Now we need to change the nodes run_list
-
Do this for the other ubuntu node
-
Now we can wait till the nodes query the server about the work that needs to be done (Convergence)
-
For lab purposes lets manually force the convergence
- login into the node & execute
sudo chef-client
- login into the node & execute
-
Creating and making cookbooks work is the major activity in the cookbook development
-
Uploading cookbook to the server adding it to run_list etc can be done once our cookbooks are working.
-
Next Steps:
- Becoming good at writing cookbooks
- Local development of cookbooks, testing them and upload right versions to server
Cookbook
- Cookbook is fundamental unit of configuration and distribution in chef.
- A cookbook defines a scenario and contains everything needed to support that scenario
- Recipe
- Attributes
- Files
- Templates
- Custom Resources
