CHEF

From open-voip.org

Jump to: navigation, search

Contents

Terms

  • Organizations
  • Environments
  • node - An ordered list of roles and/or recipes that are run in the exact orde
  • resource - package, template, service, user, group
  • recipe - collection of resources, defined using patterns (resource names, attribute-value pairs, and actions)
  • cookbook - A cookbook is the fundamental unit of configuration and policy distribution. A cookbook defines a scenario and contains everything that is required to support that scenario:
  * 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
  • run list - A run-list defines all of the information necessary for Chef to configure a node into the desired state
  * An ordered list of roles and/or recipes that are run in the exact order
  • roles -Each role consists of zero (or more) attributes and a run-list. Each node can have zero (or more) roles assigned to it.
  • search

Install

hosted chef:

https://www.getchef.com/chef/choose-your-version/

download the started kit

https://manage.opscode.com/organizations/sportzone/getting_started
http://www.getchef.com/chef/install

getchef client

chef server

C:\chef-repo\.chef\knife.rb

install chef-client on Amazon AWS linux API (debian)

url -L https://www.opscode.com/chef/install.sh | sudo bash
or
curl -L https://www.opscode.com/chef/install.sh | bash /dev/stdin -v 11.4.4

install (bootstrap) on remote node:

knife bootstrap uvo1eXXXXXXXXr50m5pq.vm.cld.sr -x root -P Ni92nf24nd -N module2

install (bootstrap) a remote note with a role and environment:

knife bootstrap uvo1eXXXXXXXXr50m5pq.vm.cld.sr -x root -P Ni92nf24nd -N module2 -r 'role[webserver]' -E production

cookbook recipe and run list

create a cookbook:

 knife cookbook create apache

example of recipes

package httpd do 
 action :install
end
service "httpd" do
 actopn [:enable, :start]
end
template "/var/www/html/index.html" do
 source "index.html.erb"
 mode "0644"
end

yum install

yum_package "java-1.7.0-openjdk-src.x86_64" do 
 action :install
end

upload cookbook to the chef server:

knife cookbook upload apache

upload all the cookbooks in one command:

knife upload cookbooks 

module2 server has a new run_list with the "apache" recepi

knife node run_list add module2 "recipe[apache]"

remove a run_list from a server

knife node run list remove module2 "recipe[apache]"

run chef-client on a node

sudo chef-client

cookbook version

compare current cookbook with server version

knife diff .\cookbooks\apache

change the file version in cookbook\apache\templates\metadata.erb

version        0.2.0

using Ruby variables - ohai attributes

OHAI command - all the info about the node - on the client

ohai
ohai hostname
ohahi memory

My Name is <%= node['hostname'] %>.

on the managment:

knife node show ip-172-30-0-201

-l to show long details

knife node show ip-172-30-0-201 -l

-a to see the attributes of a node

knife node show ip-172-30-0-201 -a hostname
knife node show ip-172-30-0-201 -a memory

custom Attributes

you can set attributes in:

  • roles
  • env
  • cookbook recipe
  • cookbook attribute file

cookbook attributes

in the cookbook attributes file

./cookbook/<cookbook>/attributes/default.rb
default['apache']['dir'] = "/etc/apache2"
default['apache']['greeting'] = "World"

use:

Hello, <%= node['apache']['greeting'] %>.

Roles

more info at

http://docs.opscode.com/essentials_roles.html

role file (webserver.json)

{ 
 "name":"webserver",
 "default_attributes"{
  "apache":{
   "greeting":"webinar"
  }
 },
 "run_list":[
  "recipe[apache]"
 ]
}

update the role file in the server

knife role from file webserver.json

add role to a server

knife node run list add module3 "role[webserver]"

run script and script file

  • run script file
cookbook_file "/tmp/lib-installer.sh" do
 source "lib-installer.sh"
 mode 0755
end

execute "install my lib" do
 command "sh /tmp/lib-installer.sh"
end
  • run script
script "install_something" do
 interpreter "bash"
 user "root"
 cwd "/tmp"
 code <<-EOH
   #insert bash script
 EOH
end
  • run python script
filename = "test.py"
 
execute 'execute_file' do
 cwd '/home/peeyush'
 command "python #{filename}"
end

Environments in an Organization

  • each cookbook can have versions in the metadata.rb files
  • each environment defined the version of each cookbook
  • each node belong to a specific environment

Organization: Production, Development, Staging ...

knife environment show _default3

to see the apache cookbook versions

knife cookbook show apache

list of environments:

knife environment list

create an environment, with a specific version of cookbook:

in the chef-repo path create a new folder called environments and create a file called "dev.rb" in this file set:
name "dev"
description "development"
cookbook "apache", "= 0.2.0"

update the new environment to teh server:

knife environment from file dev.rb
knife environment list
knife cookbook show dev

In order to change the environment of a node you can change it from the Chef server web managment bootstrap a node with an environment

knife bootstrap uvo1eXXXXXXXXr50m5pq.vm.cld.sr -x root -P Ni92nf24nd -N module2 -r 'role[webserver]' -E production

edit environment of a node

knife node environment_set NODE_NAME ENVIRONMENT_NAME (options)

search

  • by any attribute, ohai and others:
knife search node "platform:centos"

knife ec2

install

sudo gem install knife ec2

in powershell:

gem install knife-ec2
gem install knife-windows

add the AWS account credentials to a file : add the following to the chef-repo/.chef/knife.rb file:

# Amazon AWS
knife[:aws_access_key_id] = "AWS_ACCESS_KEY_ID"
knife[:aws_secret_access_key] = "AWS_SECRET_ACCESS_KEY"
# knife[:aws_ssh_key_id] = "openvoip2"

list of servers

knife ec2 server list --region eu-west1

create ec2 instance in a vpc if the AWS cerdentails are updated in the knife.rb files:

 knife ec2 server create -I ami-6e7bd919 --region eu-west-1 --flavor t2.micro --subnet subnet-83293ac5
 knife ec2 server create -I ami-6e7bd919 -N MyEc2Instance -x ec2-user -r "role[webserver]" --region eu-west-1

run chef command on remote nodes by search creteria

 knife ssh "role:webserver" "sudo chef-client" -x ec2-user

chef supermarket

install community cookboob - chef client that runs chef-client as a service

knife cookbook site download chef-client

in the supermarket.chef.io site you will see that there are dependencies - install them too :

knife cookbook site download cron
knife cookbook site download logrotate

extract

tar -zxvf chef-client.x.x.tar.gz -C cookbooks
tar -zxvf cron.x.x.tar.gz -C cookbooks
tar -zxvf logrotate.x.tar.gz -C cookbooks
rm -rf *.gz

upload

 knife upload cookbooks
Personal tools