@OracleIMC Partner Resources & Training: Discover your Modernization options + Reach new potential through Innovation

  • October 4, 2017

Oracle Infrastructure Automation with Chef

Gokhan Goksu
Technology Consultant
Chef is one of the most popular configuration management tools that automates the building of your infrastructure especially if you are working with many servers. In this blog post, I am going to show you how you can automate the installation of your VM instance hosted on Oracle IaaS with Chef. The scenario will be based on installing and running Apache web server in Oracle Linux VM.

Here are the main steps that I will cover;

  • Installing Chef development kit
  • Using the hosted Chef server
  • Creating a cookbook and recipe
  • Uploading the cookbook to Chef server
  • Registering target node to Chef server

Installing Chef Development Kit

Chef is a client-server technology where you install a development kit that includes required chef commands to work with the server. The server part is not mandatory if you are willing to work in local mode and store your recipes on your computer. However, in this post, I am going to use the hosted Chef environment which I'll explain later. Now let's get back to installing our development kit. I am using Ubuntu 16.04 for my client. Go to https://downloads.chef.io/chefdk and download the corresponding package to your computer.

Install the package with the following command sudo dpkg -i chef_13.4.24-1_amd64.deb You can verify the installation by running the following command. It outputs 13.4.24 in my case. chef-client -v

Using the Hosted Chef Server

As I mentioned earlier you can go without having a server but it is much better to have a centralized place for your configuration data, recipes and cookbooks so you can access them from anywhere and monitor your servers from the Chef server. In this scenario, I am going to use the hosted environment. First, create an account on https://manage.chef.io When you log into your account, the first thing to do is to create an organization. Choose a name as you like and proceed with the creation. The Chef server also provides a Starter Kit to make it easier to work with recipes and cookbooks. Download it from the left side menu by heading to Starter Kit > Download Starter Kit and unzip it. I am going to call this directory chef-repo

When you are inside the chef-repo, you can validate your authentication to the Chef server by knife client list If it outputs <your-organization-name>-validator, that means you are successfully authenticated to the Chef server.

Creating a Cookbook and Recipe

Now we are going to create a cookbook that will hold our server recipe which will install and run Apache web server. We will create the recipe shortly. Under the chef-repo folder, run the following command to create a cookbook first. It will be placed under cookbooks folder with a name apache. chef generate cookbook cookbooks/apache You can verify the cookbook is created with; knife cookbook list -z -z specifies we are looking for local cookbooks. Now we can create our recipe with the following command; chef generate recipe cookbooks/apache/ server This creates a recipe called server under cookbooks/apache folder. Now we are going to edit this server recipe to include our automation code. Edit the newly created server.rb file under cookbooks/apache/recipes and append the following code that will install and run apache on our target server.
package 'httpd'

file '/var/www/html/index.html' do
   content '<h2>Hello world</h2>'

service 'httpd' do
   action [:start, :enable]
When this recipe runs, it will install and run the httpd service on our target server.

Uploading the Cookbook to Chef Server

Enter the following command to upload our cookbook to Chef server knife upload cookbooks/apache Now you can verify if the cookbook is uploaded to Chef server by entering; knife cookbook list

Registering Target Node to Chef Server

It is time to register our target server which will run the apache. I have an Oracle Linux 6.4 running in my Compute Cloud Service. To register the node, I need the public IP address of my node. This registering process is called bootstrapping in Chef.

On my client machine, I type the following to register my server. Note that this command also runs the server recipe which we have uploaded earlier. Don't forget to keep your current working directory at chef-repo.

knife bootstrap -x opc -i priv.pem --sudo -N my_web_server -r "recipe[apache::server]"

  You can also verify this in target server logs

Lastly, we can do a curl localhost to see that apache is running properly and serving our files.


Imagine you have lots of servers to accomplish this same operation of installing and running apache server. This would be a very time-consuming task without an automation tool. Thus, tools like Chef enable us to manage our infrastructure with little effort. Chef has many other great features which are not covered here so please visit https://learn.chef.io/ for more. The VM I used for installing the server is hosted on Oracle IaaS. If you want to play with your own instances you can go to https://cloud.oracle.com/iaas request your free trial.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.