Top Banner
Chef Essentials on AWS Introduction ©2015 Chef Software Inc. Course v1.0.2
160

Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Jun 24, 2018

Download

Documents

duongtuong
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: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Chef Essentials on AWSIntroduction

©2015 Chef Software Inc. Course v1.0.2

Page 2: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

1-

Your Chef Team

©2015 Chef Software Inc. 2

Jody Wolfborn – Solutions Architect

[email protected]

@joderita

Jeff Mery – Solutions Architect

[email protected]

Page 3: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

1-

Introduce Yourselves

©2015 Chef Software Inc. 3

Name

Current job role

Previous job roles/background

Experience with Chef and/or config management

Favorite Text Editor

Page 4: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

1-

Expectations

4

You will leave this class with a basic understanding of Chef's core

components, architecture, commonly used tools, and basic

troubleshooting methods

You bring with you your own domain expertise and problems. Chef is

a framework for solving those problems. Our job is to teach you how

to express solutions to your problems with Chef.

©2015 Chef Software Inc.

Page 5: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

1-

Course Objectives

5

After completing this course, you should be able to:

Use Chef Resources to define the state of your system

Write and use Chef recipes and cookbooks

Automate testing of cookbooks

Create Organizations

Provision and bootstrap nodes

©2015 Chef Software Inc.

Page 6: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

1-

Chef

7

Chef can automate how you build, deploy, and manage your infrastructure.

Chef can integrate with cloud-based platforms such as Amazon Elastic

Compute Cloud to automatically provision and configure new machines.

©2015 Chef Software Inc.

Page 7: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

1-

Chef

8

Chef is a large set of tools that are able to be used on multiple

platforms and in numerous configurations.

Learning Chef is like learning a language. You will reach fluency very

fast but it will take practice until you become comfortable.

A great way to learn Chef is to use Chef

©2015 Chef Software Inc.

Page 8: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

1-

Chef Essentials

9

Ask Me Anything: It is important that we answer your questions and

set you on the path to find more.

Break It: If everything works the first time go back and make some

changes. Break it!

©2015 Chef Software Inc.

Page 9: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

1-

Chef Lab System Architecture

10

In this course you will use two different architectures:

1. Initially, you'll use a virtual workstation so you can start using Chef right

away.

2. Later, you'll use a common production type of architecture that includes a

Chef Server.

©2015 Chef Software Inc.

Page 10: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

1-

Chef Lab System Architecture

11

Architecture 1

©2015 Chef Software Inc.

Your Laptop

Virtual Workstation

Preconfigured with

Chef tools

Page 11: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

1-

Chef Lab System Architecture

12

Architecture 2

©2015 Chef Software Inc.

Chef Server

Your Local Workstation Nodes

Page 12: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

1-

Getting a Workstation

13©2015 Chef Software Inc.

Your instructor will assign you a

virtual workstation hosted in EC2.

The credentials for login are:

Username: Administrator

Password: Cod3Can!

Page 13: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

1-

Hands-on Legend

14

GE or Group Exercise: All participants and the instructor do this task

together with the instructor often leading the way and explaining

things as we proceed.

Lab: You perform this task on your own.

©2015 Chef Software Inc.

Page 14: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

©2015 Chef Software Inc.

Page 15: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Chef Resources

Page 16: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objectives

After completing this module, you should be able to:

Use Chef to manage files on a system

Use the chef-apply command

Create a basic Chef recipe file

Define Chef Resources

Page 17: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Hello, World!

Create a file on the system that states 'Hello, world!'

Page 18: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

What is a recipe file?A recipe file is a ruby file that is mostly a collection of resources. It is a

fundamental configuration element within Chef.

Page 19: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Resources

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

Page 20: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Managing a file

Let's create a recipe file, add a resource, and then apply it to our system.

Change into a directory where we can do work

Create a recipe file with a resource to generate a file

Use chef-apply to execute the recipe file

Page 21: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

> cd ~/Desktop

Page 22: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Managing a text file

Let's create a recipe file, add a resource,and then apply it to our system.

Change into a directory where we can do work

Create a recipe file with a resource to generate a file

Use chef-apply to execute the recipe file

Page 23: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Open hello.rb in Atom

file 'greeting.txt' do

content 'Hello, world!'

action :create

end

~/Desktop/hello.rb

Page 24: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Managing a File

Let's create a recipe file, add a resource,and then apply it to our system.

Change into a directory where we can do work

Create a recipe file with a resource to generate a file

Use chef-apply to execute the recipe file

Page 25: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

> chef-apply --help

Page 26: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

> chef-apply hello.rb

Page 27: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Hello, world!

> cat greeting.txt

Page 28: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Managing a File

Let's create a recipe file, add a resource, and then apply it to our system.

Change into a directory where we can do work

Create a recipe file with a resource to generate a file

Use chef-apply to execute the recipe file

Page 29: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Deleting a File Create a recipe file named 'goodbye.rb'

Add a file resource:

The file named 'greeting.txt' is deleted.

Use chef-apply to apply the policy defined in the recipe file

Page 30: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

file 'greeting.txt' do

action :delete

end

~/Desktop/goodbye.rb

Page 31: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

> chef-apply goodbye.rb

Page 32: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

> dir

Page 33: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Q&A

What questions can we answer for you?

Page 34: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean
Page 35: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Cookbooks

Organizing Recipes for Fun and Profit

Page 36: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objectives

After completing this module, you should be able to:

Generate a Chef cookbook

Write a Chef recipe that builds a web server

Commit your code to version control

Spin up an EC2 Linux instance using Test Kitchen

Destroy and recreate your test instance

Page 37: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Managing Apache on Linux

Generate a web cookbook

Create an instance to test the cookbook against

Write a recipe to install the apache package

Add content to your homepage

Update the recipe to start and enable the apache service

Page 38: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Get into your cookbooks directory> cd ~/chef-repo/cookbooks

Page 39: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Help!

Usage:

chef -h/--help

chef -v/--version

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

Available Commands:

exec Runs the command in context of the embedded ruby

env Prints environment variables used by ChefDK

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

generate Generate a new app, cookbook, or component

> chef --help

Page 40: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

More help

Usage: chef generate GENERATOR [options]

Available generators:

app Generate an application repo

cookbook Generate a single cookbook

recipe Generate a new recipe

attribute Generate an attributes file

...

> chef generate --help

Page 41: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Even more help...

Usage: chef generate cookbook NAME [options]

-b, --berks Generate cookbooks with berkshelf integration

-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

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

...

> chef generate cookbook --help

Page 42: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Generate a New Cookbook

Compiling Cookbooks...

Recipe: code_generator::cookbook

* directory[C:/Users/Administrator/chef-repo/cookbooks/web] action create

- create new directory C:/Users/Administrator/chef-repo/cookbooks/web

* template[C:/Users/Administrator/chef-repo/cookbooks/web/metadata.rb] action create_if_missing

- create new file C:/Users/Administrator/chef-repo/cookbooks/web/metadata.rb

- update content in file C:/Users/Administrator/chef-repo/cookbooks/web/metadata.rb from none to

5367bd

(diff output suppressed by config)

* template[C:/Users/Administrator/chef-repo/cookbooks/web/README.md] action create_if_missing

- create new file C:/Users/Administrator/chef-repo/cookbooks/web/README.md

- update content in file C:/Users/Administrator/chef-repo/cookbooks/web/README.md from none to

05e1d0

(diff output suppressed by config)

> chef generate cookbook web

Page 43: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Managing Apache on Linux

Generate a web cookbook

Create an instance to test the cookbook against

Write the recipe to install the apache package

Update the recipe to start and enable the apache service

Page 44: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Get into the web cookbook> cd web

Page 45: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Commit Your Changes

After committing, your git status should be clean:

On branch master

nothing to commit, working directory clean

git status

git add .

git commit –m "Initial Commit"

Page 46: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Update your metadata.rb

Page 47: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Basic Git Commands

Page 48: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Replace the Existing Kitchen Configuration

> cp ~/chef-repo/.kitchen.linux.yml .kitchen.yml

Page 49: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Edit your .kitchen.yml file> atom .kitchen.yml

Page 50: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Put web::default in your run_list

...

suites:

- name: default

run_list:

- recipe[web::default]

attributes:

~/chef-repo/cookbooks/web/.kitchen.yml

Page 51: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Commit Your Changes

After committing, your git status should be clean:

On branch master

nothing to commit, working directory clean

git status

git add .

git commit –m "Updated run list, added resource tag"

Page 52: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Check your instance status

Instance Driver Provisioner Verifier Transport Last Action

default-linux Ec2 ChefZero Busser Ssh <Not Created>

> kitchen list

Page 53: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Create a new test instance

-----> Creating <default-linux>...

If you are not using an account that qualifies under the AWS free-tier,

you may be charged to run these suites. The charge should be minimal, but

neither Test Kitchen nor its maintainers are responsible for your incurred

costs.

Instance <i-5c56adef> requested.

EC2 instance <i-5c56adef> created.

Waited 0/300s for instance <i-5c56adef> to become ready.

Waited 5/300s for instance <i-5c56adef> to become ready.

Waited 10/300s for instance <i-5c56adef> to become ready.

Waited 15/300s for instance <i-5c56adef> to become ready.

> kitchen create

Page 54: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Show your instance status

Instance Driver Provisioner Verifier Transport Last Action

default-linux Ec2 ChefZero Busser Ssh Created

> kitchen list

Page 55: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Managing Apache on Linux

Generate a web cookbook

Create an instance to test the cookbook against

Write the recipe to install the apache package

Update the recipe to start and enable the apache service

Page 56: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Add a Package Resource

#

# Cookbook Name:: web

# Recipe:: server

#

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

package 'httpd' do

action :install

end

~/chef-repo/cookbooks/web/recipes/default.rb

Page 57: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Run Chef on your instance

-----> Converging <default-linux>...

Preparing files for transfer

Preparing dna.json

Resolving cookbook dependencies with Berkshelf 4.0.1...

Removing non-cookbook files before transfer

Preparing validation.pem

Preparing client.rb

...

> kitchen converge

Page 58: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Last login: Fri Dec 25 22:46:30 2015 from ip-10-100-20-236.ec2.internal

[centos@ip-10-100-20-167 ~]$

> kitchen login

Log onto your instance

Page 59: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

httpd-2.4.6-40.el7.centos.x86_64

> rpm -q httpd

Test your Changes

Page 60: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

> rpm -e httpd

Remove the Package Manually

Page 61: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Run Chef again to repair the machine...

-----> Converging <default-linux>...

Preparing files for transfer

Preparing dna.json

Resolving cookbook dependencies with Berkshelf 4.0.1...

Removing non-cookbook files before transfer

Preparing validation.pem

Preparing client.rb

...

> kitchen converge

Page 62: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

httpd-2.4.6-40.el7.centos.x86_64

> rpm -q httpd

Test your Changes

Page 63: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Commit Your Changes

After committing, your git status should be clean:

On branch master

nothing to commit, working directory clean

git status

git add .

git commit –m "Added a package resource."

Page 64: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Add a file resource

package 'httpd' do

action :install

end

file '/var/www/html/index.html' do

content 'Hello, world!'

end

~/chef-repo/cookbooks/web/recipes/server.rb

Page 65: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Converge your node again > kitchen converge

Page 66: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Hello world!

> cat /var/www/html/index.html

Test your Changes

Page 67: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Commit Your Changes

After committing, your git status should be clean:

On branch master

nothing to commit, working directory clean

git status

git add .

git commit –m "Added a file resource."

Page 68: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Add a service resource

package 'httpd' do

action :install

end

file '/var/www/html/index.html' do

content 'Hello, world!'

end

service 'httpd' do

action [ :enable, :start ]

end

~/chef-repo/cookbooks/web/recipes/server.rb

Page 69: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Converge your node again > kitchen converge

Page 70: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Test your Changes

Page 71: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Commit Your Changes

After committing, your git status should be clean:

On branch master

nothing to commit, working directory clean

git status

git add .

git commit –m "Added a service resource."

Page 72: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Managing Apache on Linux

Generate a web cookbook

Create an instance to test the cookbook against

Write the recipe to install the apache package

Update the recipe to start and enable the apache service

Page 73: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Bonus Exercise – Disaster Recovery

kitchen destroy

kitchen converge

Page 74: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Bonus Exercise – Time Travel

git log --oneline

git checkout f947881

git checkout master

You can go back to any previous commit if you need to!

Page 75: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Q&A

What questions can we answer for you?

Page 76: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean
Page 77: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Testing Cookbooks

Page 78: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objectives

After completing this module, you should be able to:

Write a test in ServerSpec

Verify the test passes with Test Kitchen

Page 79: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Test Kitchen

What does this test when kitchen converges a

recipe?

What does it NOT test when kitchen converges a

recipe?

Page 80: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Test Kitchen

What is left to validate to ensure that the cookbook

successfully applied the policy defined in the

recipe?

Page 81: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

The First Test

Converging seems to validate that the recipe runs successfully. But does it

assert what actually is installed?

Write a test to validate a working web server

Execute the test to see it pass

Page 82: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Add a ServerSpec Test

require 'spec_helper'

describe 'web::default' do

# Serverspec examples can be found at

# http://serverspec.org/resource_types.html

it 'does something' do

skip 'Replace this with meaningful tests'

end

describe command('curl localhost') do

its(:stdout) { should match(/ip-204-54-11-27/) }

end

end

.../web/test/integration/default/serverspec/default_spec.rb

-+

Page 83: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

The First Test

Converging seems to validate that the recipe runs successfully. But does it

assert what actually is installed?

Write a test to validate a working web server

Execute the test to see it pass

Page 84: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Test Your Instance

web::default

Command "curl localhost"

stdout

should match /Hello World!/

Finished in 0.18118 seconds (files took 0.26053 seconds to load)

1 example, 0 failures

> kitchen verify

Page 85: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Commit Your Changes

After committing, your git status should be clean:

On branch master

nothing to commit, working directory clean

git status

git add .

git commit –m "Wrote test to verify homepage is working"

Page 86: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

The First Test

Converging seems to validate that the recipe runs successfully. But does it

assert what actually is installed?

Write a test to validate a working web server

Execute the test to see it pass

Page 87: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Q&A

What questions can we answer for you?

Page 88: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean
Page 89: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Refactoring Your Recipe

Page 90: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objectives

After 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

Page 91: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Status Page – Hello World v2.0

We can do better than 'Hello, world!'

Update the default test page to include the host name and IP address of the system

Converge and verify that the web page displays the host name and IP address

Add other system data and an image to our homepage

Page 92: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

52.90.82.1

ip-10-100-20-167

curl http://169.254.169.254/latest/meta-data/public-ipv4

hostname

Find our Public IP Address & Hostname

Page 93: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Add the data to our recipe

package 'httpd' do

action :install

end

file '/var/www/html/index.html' do

content '

52.90.82.1

ip-10-100-20-167

'

end

service 'httpd' do

action [:start, :enable]

end

~/chef-repo/cookbooks/httpd/recipes/server.rb

Page 94: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Converge the Instance

...

- update content in file /var/www/html/index.html from 7f83b1 to 6228bf

--- /var/www/html/index.html 2015-12-25 22:50:09.497834997 +0000

+++ /var/www/html/.index.html20151225-12310-5fw0wy 2015-12-25

23:14:51.505056903 +0000

@@ -1,2 +1,4 @@

-Hello World!

+

+52.90.82.1

> kitchen converge

Page 95: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Run your Test> kitchen verify

Page 96: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Commit Your Changes

After committing, your git status should be clean:

On branch master

nothing to commit, working directory clean

git status

git add .

git commit –m "Changed index.html into status page"

Page 97: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Status Page – Hello World v2.0

Adding more system data to our status page

Update the default test page to include the host name and IP address of the system

Converge and verify that the web page displays the host name and IP address

Add other system data and an image to our homepage

Page 98: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

<html><body>

<img

src=https://upload.wikimedia.org/wikipedia/commons/thumb/1/1d/AmazonWebservices_Logo.svg/2000

px-AmazonWebservices_Logo.svg.png width=200>

<pre>

Hostname: ip-10-100-20-167

IP: 52.90.82.1

Memory: 1015944 kB

# CPUs: 1

Kernel: 3.10.0-229.14.1.el7.x86_64

OS Version: CentOS Linux release 7.1.1503 (Core)

</pre></body></html>

curl -L http://tinyurl.com/my-aws-sysinfo-sh -o sysinfo.sh

sh sysinfo.sh

Generate Content for our Homepage

Page 99: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Copy Pasta, Paisano!

Page 100: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Converge Again

...

- update content in file /var/www/html/index.html from 7f83b1 to 6228bf

--- /var/www/html/index.html 2015-12-25 22:50:09.497834997 +0000

+++ /var/www/html/.index.html20151225-12310-5fw0wy 2015-12-25

23:14:51.505056903 +0000

...

> kitchen converge

Page 101: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Run your Test> kitchen verify

Page 102: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Commit Your Changes

After committing, your git status should be clean:

On branch master

nothing to commit, working directory clean

git status

git add .

git commit –m "Added more data to status page."

Page 103: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Status Page – What's Next?

In the next chapter we'll make it more dynamic...

Update the default test page to include the host name and IP address of the system

Converge and verify that the web page displays the host name and IP address

Add other system data and an image to our homepage

Page 104: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Q&A

What questions can we answer for you?

Page 105: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean
Page 106: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Managing Files with Chef Templates

Learn to separate code from data

Page 107: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objectives

After 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 108: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Cleaner Apache Recipe

Adding all this HTML to the index page made it somewhat unwieldy and

harder to manage.

Create a template with chef generate

Define the contents of the ERB template

Change the file resource to the template resource in the 'web' cookbook

Page 109: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

How do we generate a template?> chef generate template --help

Page 110: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Generate a Template for our Homepage> chef generate template index.html

Page 111: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Clean up the code

Storing files inside a recipe is not efficient. Let's move all that code into the

template.

Create a template with chef generate

Define the contents of the ERB template

Change the file resource to the template resource in the 'apache' cookbook

Page 112: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Move the HTML into our new template

<html>

<body>

<imgsrc=https://upload.wikimedia.org/wikipedia/commons/thumb/1/1d/AmazonWebservices_Logo.svg/2000px-AmazonWebservices_Logo.svg.png width=200>

<pre>

Hostname: ip-10-100-20-229

IP: 54.84.76.148

Memory: 1015944 kB

# CPUs: 1

Kernel: 3.10.0-229.14.1.el7.x86_64

OS Version: CentOS Linux release 7.1.1503 (Core)

</pre>

</body>

</html>

~/chef-repo/cookbooks/web/templates/default/index.html.erb

Page 113: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Update your recipe to look like this:

package 'httpd' do

action :install

end

template '/var/www/html/index.html' do

action :create

source 'index.html.erb'

end

service 'httpd' do

action [ :enable, :start ]

end

~/chef-repo/cookbooks/web/recipes/default.rb

Page 114: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Converge the Instance

...

- update content in file /var/www/html/index.html from 7f83b1 to 6228bf

--- /var/www/html/index.html 2015-12-25 22:50:09.497834997 +0000

+++ /var/www/html/.index.html20151225-12310-5fw0wy 2015-12-25

23:14:51.505056903 +0000

...

> kitchen converge

Page 115: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Run your Test> kitchen verify

Page 116: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Update the Cookbook Version

Update the version of the 'web' cookbook's version for this patch

Commit the changes to the 'web' cookbook to version control

Page 117: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Bump your cookbook version

name 'web'

maintainer 'The Authors'

maintainer_email '[email protected]'

license 'all_rights'

description 'Installs/Configures web cookbook'

long_description 'Installs/Configures web cookbook'

version '0.2.0'

~/chef-repo/cookbooks/web/metadata.rb

Page 118: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Commit Your Changes

After committing, your git status should be clean:

On branch master

nothing to commit, working directory clean

git status

git add .

git commit –m "Moved HTML code into template, bumped version."

Page 119: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Q&A

What questions can we answer for you?

Page 120: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean
Page 121: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Chef Templates and Node Attributes

More refactoring! Let's make this recipe more dynamic.

Page 122: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objectives

After completing this module, you should be able to:

Use Ohai to gather data about your instance

Use ERB tags to display node data in a template

Create dynamic, automatically updating templates

Page 123: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objective:

Hard coded data

Our status page still has hard coded data in it. What if our instance gets

rebooted? What if we want to use this recipe on another node?

Use the Ohai tool to examine the state of our machine and its attributes

Remove hard-coded data from our template, replace with attributes

Test and verify that our homepage is updated dynamically

Page 124: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

6-

Ohai!

Ohai is a tool that already captures all the data that

we dug up manually in the previous sections.

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

©2015 Chef Software Inc. 125

Page 125: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

6-

Ohai!

{

"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 | less

©2015 Chef Software Inc. 126

Page 126: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

6-

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

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

©2015 Chef Software Inc. 127

Page 127: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

6-

ohai + chef-client = <3

chef-client and chef-apply 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

©2015 Chef Software Inc. 128

Page 128: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

6-

The Node Object

The node object is a representation of our system.

It stores all the attributes found about the system.

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

©2015 Chef Software Inc. 129

Page 129: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

6-

node

ipaddress hostname memory

total

cpu

0

mhz

...

IPADDRESS: 104.236.192.102

©2015 Chef Software Inc. 130

The Node

Page 130: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

6-

node

ipaddress hostname memory

total

cpu

0

mhz

...

HOSTNAME: banana-stand

©2015 Chef Software Inc. 131

The Node

Page 131: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

6-

node

ipaddress hostname memory

total

cpu

0

mhz

...

MEMORY: 502272kB

©2015 Chef Software Inc. 132

The Node

Page 132: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

6-

node

ipaddress hostname memory

total

cpu

0

mhz

...

CPU: 2399.998MHz

©2015 Chef Software Inc. 133

The Node

Page 133: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

7-

ERB

An 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

©2015 Chef Software Inc. 134

Page 134: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

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.

©2015 Chef Software Inc. 135

Page 135: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

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.

©2015 Chef Software Inc. 136

Page 136: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

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.

©2015 Chef Software Inc. 137

Page 137: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

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.

©2015 Chef Software Inc. 138

Page 138: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

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.

©2015 Chef Software Inc. 139

Page 139: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

7-

The Angry Squid

©2015 Chef Software Inc. 140

Page 140: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Update your template

Page 141: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Converge the Instance

...

- update content in file /var/www/html/index.html from 7f83b1 to 6228bf

--- /var/www/html/index.html 2015-12-25 22:50:09.497834997 +0000

+++ /var/www/html/.index.html20151225-12310-5fw0wy 2015-12-25

23:14:51.505056903 +0000

...

> kitchen converge

Page 142: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Update your template

Page 143: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Add node attributes for the other items:

node['memory']['total']

node['cpu']['total']

node['kernel']['release']

node['platform']

node['platform_version']

node['ec2']['placement_availability_zone']

Page 144: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Final index.html.erb template<html><body><img src=https://upload.wikimedia.org/wikipedia/commons/thumb/1/1d/AmazonWebservices_Logo.svg<pre>Hostname: <%= node['hostname'] %>IP: <%= node['cloud']['public_ipv4'] %>Memory: <%= node['memory']['total'] %># CPUs: <%= node['cpu']['total'] %>Kernel: <%= node['kernel']['release'] %>OS Version: <%= node['platform'] -%> <%= node['platform_version'] %>Zone: <%= node['ec2']['placement_availability_zone'] %>

</pre>

</body></html>

Page 145: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Converge the Instance

...

- update content in file /var/www/html/index.html from 7f83b1 to 6228bf

--- /var/www/html/index.html 2015-12-25 22:50:09.497834997 +0000

+++ /var/www/html/.index.html20151225-12310-5fw0wy 2015-12-25

23:14:51.505056903 +0000

@@ -1,2 +1,4 @@

-Hello World!

+

+52.90.82.1

> kitchen converge

Page 146: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Run your Test> kitchen verify

Page 147: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Commit Your Changes

After committing, your git status should be clean:

On branch master

nothing to commit, working directory clean

git status

git add .

git commit –m "Converted template to use dynamic attributes"

Page 148: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Q&A

What questions can we answer for you?

Page 149: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean
Page 150: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

User-defined Attributes

What if we need custom attributes to describe our infrastructure?

Page 151: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Objectives

After completing this module, you should be able to:

Generate a chef default attributes file

Define custom attributes that you can use in templates

Build a simple web app with custom attributes

Page 152: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

How do we generate an attributes file?> chef generate attribute --help

Page 153: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Generate a default attributes file

Compiling Cookbooks...

Recipe: code_generator::attribute

* directory[C:/Users/Administrator/chef-repo/cookbooks/web/attributes] action create

- create new directory C:/Users/Administrator/chef-repo/cookbooks/web/attributes

* template[C:/Users/Administrator/chef-repo/cookbooks/web/attributes/default.rb] action create

- create new file C:/Users/Administrator/chef-repo/cookbooks/web/attributes/default.rb

- update content in file C:/Users/Administrator/chef-repo/cookbooks/web/attributes/default.rb from

none to e3b0c4

(diff output suppressed by config)

> chef generate attribute default

Page 154: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Fetch some code for your webapp

http://www.kongregate.com/games_for_your_site

Page 155: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Insert the code into your template

Page 156: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Converge the Instance

...

- update content in file /var/www/html/index.html from 7f83b1 to 6228bf

--- /var/www/html/index.html 2015-12-25 22:50:09.497834997 +0000

+++ /var/www/html/.index.html20151225-12310-5fw0wy 2015-12-25

23:14:51.505056903 +0000

...

> kitchen converge

Page 157: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Edit the default.rb attributes file:

default['web']['width'] = '1280'

default['web']['height'] = '768'

default['web']['base'] = 'http://external.kongregate-

games.com/gamez/0021/4044/live/'

default['web']['src'] = 'http://external.kongregate-

games.com/gamez/0021/4044/live/embeddable_214044.swf'

Page 158: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Update your template with the new attributes:

Page 159: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Converge the Instance

...

- update content in file /var/www/html/index.html from 7f83b1 to 6228bf

--- /var/www/html/index.html 2015-12-25 22:50:09.497834997 +0000

+++ /var/www/html/.index.html20151225-12310-5fw0wy 2015-12-25

23:14:51.505056903 +0000

...

> kitchen converge

Page 160: Chef Essentials on AWSschd.ws/hosted_files/techsummit2017/fb/Chef- Cooking up... · Generate a Chef cookbook ... On branch master nothing to commit, working directory clean

Test your app in a browser