SlideShare a Scribd company logo
Configuration management with
Chef
Collections of Resources
                 • Routes
                 • Users
• Networking     • Groups
• Files          • Tasks
• Directories    • Packages
• Symlinks       • Software
• Mounts         • Services
                 • Configurations
                 • Other Stuff
Declarative Interface to Resources


➔
 Define policy
➔
 Say what, not how
➔
 Pull not Push
Search

➔
 Search for nodes with Roles
➔
 Find configuration data
➔
 IP addresses
➔
 Hostnames
➔
 FQDNs
Pass Results to Templates
pool_members = search("node","role:webserver”)
template "/etc/haproxy/haproxy.cfg" do
source "haproxy-app_lb.cfg.erb"
owner "root"
group "root"
mode 0644
variables :pool_members => pool_members.uniq
notifies :restart, "service[haproxy]"
end
Pass Results to Templates
# Set up application listeners here.
listen application 0.0.0.0:80
balance roundrobin
<% @pool_members.each do |member| -%>
server <%= member[:hostname] %> <%=
member[:ipaddress] %>:> weight 1 maxconn 1
check
<% end -%>
<% if node["haproxy"]["enable_admin"] -%>
listen admin 0.0.0.0:22002
mode http
stats uri /
<% end -%>
Attributes
➔
 OS attributes provided by ohai
➔
 Other attributes are configured by the installed
cookbooks
 Attributes are mutable
➔
 attributes — variables
➔
 recipes — list of instructions (“resources”)
➔
 files — files used by resources
➔
 templates — ERB templates
➔
 definitions — macros of resources
➔
 libraries — Ruby to extend Chef DSL
recipes/default.rb
template “/tmp/hello_world.txt” do
source “hello_world.txt.erb”
variables :my_name => node[:my_name]
mode 00664
action :create
end
Simple attribute
attributes/my_name.rb
my_name “Juan Vicente”
templates/default/hello_world.txt.erb
Hello, <%= @my_name %>, how are you
today?
Add the recipe to the node’s recipe list
• Invoke chef-client
• Default chef-client setup has client invoked
periodically
When chef-client runs
• Node authenticates with server
• Libraries, attributes, definitions & recipes
are synchronized
• Libraries, attributes, definitions & recipes
compiled
• Node state is converged
• Everything happens on the node
 May be simply defined, e.g.
my_name “Juan Vicente”
• Allow overriding, e.g. unless attribute?
my_name “Juan Vicente”
(“my_name”)
• List values are regular array
Resources
• The steps that make up a recipe
package “git-core” do
action :install
end
• Resources are implemented via Providers
Package
package "tar" do
version "1.16.1-1"
action :install
end
• Action can be install, upgrade, remove,
purge
• Version is optional
Remote files
• Copying remote files is easy
remote_file “/tmp/foo.png” do
source “foo.png”
owner “root”
group “root”
mode 0444
action :create
end
• Where does the file live?
Files and templates are searched for in the
following order: FQDN, platform-version,
platform, default
• For Ubuntu 12.10:
myhost.example.com
ubuntu-9.04
ubuntu
Default
More remote file fun
• File source can be a URL
source “http://warez.com/thing.tgz”
• Provide SHA256 hash to prevent needless
downloading from chef-server each time
checksum “08da0021”
Useful things
Control existence and attributes of a file,not its contents
file “/tmp/whatever” do
owner “root”
group “root”
mode “0644”
action :create
end
• Other actions are touch, delete
directory
— analog of the File resource
remote_directory
— recursive remote
copy
Useful things
Control system services from /etc/init.d and friends
• We can en/disable, start, stop & restart
service “my_daemon” do
supports :restart => true
action [ :enable, :start ]
End
User
• Group
• Cron
• Route
• Mount
Useful things

Execute arbitrary command
command “mysql-stuff” do
execute “/usr/bin/mysql </tmp/foo.sql”
creates “/tmp/outfile.sql”
environment {‘FOO’ => “bar”}
action :run
end
Useful things

bash, perl, python, ruby, csh
bash “install_foo” do
user “root”
cwd “/tmp”
code <<-EOC
wget http://example.org/foo.tgz
tar xvf foo.tgz && cd foo
./configure && make install
EOC
end
Notifies
• Chain actions
template “/etc/my_daemon/my.cnf” do
source “my.cnf.erb”
notifies :restart,
resources(:service => “my_daemon”)
end
• By default, notification postponed until end of run, add :immediately as
final argument to override
Action :nothing
• If you want a resource to run only on a notify, specify action
:nothing
execute "index-gem-repository" do
command "gem generate_index -d /srv/
gems"
action :nothing
end
Notifies
Useful for connecting to existing services
http_request “say_hello” do
url “http://myserv.local/check_in”
message :node => node[:fqdn]
action :post
end
Overriding attributes
• In cookbook, easy enough to set a default
• Per-node customizations can be made in the UI
• To set new defaults, override selectively in site-cookbooks




              Conditional resources
• Use only_if and not_if to control resource execution
• Takes either shell commands or Ruby
blocks, e.g.
only_if do
IO.read(“/tmp/foo”).chomp == ‘bar’
end
Chef attributes can be overridden at multiple levels of organization, and
we can normalize our configuration items (e.g. node attributes in Chef)
into cookbook, environment, role or node defaults and overrides. Here
is the actual node attribute precedence from low to high:
cookbook default < environment default < role default < node default <
cookbook set < node set < cookbook override < role override <
environment override < node override

By using this precedence rule, we can configure node attributes across
our entire Chef environment with a single configuration change, or
override one specific node’s attribute without making changes to the
rest of the environment.
Data Bag
A data bag stores arbitrary information about the infrastructure in a
nested hash structure. Just like any other Chef objects, it can be
accessed via RESTful API. A data bag does not belong to a specific
Chef environment, so it should be used to store truly global
configuration items. You can also encrypt a data bag to store
sensitive information that you need to keep out of your source code
repository.

For example root path for jboss, mysql, tomcat, applications... to
have the same path in all of the nodes
Example: The Dev Environment
{ "name": "dev",

"default_attributes": {   "apache2":

{   "listen_ports": [     "80",        "443"   ]   } },

"json_class": "Chef::Environment",

"description": "",

"cookbook_versions": {     "couchdb": "= 11.0.0" },

"chef_type": "environment"}
Example: The Dev Environment
{ "name": "webserver",

"default_attributes": { },

"json_class": "Chef::Role",

"env_run_lists": { "_default": [    ], "production": [ ],   "preprod":
[ ], "test": [     "role[base]",    "recipe[apache]" ],

 "dev":
[    "role[base]",    "recipe[apache]",   "recipe[apache::copy_dev_con
figs]" ] },

"run_list": [ "role[base]", "recipe[apache]" ], "description": "The
webserver role", "chef_type": "role", "override_attributes": { }}
Using environments within recipes
To have different behaviour depending on the environment, use
the "chef_environment" method of the node object. This is a
Ruby method, not a Chef attribute. For example:
file "/opt/data/testfile1.txt" do
mode "0644"
content "A sample file."
only_if { node.chef_environment == "dev"}end
Author
●
    Juan Vicente Herrera Ruiz de Alejo
●
    Juan.herrera@lumatagroup.com
●
    http://juanvicenteherrera.eu
●
    @jvicenteherrera
●
    Skype: jvherrera.quimerus.es

More Related Content

PPTX
Software Containerization
PDF
Azure web apps
PDF
Az 104 session 4: azure storage
PPTX
Docker: From Zero to Hero
PPTX
Windows Azure Virtual Machines
PDF
Best Practices with Azure Kubernetes Services
PPTX
Container orchestration overview
PDF
Intro docker
Software Containerization
Azure web apps
Az 104 session 4: azure storage
Docker: From Zero to Hero
Windows Azure Virtual Machines
Best Practices with Azure Kubernetes Services
Container orchestration overview
Intro docker

What's hot (20)

PDF
Docker 101: An Introduction
PDF
Az 104 session 3 azure compute
PDF
Terraform -- Infrastructure as Code
PDF
Curso Kubernetes CodeURJC
PDF
Azure 101
PPTX
Docker Container Security
PDF
Roles and Responsibilities of a DevOps Engineer
PPTX
A Deepdive into Azure Networking
PPTX
CAF presentation 09 16-2020
PPTX
Overview of Azure Arc enabled Kubernetes
PPTX
Discovering the 2 in Alfresco Search Services 2.0
PDF
Az 104 session 6 azure networking part2
PDF
Az 104 session 5: Azure networking
PPTX
Docker introduction
PPTX
Azure SQL Database Managed Instance
PPTX
Azure SQL Database & Azure SQL Data Warehouse
PPTX
Govern your Azure environment through Azure Policy
PDF
Kubernetes vs Docker Swarm | Container Orchestration War | Kubernetes Trainin...
PPTX
Disaster Recovery Using Azure Services
PPTX
Azure App Service Deep Dive
Docker 101: An Introduction
Az 104 session 3 azure compute
Terraform -- Infrastructure as Code
Curso Kubernetes CodeURJC
Azure 101
Docker Container Security
Roles and Responsibilities of a DevOps Engineer
A Deepdive into Azure Networking
CAF presentation 09 16-2020
Overview of Azure Arc enabled Kubernetes
Discovering the 2 in Alfresco Search Services 2.0
Az 104 session 6 azure networking part2
Az 104 session 5: Azure networking
Docker introduction
Azure SQL Database Managed Instance
Azure SQL Database & Azure SQL Data Warehouse
Govern your Azure environment through Azure Policy
Kubernetes vs Docker Swarm | Container Orchestration War | Kubernetes Trainin...
Disaster Recovery Using Azure Services
Azure App Service Deep Dive
Ad

Similar to Configuration management with Chef (20)

PDF
Chef Fundamentals Training Series Module 2: Workstation Setup
PDF
Chef Fundamentals Training Series Module 3: Setting up Nodes and Cookbook Aut...
PDF
Velocity 2011 Chef OpenStack Workshop
PDF
Introduction to Chef
KEY
Picconf12
PDF
Real world Django deployment using Chef
PDF
Chef or how to make computers do the work for us
ZIP
Rails 3 (beta) Roundup
PDF
Cooking 5 Star Infrastructure with Chef
PDF
Node object and roles - Fundamentals Webinar Series Part 3
PPTX
Configuration Management in the Cloud - Cloud Phoenix Meetup Feb 2014
PDF
Introduction to Chef
PPTX
Infrastructure modeling with chef
PDF
Common configuration with Data Bags - Fundamentals Webinar Series Part 4
PDF
BP-6 Repository Customization Best Practices
KEY
SELF 2011: Deploying Django Application Stacks with Chef
PDF
A tour of Ansible
PDF
20090514 Introducing Puppet To Sasag
KEY
20100425 Configuration Management With Puppet Lfnw
PDF
Practical Chef and Capistrano for Your Rails App
Chef Fundamentals Training Series Module 2: Workstation Setup
Chef Fundamentals Training Series Module 3: Setting up Nodes and Cookbook Aut...
Velocity 2011 Chef OpenStack Workshop
Introduction to Chef
Picconf12
Real world Django deployment using Chef
Chef or how to make computers do the work for us
Rails 3 (beta) Roundup
Cooking 5 Star Infrastructure with Chef
Node object and roles - Fundamentals Webinar Series Part 3
Configuration Management in the Cloud - Cloud Phoenix Meetup Feb 2014
Introduction to Chef
Infrastructure modeling with chef
Common configuration with Data Bags - Fundamentals Webinar Series Part 4
BP-6 Repository Customization Best Practices
SELF 2011: Deploying Django Application Stacks with Chef
A tour of Ansible
20090514 Introducing Puppet To Sasag
20100425 Configuration Management With Puppet Lfnw
Practical Chef and Capistrano for Your Rails App
Ad

More from Juan Vicente Herrera Ruiz de Alejo (20)

PDF
Un puente enre MLops y Devops con Openshift AI
PDF
Practical Threat Modeling - WorldParty 2k23 HackMadrid.pdf
PDF
PDF
OpenShift Multicluster
PDF
Deploying Minecraft with Ansible
PDF
Tell me how you provision and I'll tell you how you are
PDF
Santander DevopsandCloudDays 2021 - Hardening containers.pdf
PDF
X by orange; una telco en la nube
PDF
Dorsal carrera de la mujer ROSAE 2017
PDF
Cartel carrera de la mujer ROSAE 2017
PDF
Volkswagen Prague Marathon 2017
PDF
Plan de entrenamiento Maratón de Madrid Mes 3
PDF
Plan de entrenamiento Maratón de Madrid Mes 2
PDF
Plan de entrenamiento Maratón de Madrid Mes 1
PDF
Cartel carrera de la mujer ROSAE 2014
PPT
AWS migration: getting to Data Center heaven with AWS and Chef
ODP
Devops madrid: successful case in AWS
ODP
Devops Madrid Marzo - Caso de uso en AWS
ODP
DevOps and Chef improve your life
ODP
MongoDB Devops Madrid February 2012
Un puente enre MLops y Devops con Openshift AI
Practical Threat Modeling - WorldParty 2k23 HackMadrid.pdf
OpenShift Multicluster
Deploying Minecraft with Ansible
Tell me how you provision and I'll tell you how you are
Santander DevopsandCloudDays 2021 - Hardening containers.pdf
X by orange; una telco en la nube
Dorsal carrera de la mujer ROSAE 2017
Cartel carrera de la mujer ROSAE 2017
Volkswagen Prague Marathon 2017
Plan de entrenamiento Maratón de Madrid Mes 3
Plan de entrenamiento Maratón de Madrid Mes 2
Plan de entrenamiento Maratón de Madrid Mes 1
Cartel carrera de la mujer ROSAE 2014
AWS migration: getting to Data Center heaven with AWS and Chef
Devops madrid: successful case in AWS
Devops Madrid Marzo - Caso de uso en AWS
DevOps and Chef improve your life
MongoDB Devops Madrid February 2012

Recently uploaded (20)

PPTX
A Presentation on Artificial Intelligence
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Approach and Philosophy of On baking technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Modernizing your data center with Dell and AMD
PPTX
Cloud computing and distributed systems.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
A Presentation on Artificial Intelligence
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectral efficient network and resource selection model in 5G networks
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The AUB Centre for AI in Media Proposal.docx
Network Security Unit 5.pdf for BCA BBA.
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Dropbox Q2 2025 Financial Results & Investor Presentation
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation theory and applications.pdf
Approach and Philosophy of On baking technology
“AI and Expert System Decision Support & Business Intelligence Systems”
MYSQL Presentation for SQL database connectivity
Modernizing your data center with Dell and AMD
Cloud computing and distributed systems.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx

Configuration management with Chef

  • 2. Collections of Resources • Routes • Users • Networking • Groups • Files • Tasks • Directories • Packages • Symlinks • Software • Mounts • Services • Configurations • Other Stuff
  • 3. Declarative Interface to Resources ➔ Define policy ➔ Say what, not how ➔ Pull not Push
  • 4. Search ➔ Search for nodes with Roles ➔ Find configuration data ➔ IP addresses ➔ Hostnames ➔ FQDNs
  • 5. Pass Results to Templates pool_members = search("node","role:webserver”) template "/etc/haproxy/haproxy.cfg" do source "haproxy-app_lb.cfg.erb" owner "root" group "root" mode 0644 variables :pool_members => pool_members.uniq notifies :restart, "service[haproxy]" end
  • 6. Pass Results to Templates # Set up application listeners here. listen application 0.0.0.0:80 balance roundrobin <% @pool_members.each do |member| -%> server <%= member[:hostname] %> <%= member[:ipaddress] %>:> weight 1 maxconn 1 check <% end -%> <% if node["haproxy"]["enable_admin"] -%> listen admin 0.0.0.0:22002 mode http stats uri / <% end -%>
  • 7. Attributes ➔ OS attributes provided by ohai ➔ Other attributes are configured by the installed cookbooks Attributes are mutable ➔ attributes — variables ➔ recipes — list of instructions (“resources”) ➔ files — files used by resources ➔ templates — ERB templates ➔ definitions — macros of resources ➔ libraries — Ruby to extend Chef DSL
  • 8. recipes/default.rb template “/tmp/hello_world.txt” do source “hello_world.txt.erb” variables :my_name => node[:my_name] mode 00664 action :create end Simple attribute attributes/my_name.rb my_name “Juan Vicente” templates/default/hello_world.txt.erb Hello, <%= @my_name %>, how are you today? Add the recipe to the node’s recipe list • Invoke chef-client • Default chef-client setup has client invoked periodically
  • 9. When chef-client runs • Node authenticates with server • Libraries, attributes, definitions & recipes are synchronized • Libraries, attributes, definitions & recipes compiled • Node state is converged • Everything happens on the node May be simply defined, e.g. my_name “Juan Vicente” • Allow overriding, e.g. unless attribute? my_name “Juan Vicente” (“my_name”) • List values are regular array
  • 10. Resources • The steps that make up a recipe package “git-core” do action :install end • Resources are implemented via Providers Package package "tar" do version "1.16.1-1" action :install end • Action can be install, upgrade, remove, purge • Version is optional
  • 11. Remote files • Copying remote files is easy remote_file “/tmp/foo.png” do source “foo.png” owner “root” group “root” mode 0444 action :create end • Where does the file live?
  • 12. Files and templates are searched for in the following order: FQDN, platform-version, platform, default • For Ubuntu 12.10: myhost.example.com ubuntu-9.04 ubuntu Default More remote file fun • File source can be a URL source “http://warez.com/thing.tgz” • Provide SHA256 hash to prevent needless downloading from chef-server each time checksum “08da0021”
  • 13. Useful things Control existence and attributes of a file,not its contents file “/tmp/whatever” do owner “root” group “root” mode “0644” action :create end • Other actions are touch, delete directory — analog of the File resource remote_directory — recursive remote copy
  • 14. Useful things Control system services from /etc/init.d and friends • We can en/disable, start, stop & restart service “my_daemon” do supports :restart => true action [ :enable, :start ] End User • Group • Cron • Route • Mount
  • 15. Useful things Execute arbitrary command command “mysql-stuff” do execute “/usr/bin/mysql </tmp/foo.sql” creates “/tmp/outfile.sql” environment {‘FOO’ => “bar”} action :run end
  • 16. Useful things bash, perl, python, ruby, csh bash “install_foo” do user “root” cwd “/tmp” code <<-EOC wget http://example.org/foo.tgz tar xvf foo.tgz && cd foo ./configure && make install EOC end
  • 17. Notifies • Chain actions template “/etc/my_daemon/my.cnf” do source “my.cnf.erb” notifies :restart, resources(:service => “my_daemon”) end • By default, notification postponed until end of run, add :immediately as final argument to override Action :nothing • If you want a resource to run only on a notify, specify action :nothing execute "index-gem-repository" do command "gem generate_index -d /srv/ gems" action :nothing end
  • 18. Notifies Useful for connecting to existing services http_request “say_hello” do url “http://myserv.local/check_in” message :node => node[:fqdn] action :post end
  • 19. Overriding attributes • In cookbook, easy enough to set a default • Per-node customizations can be made in the UI • To set new defaults, override selectively in site-cookbooks Conditional resources • Use only_if and not_if to control resource execution • Takes either shell commands or Ruby blocks, e.g. only_if do IO.read(“/tmp/foo”).chomp == ‘bar’ end
  • 20. Chef attributes can be overridden at multiple levels of organization, and we can normalize our configuration items (e.g. node attributes in Chef) into cookbook, environment, role or node defaults and overrides. Here is the actual node attribute precedence from low to high: cookbook default < environment default < role default < node default < cookbook set < node set < cookbook override < role override < environment override < node override By using this precedence rule, we can configure node attributes across our entire Chef environment with a single configuration change, or override one specific node’s attribute without making changes to the rest of the environment.
  • 21. Data Bag A data bag stores arbitrary information about the infrastructure in a nested hash structure. Just like any other Chef objects, it can be accessed via RESTful API. A data bag does not belong to a specific Chef environment, so it should be used to store truly global configuration items. You can also encrypt a data bag to store sensitive information that you need to keep out of your source code repository. For example root path for jboss, mysql, tomcat, applications... to have the same path in all of the nodes
  • 22. Example: The Dev Environment { "name": "dev", "default_attributes": { "apache2": { "listen_ports": [ "80", "443" ] } }, "json_class": "Chef::Environment", "description": "", "cookbook_versions": { "couchdb": "= 11.0.0" }, "chef_type": "environment"}
  • 23. Example: The Dev Environment { "name": "webserver", "default_attributes": { }, "json_class": "Chef::Role", "env_run_lists": { "_default": [ ], "production": [ ], "preprod": [ ], "test": [ "role[base]", "recipe[apache]" ], "dev": [ "role[base]", "recipe[apache]", "recipe[apache::copy_dev_con figs]" ] }, "run_list": [ "role[base]", "recipe[apache]" ], "description": "The webserver role", "chef_type": "role", "override_attributes": { }}
  • 24. Using environments within recipes To have different behaviour depending on the environment, use the "chef_environment" method of the node object. This is a Ruby method, not a Chef attribute. For example: file "/opt/data/testfile1.txt" do mode "0644" content "A sample file." only_if { node.chef_environment == "dev"}end
  • 25. Author ● Juan Vicente Herrera Ruiz de Alejo ● Juan.herrera@lumatagroup.com ● http://juanvicenteherrera.eu ● @jvicenteherrera ● Skype: jvherrera.quimerus.es