Comparing Linux OS Image Update Models - EOSS 2024.pdf
Introduction to chef
1. INTRODUCTION TO CHEF – BY DAMITH KOTHALAWALA ,MBCS
Content Acknowledgement
Most of the internal content are from https://docs.chef.io
CHEF™ logo is a registered trademark of Chef.io
Please do not copy/redistribute without taking prior permission from chef.io
2. About Me I am Damith Rushika Kothalawala and I work for Pearson as a Technical
Specialist. (Application Solutions Engineering)
I currently owns Basic Chef Fluency Badge from CHEF Certification + 4 other
Certifications from different vendors.
Find more about me @: https://www.linkedin.com/in/damithkothalawala/
4. What is Chef
Chef is an open-source systems management and cloud
infrastructure automation framework created by Opscode.
(C) DAMITH RUSHIKA KOTHALAWALA 2017
5. Why named as Chef ??
(C) DAMITH RUSHIKA KOTHALAWALA 2017
6. Traditional Chef
1. Chef creates new recipes after undergoing several experimenting with food.
2. Chef has a book with his favorite recipes, which is called a “Cook Book”
3. One Chef can have many “Cook Books” which contains various types of food.
❖ Chinese Food Cookbook
❖ Cookbook of Soups
❖ Cookbook of Sauces
❖ Traditional Sri Lankan Cuisine Cookbook
4. There can be relationships between two Cookbooks when its comes to food
preparation
❖ Chef may have to refer one recipe of Cookbook of Sauces when making a soup from Cookbook of Soups
➢ Ex. Sometimes you need Soya Sauce to prepare a “Tom Yum” Soup.
5. Chef needs a Kitchen & Knife to do his preparation of food.
(C) DAMITH RUSHIKA KOTHALAWALA 2017
7. So what is the similarity ?
OpsCode’s Chef does the same as a traditional Chef in
theory, but OpsCode chef is for the system/infra
automation.
Thinking about OpsCode’s chef this way will help you to
understand its use well.
(C) DAMITH RUSHIKA KOTHALAWALA 2017
9. CI/CD
Continuous Delivery (CD) is the practice of using
automation to produce releasable software in
short iterations, allowing teams to ship working
software more frequently. The recent emphasis
on continuous integration, built-in testing,
constant monitoring, and analytics feedback all
point towards an overall trend in the software
industry: increasing the ability to react. As
organizations explore what these changes mean
for them, they invariably discover continuous
delivery which is commonly known as CD.
(C) DAMITH RUSHIKA KOTHALAWALA 2017
10. DevOPS
DevOps (a clipped compound of "software DEVelopment" and "information technology
OPerationS") is a term used to refer to a set of practices that emphasize the collaboration and
communication of both software developers and information technology (IT) professionals
while automating the process of software delivery and infrastructure changes – Wikipedia
(C) DAMITH RUSHIKA KOTHALAWALA 2017
11. Let's go back to …
(C) DAMITH RUSHIKA KOTHALAWALA 2017
12. A Single Practical activity is better than
1000 words..
Let’s start learning by doing it ;-)
(C) DAMITH RUSHIKA KOTHALAWALA 2017
13. Download ChefDK (Development Kit)
Please navigate to https://downloads.chef.io/chefdk then download relevant installer for your
OS
(C) DAMITH RUSHIKA KOTHALAWALA 2017
15. Let’s write recipes
As in ordinary cooking, you may have to do some R&D before you write a perfect recipe for your
cookbook. Let's start by writing one.
Something to know before We Start
o Chef software itself is written using a programming language called Ruby
o But you do not need to know about ruby to start working with chef
o Knowledge about a single programming language would be enough to work with Chef’s recipe files.
(C) DAMITH RUSHIKA KOTHALAWALA 2017
18. Let’s review what we have done
We have just created “File Resource” called
/tmp/hello.txt using chef
What is a Resource??
(C) DAMITH RUSHIKA KOTHALAWALA 2017
19. Resource
A resource is a statement of configuration policy. It describes the
desired state of an element of your infrastructure and the steps
needed to bring that item to the desired state.
Source https://docs.chef.io/resources.html
As you know, anything is a file on
Anything on a system is a Resource for
(C) DAMITH RUSHIKA KOTHALAWALA 2017
20. Resource Definition
(C) DAMITH RUSHIKA KOTHALAWALA 2017
file ‘/tmp/hello.txt' do
content 'Hello, world!'
end
The TYPE named NAME should be ACTION'd with PROPERTIES
22. What is chef-client
chef-client is an agent that runs locally on every node that is under
the management of Chef.
When a chef-client runs, it will perform all of the steps that is
required to bring the node into the expected state.
Source: https://docs.chef.io/chef_client.html
(C) DAMITH RUSHIKA KOTHALAWALA 2017
23. --local-mode (or -z)
chef-client's default mode attempts to contact a Chef Server and ask
for the recipes to run for the given node.
We override the behavior in order to have it work in the local mode.
(C) DAMITH RUSHIKA KOTHALAWALA 2017
24. Recap: Apply the hello Recipe
(C) DAMITH RUSHIKA KOTHALAWALA 2017
What would happen if
the 'hello.txt' file
contents were
modified?
26. (C) DAMITH RUSHIKA KOTHALAWALA 2017
What would happen if the file
• permissions (mode)
• Owner
• or group changed?
Have we define a policy for these properties?
27. chef-client’s work on Test & Repair
(C) DAMITH RUSHIKA KOTHALAWALA 2017
chef-client takes an action only when it needs to. Think of
it as test and repair. Chef looks at the current state of each
resource and takes an action only when that resource is out
of policy.
28. Let’s change properties of hello.txt
1. Read https://docs.chef.io/resources.html
2. Discover the file resource's:
◦ default action.
◦ default values for mode, owner, and group.
3. Update the file policy in "hello.rb" to:
The file named 'hello.txt' should be created with the content 'Hello,
world!', mode '0644', owner is 'root', and group is 'root'.
(C) DAMITH RUSHIKA KOTHALAWALA 2017
30. Learn more on resources.
Likewise you can do many things with recipes when it
comes to a real deployment.
So please try to refer chef’s documentation and make some
changes on your local system.
(C) DAMITH RUSHIKA KOTHALAWALA 2017
31. Traditional
Cookbook
As discussed, a real world cookbook is a collection of recipes
which can be
1. An Independent recipe
❑ ex. Steamed Rice Recipe
2. Related to one or more recipes on same cookbook
❑ ex. Fried Rice Recipe (You need streamed rice 1st to make
fried rice)
3. Related to one or more recipes on some other Cookbooks
❑ ex. Chinese Chopsuey Rice (You need. Multiple recipes
from many cookbooks if you make it without using
market products)
(C) DAMITH RUSHIKA KOTHALAWALA 2017
33. OpsCode’s Chef Cookbook
A Chef cookbook is the fundamental unit of configuration and policy
distribution.
Each cookbook defines a scenario, such as everything needed to
install and configure MySQL, and then it contains all of the
components that are required to support that scenario.
Read the first three paragraphs here:
http://docs.chef.io/cookbooks.html
(C) DAMITH RUSHIKA KOTHALAWALA 2017
34. Cookbook
❑Recipes that specify the resources to use and the
order in which they are to be applied
❑ Attribute values
❑ File distributions
❑ Templates
❑ Extensions to Chef, such as libraries, definitions, and
custom resources
❑ Version Control
(C) DAMITH RUSHIKA KOTHALAWALA 2017
75. Most of the Recipes Cannot be
Customized on the fly
This one serves only 4. What happen if you want to make it for 2 or
10 ?
But what if that recipe got an option to customize
as per your requirement ? And adjust the recipe
as per those variables
Ex.
Please Enter Number of Servings
You need 5 baking potatoes
(C) DAMITH RUSHIKA KOTHALAWALA 2017
5
76. Let’s Learn About Chef Node
Attributes
(C) DAMITH RUSHIKA KOTHALAWALA 2017
77. Node Attribute Definition
An attribute is a specific detail about a node. Attributes are used by the chef-client to
understand:
❑The current state of the node
❑What the state of the node was at the end of the previous chef-client run
❑What the state of the node should be at the end of the current chef-client run
Attributes are defined by:
❑The state of the node itself
❑Cookbooks (in attribute files and/or recipes)
❑Roles
❑Environments
(C) DAMITH RUSHIKA KOTHALAWALA 2017
78. Ohai
Ohai is a tool that is used to detect
attributes on a node, and then provide
these attributes to the chef-client at the
start of every chef-client run
(C) DAMITH RUSHIKA KOTHALAWALA 2017
80. Details provided by ohai
Ohai will gather almost everything about your system and present its collected data in JSON
format.
Let's get some selected detail output
ohai memory/total
ohai memory/free
ohai cpu
ohai ipaddress
And etc…
(C) DAMITH RUSHIKA KOTHALAWALA 2017
84. Sometimes we may need to customize any given installation/configuration
based on the type of your development/deployment or installation
requirements.
Examples.
1. Customized installation of a company developed software
2. Configuration files that should be updated on each new application
deployment.
(C) DAMITH RUSHIKA KOTHALAWALA 2017
Cookbook specific node attributes
85. Let’s try with a new Cookbook
(C) DAMITH RUSHIKA KOTHALAWALA 2017
#make sure to be at your $HOME
cd ~
#create new cookbooks called cms
chef generate cookbook cookbooks/cms
#now generate new recipe called default
chef generate recipe cookbooks/cms default
#Let’s create new attribute file
chef generate attribute cookbooks/cms default
86. Let’s declare some node variables
vim cookbooks/cms/attributes/default.rb
#Normally we define cookbook specific node variables in following way
#default[‘cookbookname’][‘var1’]=’value’
default[‘cms’][‘company`]=’cms’
default[‘cms’][‘location’][‘country’]=’Sri Lanka’
default[‘cms’][‘location’][‘city’]=’Colombo 02’
87. Now add new recipe as follows
vim cookbooks/cms/recipes/default.rb
#note “ <-
file ‘/home/damith/nodeinfo.txt’ do
content “
This system is owned by #{node[‘cms’][‘company’]},
#{node[‘cms’][‘location’][‘city’]},#{node[‘cms’][‘location’][‘country’]}
”
end
88. Let’s run this cookbook & look at new
content
sudo chef-client -zr “recipe[cms]”
cat ~/hostinfo.txt
#and see what is there
89. How override node attributes
#this is quite simple via json. See following example
vim customer.json
{
“cms”: {
“company”: “Pearson”,
“location”: {
“city”: “Colombo 09”
}
}
}
90. Now run recipe with customer json
sudo chef-client -zr “recipe[cms]” -j customer.json
cat ~/hostinfo.txt
#and see what is there
91. Let’s Add a Template!
Adding all the information into the recipe did make it hard to read.
Objectives
❑ Create a template with chef generate
❑ Define the contents of the ERB template
❑ Change the file resource to the template resource ❑ Update the cookbook's version
number
❑ Apply the updated recipe and verify the results
92. Let’s add new template file
#make sure to be at your $HOME
cd ~
#create new cookbooks called cms
chef generate cookbook cookbooks/cms
#now generate new recipe called default
chef generate recipe cookbooks/cms default
#Let’s create new attribute file
chef generate attribute cookbooks/cms default
#make sure to be at your $HOME
cd ~
#now generate new template called hostinfo
chef generate template cookbooks/cms hostinfo
#now check cookbook tree using
tree cookbooks/cms
93. What is ERB ?
An Embedded Ruby (ERB) template allows Ruby
code to be embedded inside a text file within
specially formatted tags. Ruby code can be
embedded using expressions and statements.
Learn more at : https://docs.chef.io/templates.html#variables
94. text within an ERB template
vi cookbooks/cms/templates/default/hostinfo.erb
~
Host Information
================
This system is owned by <%=node[‘cms’][‘company’]%>,
<%=node[‘cms’][‘location’][‘city’]%>,<%=node[‘cms’][‘location’][‘country
’]%>
95. Using Template on Recipe
vim cookbooks/cms/recipes/default.rb
#note
file ‘/home/damith/nodeinfo.txt’ do
source ‘hostinfo.erb`
end
Use of templates will help us to have more cleaner
codes on recipes.