Top Banner
©2016 Chef Software Inc. 11 Automating Infrastructure with Chef Jennifer Davis Pittsburgh Tech Fest 2016 Course v1.1.1
168

Automating Infrastructure with Chef

Feb 17, 2017

Download

Technology

Jennifer Davis
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­1

Automating Infrastructure with Chef

Jennifer DavisPittsburgh Tech Fest 2016

Course v1.1.1

Page 2: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­2

The Chef Workflow

Page 3: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­3

Agenda

Ø Using Chef resources

Ø Building Chef cookbooks

Ø Collecting details about the system

Ø Managing data with templates

Page 4: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­4

Agenda

Ø Lab

Ø InSpec

Page 5: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­5

Course ObjectivesYou will leave this workshop with a basic understanding of Chef's core components, architecture, and commonly used tools.

Page 6: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­6

Objective:

Pre-­built WorkstationWe will provide for you a workstation with all the tools installed.

q Login to the Remote Workstationq Select a Text Editor

Page 7: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­7

Chef Lab System Architecture

Your Laptop

Remote WorkstationPreconfigured with

Chef tools

Page 8: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­8

Logging in to the Workstation

The authenticity of host '54.209.164.144 (54.209.164.144)' can't be established.RSA key fingerprint is SHA256:tKoTsPbn6ER9BLThZqntXTxIYem3zV/iTQWvhLrBIBQ.Are you sure you want to continue connecting (yes/no)? yes

[email protected]'s password: PASSWORD

chef@ip-172-31-15-97 ~]$

> ssh IPADDRESS -l USERNAME

Page 9: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­9

Objective:

Pre-­built WorkstationWe will provide for you a workstation with all the tools installed.

ü Login to the Remote Workstationq Select a Text Editor

Page 10: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­10

Choose an EditorYou'll need to choose an editor to edit files:

emacsnanovi / vim

Page 11: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­11

Objective:

Pre-­built WorkstationWe will provide for you a workstation with all the tools installed.

ü Login to the Remote Workstationü Select a Text Editor

Page 12: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­12

Chef Resources

Chef's Fundamental Building Blocks

Page 13: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­13

Objectives

Ø Use Chef to install packages on your virtual workstation

Ø Use the chef-­client command

Ø Create a basic Chef recipe file

Ø Define Chef Resources

Page 14: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­14

Objective:

Time for Some Fun!The workstation needs a little personal touch;; something that makes it a little more fun.

q Write a recipe that installs the 'cowsay' packageq Apply the recipe to the workstationq Use 'cowsay' to say something

Page 15: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­15

Learning Chef

One of the best ways to learn a technology is to apply the technology in every situation that it can be applied.

A number of chef tools are installed on the system so lets put them to use.

Page 16: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­16

ResourcesA 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.

https://docs.chef.io/resources.html

Page 17: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­17

Example: Packagepackage 'httpd' do

action :installend

The package named 'httpd' is installed.

https://docs.chef.io/resource_package.html

Page 18: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­18

Example: Serviceservice 'ntp' do

action [ :enable, :start ]end

The service named 'ntp' is enabled (start on reboot) and started.

https://docs.chef.io/resource_service.html

Page 19: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­19

Example: Filefile '/etc/motd' do

content 'This computer is the property ...'end

The file name '/etc/motd' is created with content 'This computer is the property ...'

https://docs.chef.io/resource_file.html

Page 20: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­20

Example: Filefile '/etc/php.ini.default' do

action :deleteend

The file name '/etc/php.ini.default' is deleted.

https://docs.chef.io/resource_file.html

Page 21: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­21

Resource Definitionfile 'hello.txt' docontent 'Hello, world!'

end

The TYPE named NAME should be ACTION'd with PROPERTIES

Page 22: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­22

Resource Definitionfile 'hello.txt' docontent 'Hello, world!'

end

The TYPE named NAME should be ACTION'd with PROPERTIES

Page 23: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­23

Resource Definitionfile 'hello.txt' docontent 'Hello, world!'

end

The TYPE named NAME should be ACTION'd with PROPERTIES

Page 24: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­24

Resource Definitionfile 'hello.txt' docontent 'Hello, world!'

end

The TYPE named NAME should be ACTION'd with PROPERTIES

Page 25: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­25

Resource Definitionfile 'hello.txt' docontent 'Hello, world!'

end

?The TYPE named NAME should be ACTION'd with PROPERTIES

Page 26: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­26

> nano moo.rb

Opening a Recipe File with an Editor

Page 27: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­27

Adding a Resource to the Recipe

package 'cowsay' doaction :install

end

~/moo.rb

Page 28: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­28

Objective:

Time for Some Fun!The workstation needs a little personal touch;; something that makes it a little more fun.

ü Write a recipe that installs the 'cowsay' packageq Apply the recipe to the workstationq Use 'cowsay' to say something

Page 29: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­29

chef-­clientchef-­client is an agent that runs locally on every node that is under management by Chef.

When a chef-­client is run, it will perform all of the steps that are required to bring the node into the expected state.

https://docs.chef.io/chef_client.html

Page 30: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­30

-­-­local-­mode (or -­z)chef-­client's default mode attempts to contact a Chef Server and ask it for the recipes to run for the given node.

We are overriding that behavior to have it work in a local mode.

Page 31: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­31

Starting Chef Client, version 12.5.1

resolving cookbooks for run list: []

Synchronizing Cookbooks:

Compiling Cookbooks...

[2016-02-19T13:08:13+00:00] WARN: Node ip-172-31-12-176.ec2.internal has an empty run list.

Converging 1 resources

Recipe: @recipe_files::/home/chef/moo.rb

* yum_package[nano] action install

- install version 3.03-8.e16 of package cowsay

Running handlers:

Running handlers complete

Chef Client finished, 1/1 resources updated in 38 seconds

> sudo chef-client –-local-mode moo.rb

Applying the Recipe

Page 32: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­32

Objective:

Time for Some Fun!The workstation needs a little personal touch;; something that makes it a little more fun.

ü Write a recipe that installs the 'cowsay' packageü Apply the recipe to the workstationq Use 'cowsay' to say something

Page 33: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­33

> cowsay will moo for food

Running cowsay with a Message

_____< will moo for food >

-----\ ^__^

\ (oo)\_______

(__)\ )\/\||----w |

|| ||

Page 34: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­34

Objective:

Time for Some Fun!The workstation needs a little personal touch;; something that makes it a little more fun.

ü Write a recipe that installs the 'cowsay' packageü Apply the recipe to the workstationü Use 'cowsay' to say something

Page 35: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­35

DiscussionWhat would happen if you applied the recipe again?

What would happen if the package were to become uninstalled?

Page 36: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­36

Test and Repairchef-client takes action only when it needs to. Think of it as test and repair.

Chef looks at the current state of each resource and takes action only when that resource is out of policy.

Page 37: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­37

Test and Repair

Yes NoIs package named 'cowsay'installed?(test)

Do NothingBring resource to desired state(repair)

package 'cowsay'

Page 38: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­38

Objective:

Hello, World?I heard Chef is written in Ruby. If that's the case its required that we write a quick "Hello, world!" application.

q Create a recipe that writes out a file with the contents "Hello, world!"q Apply that recipe to the workstationq Verify the contents of the file

Page 39: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­39

> nano hello.rb

Opening a Recipe File with an Editor

Page 40: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­40

Adding a Resource to the Recipe

file '/hello.txt' docontent 'Hello, world!'

end

~/hello.rb

Page 41: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­41

Objective:

Hello, World?I heard Chef is written in Ruby. If that's the case its required that we write a quick "Hello, world!" application.

ü Create a recipe that writes out a file with the contents "Hello, world!"q Apply that recipe to the workstationq Verify the contents of the file

Page 42: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­42

Starting Chef Client, version 12.5.1

resolving cookbooks for run list: []

Synchronizing Cookbooks:

Compiling Cookbooks...

[2016-02-19T13:08:13+00:00] WARN: Node ip-172-31-12-176.ec2.internal has an empty run list.

Converging 1 resources

Recipe: @recipe_files::/home/chef/hello.rb

* file[hello.txt] action create

- create new file hello.txt

- update content in file hello.txt from non to 315f5b

+++ ./.hello.txt20160224-8559-19kqial

2016-02-24 16:51:04.400844959 +0000

@@ -1 +1,2 @@

+Hello, world!

> sudo chef-client –-local-mode hello.rb

Applying the Recipe

Page 43: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­43

Objective:

Hello, World?I heard Chef is written in Ruby. If that's the case its required that we write a quick "Hello, world!" application.

ü Create a recipe that writes out a file with the contents "Hello, world!"ü Apply that recipe to the workstationq Verify the contents of the file

Page 44: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­44

Hello, world!

> cat hello.txt

Looking at the Contents of a File

Page 45: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­45

Objective:

Hello, World?I heard Chef is written in Ruby. If that's the case its required that we write a quick "Hello, world!" application.

ü Create a recipe that writes out a file with the contents "Hello, world!"ü Apply that recipe to the workstationü Verify the contents of the file

Page 46: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­46

DiscussionWhat is a resource?

What are some other possible examples of resources?

How did the example resources we wrote describe the desired state of an element of our infrastructure?

What does it mean for a resource to be a statement of configuration policy?

Page 47: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­47

Q&AWhat questions can we answer for you?

• resources• chef-­client• Test and Repair

Page 48: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­48

Cookbooks

Organizing Recipes

Page 49: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­49

ObjectivesØ Generate a Chef cookbook

Ø Generate a Chef recipe

Ø Applying a run-­list of recipes to a system

Page 50: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­50

Objective:

Setting up the WorkstationTime to create a recipe that sets up the workstation that we can share with others.

q Create a cookbookq Create a setup recipe within the cookbookq Apply the recipe to the workstationq Verify the workstation has been setup

Page 51: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­51

CookbookEach cookbook defines a scenario, such as everything needed to install and configure an application, and then it contains all of the components that are required to support that scenario.

Page 52: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­52

> mkdir cookbooks; cd cookbooks

Creating a Directory for Cookbooks

Page 53: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­53

What is 'chef'?An executable program that allows you generate cookbooks and cookbook components.

Page 54: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­54

Usage:

chef -h/--help

chef -v/--version

chef command [arguments...] [options...]

Available Commands:

exec Runs the command in context of the embedded ruby

gem Runs the `gem` command in context of the embedded ruby

generate Generate a new app, cookbook, or component

shell-init Initialize your shell to use ChefDK as your primary ruby

install Install cookbooks from a Policyfile and generate a locked cookboo...

update Updates a Policyfile.lock.json with latest run_list and cookbooks

> chef --help

Executing chef with the help flag

Page 55: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­55

Usage: chef generate GENERATOR [options]

Available generators:app Generate an application repo

cookbook Generate a single cookbook

recipe Generate a new recipeattribute Generate an attributes file

template Generate a file templatefile Generate a cookbook file

lwrp Generate a lightweight resource/provider

repo Generate a Chef policy repository

> chef generate --help

Executing chef generate with the help flag

Page 56: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­56

Compiling Cookbooks...Recipe: code_generator::cookbook

* directory[/home/chef/cookbooks/workstation] action create- create new directory /home/chef/cookbooks/workstation

* template[/home/chef/cookbooks/workstation/metadata.rb] action create_if_missing

- create new file /home/chef/cookbooks/workstation/metadata.rb

- update content in file /home/chef/cookbooks/workstation/metadata.rb from none to 0c09e4

(diff output suppressed by config)

* template[/home/chef/cookbooks/workstation/README.md] action

> chef generate cookbook workstation

Generating a cookbook

Page 57: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­57

Objective:

Setting up the WorkstationTime to create a recipe that sets up the workstation that we can share with others.

ü Create a workstation cookbookq Create a setup recipe within the cookbookq Apply the recipe to the workstationq Verify the workstation has been setup

Page 58: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­58

Usage: chef generate GENERATOR [options]

Available generators:app Generate an application repo

cookbook Generate a single cookbook

recipe Generate a new recipeattribute Generate an attributes file

template Generate a file templatefile Generate a cookbook file

lwrp Generate a lightweight resource/provider

repo Generate a Chef policy repository

> chef generate --help

Executing chef generate with the help flag

Page 59: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­59

Usage: chef generate recipe [path/to/cookbook] NAME [options]-C, --copyright COPYRIGHT Name of the copyright holder

- defaults to 'The Authors'-m, --email EMAIL Email address of the author -

defaults to '[email protected]'

-a, --generator-arg KEY=VALUE Use to set arbitrary attribute KEY to VALUE in the code_generator cookbook

-I, --license LICENSE all_rights, apache2, mit, gplv2, gplv3 - defaults to all_rights

-g GENERATOR_COOKBOOK_PATH, Use GENERATOR_COOKBOOK_PATH for the code_generator cookbook

--generator-cookbook

> chef generate recipe --help

Executing chef generate with the help flag

Page 60: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­60

Compiling Cookbooks...Recipe: code_generator::recipe

* directory[cookbooks/workstation/spec/unit/recipes] action create (up to date)

* cookbook_file[cookbooks/workstation/spec/spec_helper.rb] action create_if_missing (up to date)

* template[cookbooks/workstation/spec/unit/recipes/setup_spec.rb] action create_if_missing

- create new file cookbooks/workstation/spec/unit/recipes/setup_spec.rb

- update content in file cookbooks/workstation/spec/unit/

> chef generate recipe workstation setup

Generating a recipe within the cookbook

Page 61: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­61

Defining the Setup Recipe

## Cookbook Name:: workstation

# Recipe:: setup#

# Copyright (c) 2016 The Authors, All Rights Reserved.

package 'tree'

file '/etc/motd' docontent 'Property of ...'

end

~/cookbooks/workstation/recipes/setup.rb

Page 62: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­62

Objective:

Setting up the WorkstationTime to create a recipe that sets up the workstation that we can share with others.

ü Create a workstation cookbookü Create a setup recipe within the cookbookq Apply the recipe to the workstationq Verify the workstation has been setup

Page 63: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­63

chef-­clientchef-­client is an agent that runs locally on every node that is under management by Chef.

When a chef-­client is run, it will perform all of the steps that are required to bring the node into the expected state.

https://docs.chef.io/chef_client.html

Page 64: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­64

-­-­runlist "recipe[COOKBOOK::RECIPE]"In local mode, we need to provide a list of recipes to apply to the system. This is called a run list. A run list is an ordered collection of recipes to execute.

Each recipe in the run list must be addressed with the format recipe[COOKBOOK::RECIPE].

Page 65: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­65

Starting Chef Client, version 12.7.2resolving cookbooks for run list: ["workstation::setup"]

Synchronizing Cookbooks:- workstation (0.1.0)

Compiling Cookbooks...

Converging 2 resourcesRecipe: workstation::setup

* yum_package[tree] action install- install version 1.5.3-3.el6 of package tree

* file[/etc/motd] action create

> sudo chef-client --local-mode --runlist "recipe[workstation::setup]"

Applying the workstation's setup recipe

Page 66: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­66

Objective:

Setting up the WorkstationTime to create a recipe that sets up the workstation that we can share with others.

ü Create a workstation cookbookü Create a setup recipe within the cookbookü Apply the recipe to the workstationq Verify the workstation has been setup

Page 67: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­67

Berksfile chefignore

metadata.rb README.md

recipes

default.rb setup.rb

spec spec_helper.rb

10 directories, 11 files

> tree cookbooks/workstation

Using the tree application

Page 68: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­68

Property of ...

> cat /etc/motd

Viewing the Message of the Day file

Page 69: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­69

Objective:

Setting up the WorkstationTime to create a recipe that sets up the workstation that we can share with others.

ü Create a workstation cookbookü Create a setup recipe within the cookbookü Apply the recipe to the workstationü Verify the workstation has been setup

Page 70: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­70

DiscussionWhat file would you read first when examining a cookbook? second?

What other recipes might you include workstation cookbook?

Page 71: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­71

Q&AWhat questions can we answer for you?

• Cookbooks• Recipes• Run-­lists

Page 72: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­72

Ohai

Finding and Displaying Information About Our System

Page 73: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­73

ObjectivesAfter completing this module, you should be able to

Ø Capture details about a system

Ø Use the node object within a recipe

Ø Use Ruby's string interpolation

Ø Update the version of a cookbook

Page 74: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­74

Managing a Large Number of Servers Have you ever had to manage a large number of servers that were almost identical?

How about a large number of identical servers except that each one had to have host-­specific information in a configuration file?

Page 75: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­75

Some Useful System Dataq IP Addressq hostnameq memoryq CPU -­ MHz

Page 76: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­76

Objective:

Details About the NodeDisplaying system details in the MOTD definitely sounds useful.

q Discover attributes about the system with Ohaiq Update the MOTD file contents, in the "workstation" cookbook, to include node detailsq Update the cookbook's version numberq Apply the updated recipe and verify the results

Page 77: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­77

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. Ohai is required by the chef-­client and must be present on a node. (Ohai is installed on a node as part of the chef-­client install process.)

http://docs.chef.io/ohai.html

Page 78: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­78

All About The System Ohai queries the operating system with a number of commands, similar to the ones demonstrated.

The data is presented in JSON (JavaScript Object Notation).

Page 79: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­79

"kernel":

"name": "Linux",

"release": "2.6.32-431.1.2.0.1.el6.x86_64",

"version": "#1 SMP Fri Dec 13 13:06:13 UTC 2013",

"machine": "x86_64",

"os": "GNU/Linux",

"modules":

"veth":

"size": "5040",

"refcount": "0"

,

"ipt_addrtype":

> ohai

Running Ohai to Show All Attributes

Page 80: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­80

["172.31.57.153"

]

> ohai ipaddress

Running Ohai to Show the IP Address

Page 81: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­81

["ip-172-31-57-153"

]

> ohai hostname

Running Ohai to Show the Hostname

Page 82: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­82

"swap":

"cached": "0kB","total": "0kB",

"free": "0kB"

,"total": "604308kB",

"free": "297940kB","buffers": "24824kB",

"cached": "198264kB",

> ohai memory

Running Ohai to Show the Memory

Page 83: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­83

["604308kB"

]

> ohai memory/total

Running Ohai to Show the Total Memory

Page 84: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­84

"0":

"vendor_id": "GenuineIntel","family": "6",

"model": "45",

"model_name": "Intel(R) Xeon(R) CPU E5-2650 0 @ 2.00GHz","stepping": "7",

"mhz": "1795.673","cache_size": "20480 KB",

"physical_id": "34

> ohai cpu

Running Ohai to Show the CPU

Page 85: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­85

"vendor_id": "GenuineIntel",

"family": "6","model": "45",

"model_name": "Intel(R) Xeon(R) CPU E5-2650 0 @ 2.00GHz",

"stepping": "7","mhz": "1795.673",

"cache_size": "20480 KB","physical_id": "34",

"core_id": "0",

"cores": "1","flags": [

> ohai cpu/0

Running Ohai to Show the First CPU

Page 86: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­86

["1795.673"

]

> ohai cpu/0/mhz

Running Ohai to Show the First CPU Mhz

Page 87: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­87

Objective:

Details About the NodeNow it is time to explore how we can use Ohai data in the recipe.

ü Discover attributes about the system with Ohaiq Update the MOTD file contents, in the "workstation" cookbook, to include node detailsq Update the cookbook's version numberq Apply the updated recipe and verify the results

Page 88: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­88

ohai + chef-­client = <3chef-­client automatically executes ohai and stores the data about the node in an object we can use within the recipes named node.

http://docs.chef.io/ohai.html

Page 89: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­89

The Node ObjectThe node object is a representation of our system. It stores all the attributes found about the system.

http://docs.chef.io/nodes.html#attributes

Page 90: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­90

node

ipaddress hostname memory

total

cpu

0

mhz

...

"IPADDRESS: #node['ipaddress']"

IPADDRESS: 104.236.192.102

The Node

Page 91: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­91

node

ipaddress hostname memory

total

cpu

0

mhz

...

"HOSTNAME: #node['hostname']"

HOSTNAME: banana-stand

The Node

Page 92: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­92

The Node

"MEMORY: #node['memory']['total']"

node

ipaddress hostname memory

total

cpu

0

mhz

...

MEMORY: 502272kB

Page 93: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­93

The Node

"CPU: #node['cpu']['0']['mhz'] MHz"

node

ipaddress hostname memory

total

cpu

0

mhz

...

CPU: 2399.998 MHz

Page 94: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­94

String Interpolation

apple_count = 4puts "I have #apple_count apples"

http://en.wikipedia.org/wiki/String_interpolation#Ruby

I have 4 apples

Page 95: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­95

String Interpolation

apple_count = 4puts "I have #apple_count apples"

I have 4 apples

Page 96: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­96

String Interpolation

I have 4 apples

apple_count = 4puts "I have #apple_count apples"

Page 97: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­97

Updating the content Property

# ... PACKAGE RESOURCES ...file '/etc/motd' do

content "Property of ...

IPADDRESS: #node['ipaddress']

HOSTNAME : #node['hostname']MEMORY : #node['memory']['total']

CPU : #node['cpu']['0']['mhz']"

end

~/cookbooks/workstation/recipes/setup.rb

Page 98: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­98

Objective:

Details About the NodeNow that we have added a new feature it is time to update the cookbook's version number.

ü Discover attributes about the system with Ohaiü Update the MOTD file contents, in the "workstation" cookbook, to include node detailsq Update the cookbook's version numberq Apply the updated recipe and verify the results

Page 99: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­99

Cookbook VersionsA cookbook version represents a set of functionality that is different from the cookbook on which it is based.

https://docs.chef.io/cookbook_versions.html

Page 100: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­100

Semantic VersionsGiven a version number MAJOR.MINOR.PATCH, increment the:

• MAJOR version when you make incompatible API changes• MINOR version when you add functionality in a backwards-­compatible manner

• PATCH version when you make backwards-­compatible bug fixes

http://semver.org

Page 101: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­101

Major, Minor, or Patch?What kind of changes did you make to the cookbook?

Page 102: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­102

Updating the Cookbook Version

name 'workstation'maintainer 'The Authors'

maintainer_email '[email protected]'license 'all_rights'

description 'Installs/Configures workstation'

long_description 'Installs/Configures workstation'version '0.2.0'

~/cookbooks/workstation/metadata.rb

Page 103: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­103

Objective:

Details About the NodeNow let's apply this updated policy to the state of the system.

ü Discover attributes about the system with Ohaiü Update the MOTD file contents, in the "workstation" cookbook, to include node detailsü Update the cookbook's version numberq Apply the updated recipe and verify the results

Page 104: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­104

Starting Chef Client, version 12.7.2resolving cookbooks for run list: ["workstation::setup"]

Synchronizing Cookbooks:- workstation (0.2.0)

Compiling Cookbooks...

Converging 2 resourcesRecipe: workstation::setup

* yum_package[tree] action install (up to date)* file[/etc/motd] action create

- update content in file /etc/motd from d100eb t

$ sudo chef-client --local-mode -r "recipe[workstation::setup]"

Applying the workstation Cookbook

Page 105: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­105

Property of ...

IPADDRESS: 172.31.57.153HOSTNAME : ip-172-31-57-153

MEMORY : 604308kB

CPU : 1795.673

$ cat /etc/motd

Verifying that the MOTD has been Updated

Page 106: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­106

Objective:

Details About the NodeThe workstation will now report back with information about it.

ü Discover attributes about the system with Ohaiü Update the MOTD file contents, in the "workstation" cookbook, to include node detailsü Update the cookbook's version numberü Apply the updated recipe and verify the results

Page 107: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­107

DiscussionWhat is the major difference between a single-­quoted string and a double-­quoted string?

How are the details about the system available within a recipe?

How does the version number help convey information about the state of the cookbook?

Page 108: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­108

Q&AWhat questions can we help you answer?

• Ohai• Node Object• Node Attributes• String Interpolation• Semantic Versions

Page 109: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­109

Using Templates

Extracting the Content for Clarity

Page 110: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­110

ObjectivesAfter completing this module, you should be able to

Ø Explain when to use a template resource

Ø Create a template file

Ø Use ERB tags to display node data in a template

Ø Define a template resource

Page 111: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­111

Cleaner RecipesIn the last section we updated our cookbook to display information about our node.

We expanded the text within the file resource's content property.

Page 112: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­112

Viewing the workstation's setup recipe

package 'tree'

file '/etc/motd' docontent "Property of ...

IPADDRESS: #node['ipaddress']HOSTNAME : #node['hostname']

MEMORY : #node['memory']['total']CPU : #node['cpu']['0']['mhz']

"

end

~/cookbooks/workstation/recipes/setup.rb

Page 113: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­113

"<h1 style="color: red;">Hello, World!</h1>"

Double Quotes close Double QuotesDouble quoted strings are terminated by double quotes.

Page 114: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­114

"<h1 style=\"color: red;\">Hello, World!</h1>"

BackslashWe can use double-­quotes as long as we prefix them with a backslash.

Page 115: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­115

"Root Path: \"

BackslashBackslashes are reserved characters. So to use them you need to use a backslash.

Page 116: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­116

"Root Path: \\"

BackslashBackslashes are reserved characters. So to use them you need to use a backslash.

Page 117: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­117

This content property generates unexpected formattingfile '/etc/motd' do

content 'This is the first line of the file.

This is the second line. If I try and line it up...'

end

This is the first line of the file.This is the second line. If I try and line it up...

/etc/motd

Page 118: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­118

Copy PasteThis process is definitely error prone. Especially because a human has to edit the file again before it is deployed.

Page 119: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­119

What We NeedWe need the ability to store the data in another file, which is in the native format of the file we are writing out but that still allows us to insert ruby code...

...specifically, the node attributes we have defined.

Page 120: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­120

Objective:

Cleaner Setup RecipeAdding all the information into the recipe did make it hard to read.

q Create a template with chef generateq Define the contents of the ERB templateq Change the file resource to the template resourceq Update the cookbook's version numberq Apply the updated recipe and verify the results

Page 121: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­121

TemplateA cookbook template is an Embedded Ruby (ERB) template that is used to generate files … Templates may contain Ruby expressions and statements and are a great way to...

Use the template resource to add cookbook templates to recipes;; place the corresponding Embedded Ruby (ERB) template in a cookbook’s /templates directory.

https://docs.chef.io/resource_template.html

Page 122: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­122

TemplateTo use a template, two things must happen:

1. A template resource must be added to a recipe2. An Embedded Ruby (ERB) template must be added to a cookbook

https://docs.chef.io/resource_template.html#using-­templates

Page 123: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­123

Usage: chef generate GENERATOR [options]

Available generators:app Generate an application repo

cookbook Generate a single cookbook

recipe Generate a new recipeattribute Generate an attributes file

template Generate a file templatefile Generate a cookbook file

lwrp Generate a lightweight resource/provider

repo Generate a Chef policy repositorypolicyfile Generate a Policyfile for use with the install/push

commands (experimental)

> chef generate --help

Showing chef generate's Help

Page 124: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­124

Usage: chef generate template [path/to/cookbook] NAME [options]-C, --copyright COPYRIGHT Name of the copyright holder

- defaults to 'The Authors'-m, --email EMAIL Email address of the author -

defaults to ...

-a, --generator-arg KEY=VALUE Use to set arbitrary attribute KEY to VALUE in the

-I, --license LICENSE all_rights, apache2, mit, gplv2, gplv3 - defaults to

-s, --source SOURCE_FILE Copy content from SOURCE_FILE

-g GENERATOR_COOKBOOK_PATH, Use GENERATOR_COOKBOOK_PATH for the code_generator

--generator-cookbook

> chef generate template --help

Showing chef generate template's Help

Page 125: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­125

Compiling Cookbooks...Recipe: code_generator::template

* directory[cookbooks/workstation/templates/default] action create

- create new directory cookbooks/workstation/templates/default

* template[cookbooks/workstation/templates/default/motd.erb] action create

- create new file cookbooks/workstation/templates/default/motd.erb

- update content in file cookbooks/workstation/templates/default/motd.erb from none to e3b0c4

> chef generate template cookbooks/workstation motd

Generating a motd Template

Page 126: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­126

cookbooks/workstation/templates/ default

motd.erb

1 directory, 1 file

> tree cookbooks/workstation/templates

Examining the templates Directory

Page 127: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­127

Objective:

Cleaner RecipesNow it is time to populate the template file

ü Create a template with chef generateq Define the contents of the ERB templateq Change the file resource to the template resourceq Update the cookbook's version numberq Apply the updated recipe and verify the results

Page 128: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­128

ERBAn 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.

https://docs.chef.io/templates.html#variables

Page 129: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­129

Text Within an ERB Template<% if (50 + 50) == 100 %>50 + 50 = <%= 50 + 50 %><% else %>At some point all of MATH I learned in school changed.<% end %>

Each ERB tag has a beginning tag and a matched ending tag.

Page 130: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­130

Text Within an ERB Template<% if (50 + 50) == 100 %>50 + 50 = <%= 50 + 50 %><% else %>At some point all of MATH I learned in school changed.<% end %>

Each ERB tag has a beginning tag and a matched ending tag.

Page 131: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­131

Text Within an ERB Template<% if (50 + 50) == 100 %>50 + 50 = <%= 50 + 50 %><% else %>At some point all of MATH I learned in school changed.<% end %>

Each ERB tag has a beginning tag and a matched ending tag.

Page 132: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­132

Text Within an ERB Template<% if (50 + 50) == 100 %>50 + 50 = <%= 50 + 50 %><% else %>At some point all of MATH I learned in school changed.<% end %>

Executes the ruby code within the brackets and do not display the result.

Page 133: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­133

Text Within an ERB Template<% if (50 + 50) == 100 %>50 + 50 = <%= 50 + 50 %><% else %>At some point all of MATH I learned in school changed.<% end %>

Executes the ruby code within the brackets and display the results.

Page 134: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­134

The Angry Squid

<%=

Page 135: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­135

Copying the Existing Content into the Template

Property of ...

IPADDRESS: #node['ipaddress']HOSTNAME : #node['hostname']

MEMORY : #node['memory']['total']

CPU : #node['cpu']['0']['mhz']

~/cookbooks/workstation/templates/default/motd.erb

Page 136: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­136

Changing String Interpolation to ERB Tags

Property of ...

IPADDRESS: <%= node['ipaddress'] %>HOSTNAME : <%= node['hostname'] %>

MEMORY : <%= node['memory']['total'] %>

CPU : <%= node['cpu']['0']['mhz'] %>

~/cookbooks/workstation/templates/default/motd.erb

Page 137: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­137

Objective:

Cleaner RecipesThe template is created and defined. It now needs to be used within the recipe.

ü Create a template with chef generateü Define the contents of the ERB templateq Change the file resource to the template resourceq Update the cookbook's version numberq Apply the updated recipe and verify the results

Page 138: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­138

Removing the file Resource

# ... PACKAGE RESOURCES ...

file '/etc/motd' docontent "Property of ...

IPADDRESS: #node['ipaddress']HOSTNAME : #node['hostname']

MEMORY : #node['memory']['total']CPU : #node['cpu']['0']['mhz']

"

end

~/cookbooks/workstation/recipes/setup.rb

Page 139: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­139

Changing from file to template Resource

# ... PACKAGE RESOURCES ...

template '/etc/motd' dosource 'motd.erb'

end

~/cookbooks/workstation/recipes/setup.rb

Page 140: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­140

Objective:

Cleaner RecipesThis is a change to the cookbook so it is time to update the version again.

ü Create a template with chef generateü Define the contents of the ERB templateü Change the file resource to the template resourceq Update the cookbook's version numberq Apply the updated recipe and verify the results

Page 141: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­141

Updating the Cookbook's Version Number

name 'workstation'maintainer 'The Authors'

maintainer_email '[email protected]'license 'all_rights'

description 'Installs/Configures workstation'

long_description 'Installs/Configures workstation'version '0.2.1'

~/cookbooks/workstation/metadata.rb

Page 142: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­142

Objective:

Cleaner RecipesThis is a change to the cookbook so it is time to update the version again.

ü 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 numberq Apply the updated recipe and verify the results

Page 143: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­143

- workstation (0.2.1)Compiling Cookbooks...

Converging 2 resourcesRecipe: workstation::setup

* yum_package[tree] action install (up to date)

* template[/etc/motd] action create (up to date)

Running handlers:Running handlers complete

Chef Client finished, 0/2 resources updated in 12 seconds

> sudo chef-client --local-mode --runlist "recipe[workstation::setup]"

Applying the Updated Cookbook

Page 144: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­144

Property of ...

IPADDRESS: 172.31.57.153HOSTNAME : ip-172-31-57-153

MEMORY : 604308kB

CPU : 1795.673

> cat /etc/motd

Verifying the Conents of the MOTD File

Page 145: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­145

Objective:

Cleaner RecipesThis is a change to the cookbook so it is time to update the version again.

ü 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

Page 146: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­146

DiscussionWhat is the benefit of using a template over defining the content within a recipe? What are the drawbacks?

What are the two types of ERB tags we talked about?

What do each of the ERB tags accomplish?

Page 147: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­147

Q&AWhat questions can we help you answer?

• template resource• Files in the template directory• ERB

Page 148: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­148

Lab: Building a Webserver Cookbook

Page 149: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­149

Setting up a Web Serverq Create a cookbook named 'apache' with a recipe named 'server'q The 'server' recipe defines the following policy:

o The package named 'httpd' is installed.

o The template named '/var/www/html/index.html' is created with the source 'index.html.erb'

o The service named 'httpd' is started and enabled.q Create a template named 'index.html.erb' and populate it with a

welcome message, the node's ipaddress, and the node's hostname.q Use chef-­client to apply the apache cookbook's server recipeq Verify the site is available by running curl localhost

Page 150: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­150

Compiling Cookbooks...Recipe: code_generator::cookbook

* directory[/home/chef/cookbooks/apache] action create- create new directory /home/chef/cookbooks/apache

* template[/home/chef/cookbooks/apache/metadata.rb] action create_if_missing

- create new file /home/chef/cookbooks/apache/metadata.rb

- update content in file /home/chef/cookbooks/apache/metadata.rb from none to 37ed5f

(diff output suppressed by config)

* template[/home/chef/cookbooks/apache/README.md] action

> chef generate cookbook cookbooks/apache

Creating the apache Cookbook

Page 151: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­151

Compiling Cookbooks...Recipe: code_generator::recipe

* directory[cookbooks/apache/spec/unit/recipes] action create (up to date)

* cookbook_file[cookbooks/apache/spec/spec_helper.rb] action create_if_missing (up to date)

* template[cookbooks/apache/spec/unit/recipes/server_spec.rb] action create_if_missing

- create new file cookbooks/apache/spec/unit/recipes/server_spec.rb

- update content in file cookbooks/apache/spec/unit/recipes/server_spec.rb from none to a43970

> chef generate recipe cookbooks/apache server

Creating the server Recipe

Page 152: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­152

Defining the Policy in the server Recipe

## Cookbook Name:: apache# Recipe:: server## Copyright (c) 2016 The Authors, All Rights Reserved.package 'httpd'

template '/var/www/html/index.html' dosource 'index.html.erb'

end

service 'httpd' doaction [:start, :enable]

end

~/cookbooks/apache/recipes/server.rb

Page 153: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­153

Compiling Cookbooks...Recipe: code_generator::template

* directory[cookbooks/apache/templates/default] action create- create new directory cookbooks/apache/templates/default

* template[cookbooks/apache/templates/default/index.html.erb] action create

- create new file cookbooks/apache/templates/default/index.html.erb

- update content in file cookbooks/apache/templates/default/index.html.erb from none to e3b0c4

(diff output suppressed by config)

> chef generate template cookbooks/workstation index.html

Creating the html Template

Page 154: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­154

Defining the index.html Template

<html><body>

<h1>Welcome Home!</h1><h2>IPADDRESS: <%= node['ipaddress'] %></h2>

<h2>HOSTNAME: <%= node['hostname'] %></h2>

<body></html>

~/cookbooks/apache/templates/default/index.html.erb

Page 155: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­155

Starting Chef Client, version 12.7.2resolving cookbooks for run list: ["apache::server"]

Synchronizing Cookbooks:- apache (0.1.0)

Compiling Cookbooks...

Converging 3 resourcesRecipe: apache::server

* yum_package[httpd] action install- install version 2.2.15-47.el6.centos.4 of package httpd

* template[/var/www/html/index.html] action create

- create new file /var/www/html/index.html

> sudo chef-client --local-mode --runlist "recipe[apache::server]"

Applying the apache Cookbook's server Recipe

Page 156: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­156

<html><body>

<h1>Welcome Home!</h1><h2>IPADDRESS: 172.31.57.153</h2>

<h2>HOSTNAME: ip-172-31-57-153</h2>

<body></html>

> curl localhost

Verifying the Default Website is Available

Page 157: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­157

Q&AWhat questions can we help you answer?

Page 158: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­158

Inspec

Page 159: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­159

Setting up a Web Serverq Create a cookbook named 'apache' with a recipe named 'server'q The 'server' recipe defines the following policy:

o The package named 'httpd' is installed.

o The template named '/var/www/html/index.html' is created with the source 'index.html.erb'

o The service named 'httpd' is started and enabled.q Create a template named 'index.html.erb' and populate it with a

welcome message, the node's ipaddress, and the node's hostname.q Use chef-­client to apply the apache cookbook's server recipeq Verify the site is available by running curl localhost

Page 160: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­160

Creating a check

describe service 'ssh-agent' doit should be_running

end

~/test.rb

Page 161: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­161

.

Finished in 0.00901 seconds (files took 0.98501 seconds to load)1 example, 0 failures

> inspec exec test.rb

Test a target

Page 162: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­162

Verifying version

describe file('/etc/ssh/sshd_config') doits(:content) should match /Protocol 2/

end

~/test.rb

Page 163: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­163

Verifying version – Better!

describe sshd_config doits('Protocol') should cmp 2

end

~/test.rb

Page 164: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­164

Host resource

describe host('example.com', port: 80, proto: 'tcp') doit should be_reachable

end

~/test.rb

Page 165: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­165

Mysql_session resource

sql = mysql_session('my_user','password')describe sql.query('show databases like \'test\';') do

its(:stdout) should_not match(/test/) end

~/test.rb

Page 166: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­166

Mysql_session resource

sql = mysql_session('my_user','password')describe sql.query('show databases like \'test\';') do

its(:stdout) should_not match(/test/) end

~/test.rb

Page 167: Automating Infrastructure with Chef

©2016 Chef Software Inc. 1-­167

Available Resourcesapache_confaptaudit_policyauditd_confauditd_rulesbondbridgecsvcommanddirectoryetc_groupfile

gemgrouphostinetd_confinterfaceiptableskernel_modulekernel_parameterlimits_conflogin_defsmountmysql_conf

mysql_sessionnpmntp_confonegetosos_envpackageparse_configparse_config_filepasswdpipport…. And more!!!

Page 168: Automating Infrastructure with Chef