Chef Solo Basics : Developer Notes

Objective


To learn the basic Chef concepts using Chef Solo in Ubuntu 12.04.5 LTS.

Steps


Step 1

cat /etc/lsb-release

Step 2

chef-solo -v

Step 3

sudo su

Step 4

curl -L https://www.opscode.com/chef/install.sh | bash

Step 5

chef-solo -v

The Chef version installed on my Ubuntu VM is 12.3.

Step 6

$mkdir /etc/chef
$cd /etc/chef
$vi solo.rb

Add:

repo = '/etc/chef/src'

cookbook_path repo + '/cookbooks'
data_bag_path repo + '/data_bags'
role_path     repo + '/roles'

log_level :info
log_location STDOUT

Run:

cat solo.rb

Step 7

mkdir src
cd src/
mkdir cookbooks data_bags roles
cd cookbooks
mkdir common
cd common
mkdir recipes

Step 8

cd recipes
vi default.rb

Add:

directory '/root' do
  mode '0700'
  user 'root'
  group 'root'
end

Step 9

chef-solo -o 'recipe[common]'
chmod 760 /root
chef-solo -o 'recipe[common]'

Step 10

cat /etc/motd

Go to chef/src/cookbooks/common directory

$mkdir -p files/default
$vi files/default/motd

Add :

This is a test.

Step 11

vi recipes/default.rb 

Add the following to the bottom of the file.

cookbook_file '/etc/motd' do
  mode '0644'
  user 'root'
  group 'root'
end

Run:

cat recipes/default.rb

Step 12

$chef-solo -o 'recipe[common]'
$cat /etc/motd

Step 13

vi recipes/packages.rb

Add:

package 'whoopsie' do
  action :remove
end

Run:

$chef-solo -o 'recipe[common::packages]'

Step 14

vi recipes/default.rb

Add:

include_recipe 'common::packages'

at the top

chef-solo -o 'recipe[common]'

Step 15

rm /etc/motd
chef-solo -o 'recipe[common]'

Step 16

dpkg -l | grep landsca
vi recipes/packages.rb

Add:

package 'landscape-client-ui-install' do
  action :remove
end

Run:

chef-solo -o 'recipe[common]'

Step 17

ruby -v
vi recipes/packages.rb

Add:

package 'ruby1.9.1'

Run:

chef-solo -o 'recipe[common]'
ruby -v
apt-get install whoopsie
chef-solo -o 'recipe[common]'
dpkg -l | grep ruby

Step 18

cd ..
mkdir apache
cd apache
mkdir attributes
mkdir templates
mkdir recipes
mkdir templates/default
vi attributes/default.rb

Add:

node.default['apache']['srvlimit'] = 128

Edit:

vi recipes/default.rb

Add:

log node['apache']['srvlimit']

cat attributes/default.rb
cat recipes/default.rb

Step 19

chef-solo -o 'recipe[apache]'

vi recipes/default.rb

Add:

package 'apache2'

service 'apache2' do
  action :start
end

Run:

chef-solo -o 'recipe[apache]'
service apache2 stop
chef-solo -o 'recipe[apache]'

Step 20

vi templates/default/serverlimit.conf.erb

Add:

ServerLimit <%= node['apache']['srvlimit'] %>
MaxClients <%= node['apache']['srvlimit'] %>

Edit:

vi recipes/default.rb

Add:

template '/etc/apache2/conf.d/serverlimit.conf' do
  mode '0644'
  user 'root'
  group 'root'
  notifies :restart, 'service[apache2]'
end

Step 21

chef-solo -o 'recipe[apache]'

cat /etc/apache2/conf.d/serverlimit.conf

echo bidon > /etc/apache2/conf.d/serverlimit.conf

cat /etc/apache2/conf.d/serverlimit.conf

chef-solo -o 'recipe[apache]'

cat /etc/apache2/conf.d/serverlimit.conf

Step 22

Go to src directory.

cd roles
vi webpetit.rb

Add:

default_attributes(
 'apache' => {
   'srvlimit' => 64
 }
 )

run_list(
  'recipe[common]',
  'recipe[apache]'
)

Run:

chef-solo -o 'role[webpetit]'

Step 23

cp webpetit.rb webgros.rb
vi webgros.rb

Add:

default_attributes(
  'apache' => {
    'srvlimit' => 1024
  }
)

run_list(
 'recipe[common]',
 'recipe[apache]'
)

Run:

$chef-solo -o 'role[webgros]'

Summary


In this article, you learned some of the basic concepts about Chef Solo.

Reference


Chef Tutorial on Ubuntu Linux


Related Articles


Create your own user feedback survey