Skip to content

bbourqu/chef-training

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 

Repository files navigation

What I want to learn

  • Staying OS Agnostic?
  • Testing well

Notes in no particular order

package 'httpd'  

installs latest version of that package

service 'ntp' do
  action [ :enable, :start ]
end

enabled on reboot and started

file '/etc/motd' do
  content 'This computer is the property ...'
end

a file created with the content defined

 action: delete

will delete the file

chef-apply --help will send out the help block
chef-apply -e execute inline command
chef-apply <file> run the recipe

File Resource Information

Declare out the attributes that make sense eg. action :create


chef --help will send out the help block chef generate <name> generate a new app, cookbook, or component

Chef Cookbook Information

Examples

chef generate cookbook apache
chef generate recipe server


Ensure you are giting everything
Read the README.md first


chef-apply is for a one-off on recipes chef-client is an agent that performs all the steps on a node rather than 'one-off'

chef-client --local-mode -r "recipe[apach::server]"

running the 'server' recipe from the 'apache' cookbook from the root dir of your cookbook repo

NOTE: the chef-client assumes /home/chef unless you have a cookbooks directory

chef-client --local-mode -r "recipe[apach::server],recipe[workstation::setup]"

This command runs multiple recipes NOTE: ensure there is no space around the comma(,)


include_recipe

include_recipe 'workstation::setup'

runs the recipe here. runs in the order provided if multiple include_recipes are there

Testing Cookbooks

  • ChefDK
  • RuboCop
  • kitchen
  • rubocop

  • Write test case first
  • Run virtual machine and fail
  • Write code
  • Run virtual machine and succeed
  • Destroy virtual machine

kitchen create -> kitchen converge -> kitchen verify -> kitchen destroy

kitchen init
kitchen converge [INSTANCE|REGEX|all]

NOTE: converge doesn't kill the machine

kitchen verify [INSTANCE|REGEXP|all]

verifies the state of the machine after the cookbook runs

kitchen destroy

destroys a test instance

kitchen test

runs kitchen destroy -> kitchen create -> kitchen converge -> kitchen verify -> kitchen destroy in that order

ServerSpec

Example

describe package('tree') do
  it { should be_installed }
end

NOTE: there is a long directory where these specs live, see Writing Test


Capturing Information about the System

utilize ohai to gather information about your system as a JSON object

Example

#{node['ipaddress']}

Templates

Use when we have formatted string interpolation in a file that will end up on the node

More information can be found at Template Resource

found at cookbooks/<cookbook>/templates/default/

Example of generating

chef generate template index.html

generates the file index.html.erb in templates/default/ under the cookbook

<%= is known as a "angry squid/squid-rocket"

now from #{node['ipaddress']} to <%= node['ipaddress'] %>

Chef Server

  1. Provision Server
  2. Install Chef
  3. Copy Web Server cookbook
  4. Apply the cookbook

Hosted Chef

Sign up for an account

  • Download Starter Kit
  • Allow the Keys for your org to be reset

Repo of all the cookbooks we need

knife - command-line tool...interfaces between local and Chef Server

berks - command-line tool...uploads cookbooks to the Chef Server

knife bootstrap - installs chef tools if they are not already installed; configures to communicate with the Chef Server; runs chef-client to apply a default run list

knife node run_list add <node_name> "recipe[<some_recipe>]" - add a recipe to the run list for a node

Community Cookbooks

Supermarket

"Wrapper Cookbook" - add new attributes while keeping the core functionality of the original cookbook (Perferred)

╭─user@host  ~/chef-repo/cookbooks/myhaproxy ‹system› ‹master*›
╰─$ knife node show node1 -a ipaddress                                                                                                                                                       node1:
  ipaddress: ###.31.#.###

showing a node attribute example

Roles

Describes a node to better define its use

knife node run_list set node2 "role[load_balancer]"

example of changing a node to a role

knife ssh "role:web" -x <user> -P <password> "sudo chef-client"

example of telling a group of nodes under the "web" role to run chef-client

Search

Chef Search

ohai is going to report back up to Chef Server

all_web_nodes = search('node','role:web')

example of a search added to an attribute

Environments

  • Apply a node to an environment
  • make search queries much more specific
  • can define defferent functions of nodes that live on the same system
  • _default is the default environment assigned to all nodes

NOTE: exists under /environments under your root cookbook dir

knife node environment set <node> acceptance

Example of making a "node" be placed into a environment

Resources

About

Chef Training Feb 2016

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published