Ohai plugin_path not being loaded on Windows 10

478 Views Asked by At

Following Ron Cowie's guide in his book Customising Chef (O'Reilly) to creating simple Ohai plugin, it appears as though the Ohai plugins_path is not being loaded on my Windows 10 laptop.

Steps are as follows:

  1. Create Example1.rb plugin file. I have done so in C:\dev\opscode\ohai\plugins\example1.rb
Ohai.plugin(:Example1) do 1
  provides "awesome_level" 2

  collect_data do 3
    awesome_level 100 4
  end
end
  1. Run irb and type following:
PS C:\WINDOWS\system32> irb
irb(main):001:0> require 'ohai'
=> true
irb(main):002:0> Ohai::Config[:plugin_path] = 'C:/Dev/opscode/ohai/plugins'
=> "C:/Dev/opscode/ohai/plugins"
irb(main):003:0> o = Ohai::System.new
=> #<Ohai::System:0x0000000003c339a0 @cli=nil, @plugin_path="", @config={},...

It looks to me as though the @plugin_path is empty and so after running o.all_plugins and then o.attributes_print("awesome_level")

> Results in error: irb(main):019:0> puts
> o.attributes_print("awesome_level") Traceback (most recent call last):
>         3: from C:/opscode/chef-workstation/embedded/bin/irb.cmd:19:in `<main>'
>         2: from (irb):19
>         1: from C:/opscode/chef-workstation/embedded/lib/ruby/gems/2.5.0/gems/ohai-14.8.10/lib/ohai/system.rb:178:in
> `attributes_print' ArgumentError (I cannot find an attribute named
> awesome_level!)

Is it syntax for setting the plugins_path on Windows. I tried double-quotes. Backslashes.

2

There are 2 best solutions below

0
On

My Chef install is not standard and the default C:\chef\ohai\plugins folder did not exist. If i used cmd line to add a directory to plugin path it worked:

ohai -d 'C:\dev\opscode\ohai\plugins'

This displayed all attributes from all plugins with right at the end my custom plugin

}, "awesome_level": 100

0
On

feels to me like you suffer from versioning issue -- to resolve any path and version issues, use bundler and lock your rubygems versions in a Gemfile.

here is an example:

$ bundle init
Writing new Gemfile to /private/var/folders/_2/rg7rz1h56sb2ln5f75gjr7558b1wz3/T/tmp.PPIvZRXi/Gemfile
$ echo "gem 'ohai'" >> Gemfile
$ bundle install --path vendor/gems --jobs 4
...

you mentioned that you are using chef. if you use ohai within chef-client run, then the plugin must be installed on the node that is managed by chef and before ohai is being load up in your recipe.