chef-solo with vagrant

To learn the concepts of chef, we can start by using chef-solo with Vagrant.
See my previous post on Vagrant to install vagrant and know more about it.

Next, lets install chef-solo on our machine.
We will install chef-solo using ruby gem,(for both Linux and Mac) make sure you have ruby installed.

root@intro:~# cd ~
root@intro:~# sudo gem install chef
Thank you for installing Chef!

So, now we have installed chef-solo and vagrant on our machine.
In this exercise, we will try and install apache2 on an ubuntu virtual machine (virtual box)
using chef-solo and vagrant.
To begin we would first need to understand few concepts; which would be required to run chef-solo in Vagrant.

run-lists : The first thing which we need to know is run-lists.
Run-list is like a playList for a node. It tells the node which recipes are needed to be run and in which order. This is always specific to the node on which it runs.
It is important to note that the chef-client always configures a node in the exact order specified by its run-list and will never run the same recipe twice.

So, if we want to install apache2 on a node:
– We would first need to put this recipe in the cookbook and configure cookbooks path.
– Then mention it in the run list.

Lets see how to do it:

1.) Configure recipes and cookbooks:

Recipes in the cookbook are maintained using knife. knife is a command line tool for chef.

Where to put your recipes?
Recipes reside in the cookbooks folder. By default if cookbooks folder is in the same level as Vagrantfile then we need not define explicitly the cookbooks path.
But it is a good practice to maintain a chef-repo, and place the cookbooks folder inside it.
So we will need to create the chef repository in our local.
Chef repository is the place where we will keep all our cookbooks and recipes.
In order to create a chef-repo we can use the already made directory structure provided by chef.

root@intro:~# wget
root@intro:~# tar -zxf master
root@intro:~# mv opscode-chef-repo* chef-repo
root@intro:~# rm master

This will create a chef-repo folder with all required objects inside it. We will discuss in detail later what are the folder/files inside the chef-repo and their use. Now we just need to concentrate on the cookbooks folder.

Inside the Vagrantfile we would need to define this path as :

config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = "~/Projects/chef-repo/cookbooks"

After configuring the cookbooks folder and specified its path, lets put some recipes inside it. Lets put the recipe of apache http server using knife. For that we need to
execute this command :

knife cookbook site download apache2

This command will download the compressed recipe folder, extract this folder in the cookbooks folder.( We can download the tar.gz anywhere but it needs to be extracted in cookbooks folder.)

Anirudhs-MacBook-Pro:cookbooks xebia$ tar -xvzf apache2-1.7.0.tar.gz

Next step would be to add this to the run-list.

2.) Adding Recipe to run list:
To add a recipe in the run-list we just need to add it using chef.add_recipe “xxx”

Lets see the example : [this is in Vagrantfile]

Vagrant.configure("2") do |config|
  config.vm.provision "chef_solo" do |chef|
    chef.add_recipe "apache"

So, to sum up we have done these changes :

1.)configured chef-repo
2.)provided cookbooks path to chef
3.)downloaded a recipe using knife
4.)added the recipe in cookbooks
5.)added this recipe in run-list

Finally or Vagrantfile looks like this :

Vagrant.configure("2") do |config| = "precise32"
    config.vm.box_url = "" :forwarded_port, guest: 80, host: 8888
    config.vm.provision :chef_solo do |chef|
        chef.add_recipe "apache2"
        chef.json = { :apache => { :default_site_enabled => true } }

Note the line “ :forwarded_port, guest: 80, host: 8888″ this is added to forward the incoming requests on port 80 (default HTTP ) to port 8888 (where apache http server is listening)

Next step is do a vagrant up and hit the browser with localhost:8888 to see the page showing famous ït works” page.


2 thoughts on “chef-solo with vagrant

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.