How to work with Chef using Oracle VirtualBox and Vagrant on a Windows host – Part II

In the previous series of this article i.e. Part I, we learned how to install Oracle VirtualBox, Vagrant, Chef Development Kit on Windows machine. We have downloaded Chef Repo and configured Knife to manage our Chef cookbooks. We learned how to create cookbooks using knife command. Now we will start developing of our Chef Cookbook to configure LAMP environment and Install WordPress on the guest host. The cookbook will responsible for the below things.

  • Install and configure Apache
  • Install and configure MySQL
  • Install and configure PHP
  • Deploy WordPress to the site

As you remember that we have downloaded the Chef Repo in the D:/ drive of the system and created a cookbook named “LinuxFunda” in our previous article i.e. Part I. Now go to that directory and we will start developing our wrapper cookbook.

Now you must thinking about the word “wrapper cookbook”. What is it ? You need to understand that what does it mean before developing it.

A wrapper cookbook wraps an upstream cookbook to change its behavior without forking it.

There are two main reasons you might want to do this:

  • Codifying the standard settings for your organization or business unit’s use of that cookbook without placing those attributes in a role
  • Modifying the behavior of an upstream cookbook.

Now lets enter in to the cookbook folder which we created in the previous article and see what is inside the folder.

Recipes

A recipe is the main workhorse of the cookbook. A cookbook can contain more than one recipe, or depend on outside recipes. Recipes are used to declare the desired state of different resources.

Resources can be of many different types

  • package: Used to manage packages on a node
  • service: Used to manage services on a node
  • user: Manage users on the node
  • group: Manage groups
  • template: Manage files with embedded ruby templates
  • cookbook_file: Transfer files from the files subdirectory in the cookbook to a location on the node
  • file: Manage contents of a file on node
  • directory: Manage directories on node
  • execute: Execute a command on the node
  • cron: Edit an existing cron file on the node

Attributes

Attributes in Chef are basically settings. Think of them as simple key-value pairs for anything you might want to use in your cookbook.

Files

The files sub-directory within the cookbook contains any static files that we will be placing on the nodes that use the cookbook.

Templates

Templates are similar to files, but they are not static. Template files end with the  .erb extension, meaning that they contain embedded Ruby.

Metadata.rb

The metadata.rb file is used, not surprisingly, to manage the metadata about a package. This includes things like the name of the package, a description, etc. It also includes things like dependency information, where you can specify which cookbooks this cookbook needs to operate. This will allow the Chef server to build the run-list for the nodes correctly and ensure that all of the pieces are transferred correctly.

Let’s start. We will use some community cookbooks to install Apache webserver, PHP and MySQL. You can find the community cookbooks here. http://community.opscode.com/cookbooks. Start downloading them to use in our wrapper cookbook.

Now let’s go back to our cookbook

Open metadata.rb in your favourite text editor. As I am using Git bash I will use vi editor. You can use notepad ++. The file should look like below.

Create a recipe named recipes/apache.rb and tell it that our cookbook relies on apache2 cookbook function and save the file. The file should look like below.

Open the recipes/default.rb and include the recipe apache.rb to it. The file should look like below.

Now if we run the cookbook LinuxFunda we should able to install Apache webserver. Let’s test our cookbook by running it on a VM.

Create a folder named vagrant/LinuxFunda inside the Chef-Repo folder.

It’s time to use Vagrant to create Virtual instances for us. Issue the below command to initiate a virtual machine.

Now you should able to see a file named “Vagrantfile” inside the folder

Edit that file and make that as below.

If you want to know more about Vagrant configuration you can read more here. https://docs.vagrantup.com/v2/

Now time to start our Virtual Box and verify. Issue the below command to start the VM.

WoW!! We are done with Apache webserver installation using Chef. Time to verify, open your browser in the windows machine and type http://localhost/. You should able to see a page like below.

Chef_Config_1

So now we learned how to use community cookbooks to create a wrapper cookbook.  We will cover rest of the things in our next article i.e. Part III.

The following two tabs change content below.

Tapas Mishra

Sr. Engineer (DevOps)
Loves to work on Opensource products. Having experience on Linux environment. Knowledge on Public cloud services like AWS, Rackspace, DigitalOcean, Linode. Please don't hesitate to give a comment on the posts. Your comments are my strength.

One thought on “How to work with Chef using Oracle VirtualBox and Vagrant on a Windows host – Part II

Leave a Reply