About Anirudh Bhatnagar

Anirudh is a Java programmer with extensive experience in building Java/J2EE applications. He has always been fascinated by the new technologies and emerging trends in software development. He has been involved in propagating these changes and new technologies in his projects. He is an avid blogger and agile enthusiast who believes in writing clean and well tested code.

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, 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 http://github.com/opscode/chef-repo/tarball/master
    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|
        config.vm.box = "precise32"
        config.vm.box_url = "http://files.vagrantup.com/precise32.box"
        config.vm.network :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 “config.vm.network :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.

Reference: chef-solo with vagrant from our JCG partner Anirudh Bhatnagar at the anirudh bhatnagar blog.
Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Leave a Reply

+ 2 = seven

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books