Skip to content

Instantly share code, notes, and snippets.

@cfreeman
Last active January 16, 2016 07:41
Show Gist options
  • Save cfreeman/5105473 to your computer and use it in GitHub Desktop.
Save cfreeman/5105473 to your computer and use it in GitHub Desktop.
Cheat sheet for getting started with chef.

Chef Cheat sheet

Configuring Chef

To install stuff needed to run chef in a solo mode (Small one or two server configuration):

gem install knife-solo

To setup chef after it has been installed:

knife configure -r . --defaults	

How to Configure Virtual Box for testing Chef

Sidebar - in order to create a host-only network in virtual box, to make it easy to SSH into it from the host machine.

  • VirtualBox -> Preferences

  • Select Network

  • Push network card with "+" this creates a new Host-only network.

  • With the VM instance OFF:

    • Select VM Settings.
    • Select Adapter 2.
    • Toggle Enable Network Adapter.
    • Set Attached To: Host-only Adapter.
    • Name: The name of the host-only network created previously - default vboxnet0
    • Power up the VM.
  • You also need to make sure that the host-only network doesn't conflict with the IP address set to what you plan on using below.

    • Select the host-only adapter in VM settings.
    • Edit, make sure it doesn't conflict with the desired address (192.168.56.105 for the below example).
  • Configure operating system as per normal. For Ubuntu:

  • sudo vim /etc/network/interfaces

  • Add the following: auto eth1 iface eth1 inet static address 192.168.56.101 netmask 255.255.255.0

Cooking with Chef

To create a new 'kitchen' or chef project:

knife kitchen my_kitchen

Cookbooks, or predefined scripts for setting up a whole ton of cookbooks from different serverside tools can be found on github. However, before you can install cookbooks you must initialise your new kitchen as a git repository:

cd my_kitchen
git init
git add cookbooks/
git add data_bags/
git add nodes/
git add roles/
git add site-cookbooks/
git add solo.rb
git commit -a -m "Created new kitchen."

Then installing a particular preconfigured cookbook:

knife cookbook site install postgresql

To create a cookbook:

knife cookbook create widget

The cookbook then needs to be added to a run list. Run lists are on a server per server (node per node) basis. To prepare a new server (node) to receive a chef cookbook and create a run list:

knife solo prepare cfree@192.168.56.101	

To prepare a server with a different version of Chef (say to avoid compatibility cookbook compatibility issues) pass the omnibus version in:

knife solo prepare cfree@192.168.56.101 --omnibus-version 10.24.0

Then to add postgresql to the run list, edit the newly created json file in nodes:

vim nodes/192.168.56.101.json

It should contain the following, the run list instructs chef what recipes to invoke on the server when you cook, while the postgresql element is a configuration element required by the postgresql cookbook:

{ 
	"postgresql":{"password":{"postgres":"test"}},
	"run_list": ["recipe[postgresql::server]"] 
}

Then cook the server using the following command:

knife solo cook cfree@192.168.56.101	
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment