chef for beginners module 4

32
chef-client Applying Recipes from Cookbooks Slide 1 of 32

Upload: chef

Post on 15-Apr-2017

225 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Chef for beginners   module 4

chef-clientApplying Recipes from Cookbooks

Slide 1 of 32

Page 2: Chef for beginners   module 4

After completing this module, you should be able to usechef-client to:

Locally apply a cookbook's recipe with chef-client.Locally apply multiple cookbooks' recipes with chef-client.Include a recipe from within another recipe.

Objectives

Slide 2 of 32

Page 3: Chef for beginners   module 4

chef-apply is a great tool for applying resources (-e)and for individual recipes but it doesn't know how toapply a cookbook.

chef-apply

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

Slide 3 of 32

Page 4: Chef for beginners   module 4

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

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

chef-client

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

Slide 4 of 32

Page 5: Chef for beginners   module 4

chef-client's default mode attempts to contact a ChefServer and ask it for the recipes to run for the givennode.

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

--local-mode

Slide 5 of 32

Page 6: Chef for beginners   module 4

In local mode, we need to provide a list of recipes toapply to the system. This is called a run list. A run listis an ordered collection of recipes to execute.

Each recipe in the run list must be addressed with theformat recipe[cookbook-name::recipe-name].

-r "recipe[cookbook-name::recipe-name]"

Slide 6 of 32

Page 7: Chef for beginners   module 4

When you are referencing the default recipe within acookbook you may optionally specify only the name ofthe cookbook.

chef-client understands that you mean to apply thedefault recipe from within that cookbook.

-r "recipe[cookbook-name(::default)]"

Slide 7 of 32

Page 8: Chef for beginners   module 4

Example UsageUsing chef-client to locally apply the setup recipe from the workstationcookbook.

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

Applying the following recipes locally: The 'setup' recipe from the 'workstation' cookbook

Slide 8 of 32

Page 9: Chef for beginners   module 4

Example UsageUsing chef-client to locally apply the server recipe from the apache cookbook.

$ sudo chef-client --local-mode -r "recipe[apache::server]"

Applying the following recipes locally: The 'server' recipe from the 'apache' cookbook

Slide 9 of 32

Page 10: Chef for beginners   module 4

Example UsageUsing chef-client to locally apply multiple recipes from multiple cookbooks.

$ sudo chef-client --local-mode \ -r "recipe[workstation::setup],recipe[apache::server]"

Applying the following recipes locally: * The 'setup' recipe from the 'workstation' cookbook * The 'server' recipe from the 'apache' cookbook

Slide 10 of 32

Page 11: Chef for beginners   module 4

Group Exercise: Return Home First$ cd ~

Slide 11 of 32

Page 12: Chef for beginners   module 4

Group Exercise: Apply the apache::server Recipe Locally$ sudo chef-client --local-mode -r "recipe[apache::server]"

[2015-09-15T14:52:45+00:00] WARN: No config file found or specified on command line, using command line options.[2015-09-15T14:52:45+00:00] WARN: No cookbooks directory found at or above current directory. Assuming /home/chef.Starting Chef Client, version 12.3.0resolving cookbooks for run list: ["apache::server"]

================================================================================Error Resolving Cookbooks for Run List:================================================================================

Slide 12 of 32

Page 13: Chef for beginners   module 4

Group Exercise: Apply the apache::server Recipe Locally$ sudo chef-client --local-mode -r "recipe[apache::server]"

[2015-09-15T14:52:45+00:00] WARN: No config file found or specified on command line, using command line options.[2015-09-15T14:52:45+00:00] WARN: No cookbooks directory found at or above current directory. Assuming /home/chef.Starting Chef Client, version 12.3.0resolving cookbooks for run list: ["apache::server"]

================================================================================Error Resolving Cookbooks for Run List:================================================================================

FAIL

Slide 13 of 32

Page 14: Chef for beginners   module 4

Group Exercise: Create a cookbooks directorychef-client requires the cookbooks to be in a cookbooks directory locatedin the user's home directory.

$ cd ~$ mkdir cookbooks$ mv workstation cookbooks$ mv apache cookbooks

Slide 14 of 32

Page 15: Chef for beginners   module 4

Group Exercise: Apply the apache::server Recipe Locally$ sudo chef-client --local-mode -r "recipe[apache::server]"

[2015-09-15T14:54:45+00:00] WARN: No config file found or specified on command line, using command line options.Starting Chef Client, version 12.3.0resolving cookbooks for run list: ["apache::server"]Synchronizing Cookbooks: - apacheCompiling Cookbooks...Converging 4 resourcesRecipe: apache::server * yum_package[httpd] action install (up to date) * file[/var/www/html/index.html] action create (up to date) * service[httpd] action enable (up to date)

Slide 15 of 32

Page 16: Chef for beginners   module 4

Group Exercise: Apply the workstation::setup Recipe Locally$ sudo chef-client --local-mode -r "recipe[workstation::setup]"

[2015-09-15T15:15:26+00:00] WARN: No config file found or specified on command line, using command line options.Starting Chef Client, version 12.3.0resolving cookbooks for run list: ["workstation::setup"]Synchronizing Cookbooks: - workstationCompiling Cookbooks...Converging 6 resourcesRecipe: workstation::setup * yum_package[nano] action install (up to date) * yum_package[vim] action install (up to date) * yum_package[emacs] action install (up to date)

Slide 16 of 32

Page 17: Chef for beginners   module 4

Group Exercise: Apply Both Recipes Locally$ sudo chef-client --local-mode \ -r "recipe[apache::server],recipe[workstation::setup]"

[2015-09-15T15:17:27+00:00] WARN: No config file found or specified on command line, using command line options.Starting Chef Client, version 12.3.0resolving cookbooks for run list: ["apache::server","workstation::setup"]Synchronizing Cookbooks: - apache - workstationCompiling Cookbooks...

Running handlers:[2015-09-15T15:17:30+00:00] ERROR: Running exception handlersRunning handlers complete

Slide 17 of 32

Page 18: Chef for beginners   module 4

A recipe can include one (or more) recipes located incookbooks by using the include_recipe method.When a recipe is included, the resources found in thatrecipe will be inserted (in the same exact order) at thepoint where the include_recipe keyword is located.

include_recipe

https://docs.chef.io/recipes.html#include-recipes

Slide 18 of 32

Page 19: Chef for beginners   module 4

Example Usage: Including a RecipeInclude the setup recipe from the workstation cookbook in this recipe.

include_recipe 'workstation::setup'

Slide 19 of 32

Page 20: Chef for beginners   module 4

Example Usage: Including a recipeInclude the server recipe from the apache cookbook in this recipe.

include_recipe 'apache::server'

Slide 20 of 32

Page 21: Chef for beginners   module 4

Group Exercise: The default recipe includes the setup recipe~/cookbooks/workstation/recipes/default.rb

## Cookbook Name:: workstation# Recipe:: default## Copyright (c) 2015 The Authors, All Rights Reserved.

include_recipe 'workstation::setup'

Slide 21 of 32

Page 22: Chef for beginners   module 4

Group Exercise: Apply the cookbook's default recipe$ sudo chef-client --local-mode -r "recipe[workstation]"

WARN: No config file found or specified on command line, using command line options.Starting Chef Client, version 12.3.0resolving cookbooks for run list: ["workstation"]Synchronizing Cookbooks: - workstationCompiling Cookbooks...Converging 0 resources

Running handlers:Running handlers completeChef Client finished, 0/0 resources updated in 3.300489827 seconds

Slide 22 of 32

Page 23: Chef for beginners   module 4

Group Exercise: Commit Your Work$ cd workstation$ git add .$ git commit -m "Default recipe includes the setup recipe"

Slide 23 of 32

Page 24: Chef for beginners   module 4

Update the apache cookbook's default recipe to:

Include the server recipe from theapache cookbook

Run chef-client and locally apply the run list:recipe[apache]Commit the changes with version control

Lab: Update the apache Cookbook

Slide 24 of 32

Page 25: Chef for beginners   module 4

Lab: The default recipe includes the apache recipe~/cookbooks/apache/recipes/default.rb

## Cookbook Name:: apache# Recipe:: default## Copyright (c) 2015 The Authors, All Rights Reserved.

include_recipe 'apache::server'

Slide 25 of 32

Page 26: Chef for beginners   module 4

Lab: Applying the apache default recipe$ sudo chef-client --local-mode -r "recipe[apache]"

[2015-09-15T15:23:18+00:00] WARN: No config file found or specified on command line, using command line options.Starting Chef Client, version 12.3.0resolving cookbooks for run list: ["apache"]Synchronizing Cookbooks: - apacheCompiling Cookbooks...Converging 0 resources

Running handlers:Running handlers completeChef Client finished, 0/0 resources updated in 3.310768509 seconds

Slide 26 of 32

Page 27: Chef for beginners   module 4

Lab: Commit Your Work$ cd apache$ git add .$ git commit -m "Default recipe includes the server recipe"

Slide 27 of 32

Page 28: Chef for beginners   module 4

Why would you want to apply more than one recipe ata time?

Discussion

Slide 28 of 32

Page 29: Chef for beginners   module 4

Why would you want to apply more than one recipe ata time?

What are the benefits and drawbacks of usinginclude_recipe within a recipe?

Discussion

Slide 29 of 32

Page 30: Chef for beginners   module 4

Why would you want to apply more than one recipe ata time?

What are the benefits and drawbacks of usinginclude_recipe within a recipe?

Do default values make it easier or harder to learn?

Discussion

Slide 30 of 32

Page 31: Chef for beginners   module 4

Q&AWhat questions can we help you answer?

chef-clientlocal moderun listinclude_recipe

Slide 31 of 32

Page 32: Chef for beginners   module 4

On to Module 5

Slide 32 of 32