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"
    ....
    end

    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"
      end
    end

    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 } }
        end
    end

    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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


− 3 = six



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
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.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close