Getting Started with Chef Solo. Part 5

Hello my dear friends. Today we will continue to talk about Chef Solo. All the example codes mentioned here, you can find at: github.com/le0pard/chef-solo-example/tree/5.0.In the previous article, we’ve learned about Chef cookbooks. In this article we will learn what is Ohai and how to write an Ohai plugin.

Ohai

Ohai detects data about your operating system. It can be used standalone, but its primary purpose is to provide node data to Chef.When invoked, it collects detailed, extensible information about the machine it’s running on, including Chef configuration, hostname, FQDN, networking, memory, CPU, platform, and kernel data.When Chef configures the node object during each Chef run, these attributes are used by the chef-client to ensure that certain properties remain unchanged. These properties are also referred to as automatic attributes. In our case (in Chef Solo), these attributes are available in the node object. For example:

Ohai plugin

In our cookbook “tomatoes” we already have the node.js recipe. Let’s create the ohai plugin, which will provide us with information about node.js that is already installed in the system node.js. We will use this information to check if we need to install node.js on the server.First of all, you need create new recipe “ohai_plugin.rb” in “tomatoes” with the following content:This recipe will generate the ohai plugin from template “system\_node\_js.rb”. Next, we should create the following template in the folder “tomatoes/templates/default/plugins”:In the first two lines we set automatic attributes by method “provides”, which will provide us with this plugin.Most of the information we want to lookup would be nested in some way, and ohai tends to do this by storing the data in a Mash. This can be done by creating a new mash and setting the attribute to it. We did this with “system\_node\_js”.In the end of code, plugin sets the version of node.js, if node.js is installed on the server. That’s it!Next, let’s try this plugin by adding the following content to the “default.rb” recipe:Now test it by running the command “vagrant provision”. When you start it for the first time, you will not see anything, as the plugin will be delivered later when the chef-client launched. But the second time, you should see a picture similar to the one below in the log:In this case we can change our node.js recipe a little:Let’s try to change the node.js version in the role “web.json”:And restart “vagrant provision”:And after some time, the server will have a new node.js:And on the next launch of chef solo you should see the new version of node.js:

Summary

In the current article, we’ve learned Ohai and how to write an Ohai plugin.All the example codes mentioned here, you can find at: github.com/le0pard/chef-solo-example/tree/5.0.That’s all folks! Thank you for reading till the end.This post is crossposting from my blog.