Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Chef Infrastructure Automation Cookbook Second Edition

You're reading from   Chef Infrastructure Automation Cookbook Second Edition Over 80 recipes to automate your cloud and server infrastructure with Chef and its associated toolset

Arrow left icon
Product type Paperback
Published in May 2015
Publisher
ISBN-13 9781785287947
Length 278 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Matthias Marschall Matthias Marschall
Author Profile Icon Matthias Marschall
Matthias Marschall
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Chef Infrastructure FREE CHAPTER 2. Evaluating and Troubleshooting Cookbooks and Chef Runs 3. Chef Language and Style 4. Writing Better Cookbooks 5. Working with Files and Packages 6. Users and Applications 7. Servers and Cloud Infrastructure Index

Developing recipes with local mode

If running your own Chef server seems like an overkill and you're not comfortable with using the hosted Chef, you can use local mode to execute cookbooks.

Getting ready

  1. Create a cookbook named my_cookbook by running the following command:
    mma@laptop:~/chef-repo $ chef generate cookbook cookbooks/my_cookbook
    
    Compiling Cookbooks...
    Recipe: code_generator::cookbook
    ...TRUNCATED OUTPUT...
  2. Edit the default recipe of my_cookbook so that it creates a temporary file:
    mma@laptop:~/chef-repo $ subl cookbooks/my_cookbook/recipes/default.rb
    
    file "/tmp/local_mode.txt" do
        content "created by chef client local mode"
    end

How to do it...

Let's run my_cookbook on your local workstation using Chef client's local mode:

  1. Run the Chef client locally with my_cookbook in the run list:
    mma@laptop:~/chef-repo $ chef-client -z -o my_cookbook
    
    [2014-12-11T22:54:44+01:00] INFO: Starting chef-zero on host localhost, port 8889 with repository at repository at /Users/mma/work/chef-repo
    [2014-12-11T22:54:44+01:00] INFO: Forking chef instance to converge...
    Starting Chef Client, version 11.18.0.rc.1
    [2014-12-11T22:54:44+01:00] INFO: *** Chef 11.18.0.rc.1 ***
    [2014-12-11T22:54:44+01:00] INFO: Chef-client pid: 20179
    [2014-12-11T22:54:47+01:00] WARN: Run List override has been provided.
    [2014-12-11T22:54:47+01:00] WARN: Original Run List: []
    [2014-12-11T22:54:47+01:00] WARN: Overridden Run List: [recipe[my_cookbook]]
    [2014-12-11T22:54:47+01:00] INFO: Run List is [recipe[my_cookbook]]
    [2014-12-11T22:54:47+01:00] INFO: Run List expands to [my_cookbook]
    [2014-12-11T22:54:47+01:00] INFO: Starting Chef Run for webops
  2. Validate that the Chef client run creates the desired temporary file:
    mma@laptop:~/chef-repo $ cat /tmp/local_mode.txt
    
    created by chef client local mode%

How it works...

The -z parameter switches the Chef client into local mode. Local mode uses chef-zero—a simple, in-memory version of the Chef server provided by Chef DK—when converging the local workstation.

By providing the -o parameter, you override the run list of your local node so that the Chef client executes the default recipe from my_cookbook.

There's more...

Chef-zero saves all modifications made by your recipes to the local file system. It creates a JSON file containing all node attributes for your local workstation in the nodes directory. This way, the next time you run the Chef client in local mode, it will be aware of any changes your recipes made to the node.

Running knife in local mode

You can use knife in local mode, too. To set the run list of your node named laptop (instead of having to override it with -o), you can run the following command:

mma@laptop:~/chef-repo $ knife node run_list add -z laptop 'recipe[my_cookbook]'

Moving to hosted Chef or your own Chef server

When you're done editing and testing your cookbooks on your local workstation with chef-zero, you can seamlessly upload them to hosted Chef or your own Chef server:

mma@laptop:~/chef-repo $ knife upload /
laptop:
  run_list: recipe[my_cookbook]

See also

You have been reading a chapter from
Chef Infrastructure Automation Cookbook Second Edition
Published in: May 2015
Publisher:
ISBN-13: 9781785287947
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image