Skip to content

Instantly share code, notes, and snippets.

@savishy
Last active March 14, 2018 08:34
Show Gist options
  • Save savishy/fa0ee1c5208144709c50b66485717ae0 to your computer and use it in GitHub Desktop.
Save savishy/fa0ee1c5208144709c50b66485717ae0 to your computer and use it in GitHub Desktop.
Chef Cheatsheet

References and Reading

  1. Super-quick start
  2. Another great Chef Gist that describes a Local Development Workflow

Jargon

  • ohai == Facter from Puppet
  • Chef Client and Chef Server: Behave similar to Puppet Master and Puppet Agent.
  • Chef DK: A quick start toolbox for first-time use.
  • Test Kitchen: Enables you to test and debug your cookbooks against virtualized environments.
  • ChefSpec: Chef supports testing of your cookbooks. Chefspec is a tool similar to RSpec but modified to fit Chef.

Installing Chef Server

  1. Create an Ubuntu 16.04 VM
  2. Open up ports 80 and 443
  3. Follow this guide.
  4. Install Management Console (also in above link).
  5. Make sure everything is up: sudo chef-server-ctl status and sudo chef-manage-ctl status
  6. Access the Web Management Console at https://IP Addr

If you encounter 502 Bad Gateway, use this StackOverflow.

Kitchen Commands


  kitchen console                                 # Kitchen Console!
  kitchen converge [INSTANCE|REGEXP|all]          # Change instance state to ...
  kitchen create [INSTANCE|REGEXP|all]            # Change instance state to ...
  kitchen destroy [INSTANCE|REGEXP|all]           # Change instance state to ...
  kitchen diagnose [INSTANCE|REGEXP|all]          # Show computed diagnostic ...
  kitchen doctor INSTANCE|REGEXP                  # Check for common system p...
  kitchen exec INSTANCE|REGEXP -c REMOTE_COMMAND  # Execute command on one or...
  kitchen help [COMMAND]                          # Describe available comman...
  kitchen init                                    # Adds some configuration t...
  kitchen list [INSTANCE|REGEXP|all]              # Lists one or more instances
  kitchen login INSTANCE|REGEXP                   # Log in to one instance
  kitchen package INSTANCE|REGEXP                 # package an instance
  kitchen setup [INSTANCE|REGEXP|all]             # Change instance state to ...
  kitchen test [INSTANCE|REGEXP|all]              # Test (destroy, create, co...
  kitchen verify [INSTANCE|REGEXP|all]            # Change instance state to ...
  kitchen version                                 # Print Kitchen's version i...```

Getting Started (Local Development Workflow)

Prerequisites

  • Vagrant
  • Virtualbox
  • Chef DK
  • Windows OS
  • A good text editor like Atom.

First run the quickstart example

Follow the commands in the quick-start guide above.

chef generate app first_cookbook
cd first_cookbook/

Edit the file cookbooks\first_cookbook\recipes\default.rb and add the content:

file "#{ENV['HOME']}/test.txt" do
  content 'This file was created by Chef!'
end

Save the file. Run

 chef-client --local-mode --override-runlist first_cookbook

Expected Result: A file test.txt should be created in your %HOME% directory.

Explore the autogenerated skeleton

The chef generate command created a lot of stub files.

│   .kitchen.yml # Contains a default Test Kitchen config with 2 test VMs. 
│   README.md
│
├───.kitchen # Is automatically generated when you run kitchen commands.
│   └───logs
│           default-centos-7.log
│           default-ubuntu-1604.log
│           kitchen.log
│
├───cookbooks # Contains the cookbook files for "first_cookbook".
│   └───first_cookbook
│       │   Berksfile
│       │   chefignore
│       │   metadata.rb
│       │
│       ├───recipes
│       │       default.rb  # This is where you add a recipe.
│       │
│       └───spec
│           │   spec_helper.rb
│           │
│           └───unit
│               └───recipes
│                       default_spec.rb # 
│
├───nodes
│       DGSLBLRHNW1.json
│
└───test
    └───smoke
        └───default
                default_test.rb

.kitchen.yml

  • The file should have been autogenerated by your chef generate command.
  • By default it contains two platforms entries. These are test environments where your cookbook will be tested.
  • IF you run kitchen list you will see the output below.
kitchen list
Instance             Driver   Provisioner  Verifier  Transport  Last Action    Last Error
default-ubuntu-1604  Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>
default-centos-7     Vagrant  ChefZero     Inspec    Ssh        <Not Created>  <None>

Run kitchen create

This will now create the VMs specified in our .kitchen.yml.

  • During VM creation, Vagrant + Virtualbox is used. Errors in Vagrant box download, or creation of VM, will cause the kitchen create command to fail.

Run kitchen converge

In general, use the test subcommand to verify the end-to-end quality of a cookbook. Use the converge and verify subcommands during the normal the day-to-day development of a cookbook. (from https://docs.chef.io/ctl_kitchen.html)

Run Chef in Chef-Zero mode with a cookbook

Reference

chef-client -z -c .\client.rb -o provisioner

What we did:

  • -z runs in Chef Zero mode i.e. without requiring an existing Chef Server.
  • -c loads the Chef client configuration from the client.rb file located in this directory.
  • -o provisioner creates a Run List with one cookbook. The cookbook provisioner is loaded from the cookbooks/ directory.

Use Knife to add a client

Most commands using Knife require you to pass in the username -u and the associated private key file --key. This is a user you would have created previously, perhaps using chef-server-ctl commands.

$ knife client create someclient -c ./client.rb -a --key /c/chef/vish.pem -u vish
  • -a means the client is an admin client.

List Existing Clients

$ knife client list -c ./client.rb --key /c/chef/vish.pem -u vish
someclient
someorg-validator

To be clarified

  • Normal Client vs Validator Client
  • Client vs User
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment