chamilad
chamilad

Reputation: 1669

How to distribute a custom Ohai plugin

I'm writing an Ohai plugin to add some custom attributes to be used by the chef-client. Where do I include the plugin.rb file?

Is it inside the cookbook?

Or do I have to copy it to some standard place?

Chef docs and other tutorials don't offer much on this information (or I must have missed a huge obvious fact). I know in Puppet you can include the custom facts in lib/facter/ folder of the module. Is it the same in chef?

Upvotes: 4

Views: 3922

Answers (2)

vskubriev
vskubriev

Reputation: 858

For all the time the workflow of using custom ohai plugins changed many times. That's chef. This is normal for chef!

First ensure that you are using latest ohai cookbook (currently 5.2.0)

If you use chef-server you can does this as follows:

knife cookbook site download ohai
cd ~/you/cookbooks/folder
tar xzf ohai-5.2.0.tar.gz
rm ohai-5.2.0.tar.gz
knife cookbook upload ohai

On the official site there is no cases how to use custom plugins.

Suppose you already have a plugin (ohai version 7) to deploy.

Include it in any application/wrapper cookbook you want following recipe:

ohai_plugin 'myplugin_status_ohai_plugin'

And puts in you application/wrapper cookbook a file with you plugin code in files/default/myplugin_status_ohai_plugin.rb. And that's all. By default you plugin will be installed to a directory named 'plugins' under the directory 'ohai' in the Chef config dir. Install and plugin collecting data function will be triggered at compile time.

If you do not specify /etc/chef/ohai/plugins as additional path in chef-client configuration is will be warn. But /etc/chef/ohai/plugins will be used by default.

To get rid of warning, use chef-client cookbook, recipe chef-client::config, node variable node['ohai']['plugin_path'] as additional path to load Ohai plugins from.

Upvotes: 0

StephenKing
StephenKing

Reputation: 37620

This is not as hard as it sounds but I agree that the documentation on Ohai 7 lacks the bit of information that was present in the Ohai 6 docs.

However, the ohai resource and the ohai cookbook are what you are looking for. An example of using that can be found in a recipe of mine.

Basically, you first have to put the plugin's .rb file into Ohai's plugin path (node[:ohai][:plugin_path]) and then reload Ohai in order to make the attributes available in the current chef run:

ohai "reload" do
  action :reload
end

template "#{node[:ohai][:plugin_path]}/myplugin.rb" do
  notifies :reload, "ohai[reload]"
end

Upvotes: 6

Related Questions