Carl_Friedrich_Gauss
Carl_Friedrich_Gauss

Reputation: 303

Custom ohai plugin runs but doesn't update attributes

I've written a very simple custom ohai plugin. It should just check if there are any files on the system with .war extensions. The chef client run seems to execute just fine (re-run ohai and merge results into node attributes). However, from my knife workstation if I edit all the node attributes, there is nothing coming up for the warFiles mash I set. Don't see the extra attribute in the Chef Manage GUI either. The cookbook is named "inventory". Why isn't my attribute being stored???

My "plugin" really just consists of a ruby file here:

 [root@host default]# pwd
 /root/chef/cookbooks/inventory/files/default
 [root@host default]# cat stuff_ohai_will_do.rb
Ohai.plugin(:Packages) do
  provides 'warFiles'

  collect_data(:default) do
    warFiles Mash.new

      so = shell_out('find / -type f -name "*.war"')
      warFiles[:file] = so.stdout.split("\n")
  end
end

and

my metadata.rb has these 2 lines in addition to the pre-configured stuff in there:

depends 'ohai'
depends 'chef-client'

and then a recipe here:

 [root@host recipes]# pwd
 /root/chef/cookbooks/inventory/recipes
 [root@host recipes]# cat ohai_plugin.rb
 include_recipe 'ohai::default'
 include_recipe 'chef-client::config'

 ohai "reload" do
   action :reload
 end

 cookbook_file "#{node['ohai']['plugin_path']}/stuff_ohai_will_do.rb" do
   notifies :reload, "ohai[reload]"
 end

This is the output of the chef-client run:

[root@host chef]# chef-client -o recipe[inventory::ohai_plugin]
Starting Chef Client, version 12.15.19
resolving cookbooks for run list: ["inventory::ohai_plugin"]
Synchronizing Cookbooks:
  - inventory (0.1.0)
  - ohai (4.2.2)
  - cron (3.0.0)
  - logrotate (2.1.0)
  - windows (2.0.2)
  - compat_resource (12.16.1)
  - chef-client (7.0.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 12 resources
Recipe: chef-client::config
  * logrotate_app[chef-client] action enable
    * directory[/etc/logrotate.d] action create (up to date)
    * template[/etc/logrotate.d/chef-client] action create (up to date)
     (up to date)
  * directory[/var/run/chef] action create (up to date)
  * directory[/var/cache/chef] action create (up to date)
  * directory[/var/lib/chef] action create (up to date)
  * directory[/var/log/chef] action create (up to date)
  * directory[/etc/chef] action create (up to date)
  * file[/var/log/chef/client.log] action create (up to date)
  * template[/etc/chef/client.rb] action create (up to date)
  * directory[/etc/chef/client.d] action create (up to date)
  * ruby_block[reload_client_config] action nothing (skipped due to action     :nothing)
Recipe: inventory::ohai_plugin
  * ohai[reload] action reload
    - re-run ohai and merge results into node attributes
  * cookbook_file[/stuff_ohai_will_do.rb] action create (up to date)

Running handlers:
Running handlers complete
Chef Client finished, 1/14 resources updated in 04 seconds

===== UPDATE =====

I've since found the plugins directory (/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/ohai-8.20.0/lib/ohai/plugins). Working directly on the chef node, I have put my plugin ruby file directly in this directory. I know it's the right dir as I moved the uptime.rb file out of the dir, re-ran chef-client, and ran ohai | grep -i uptime ... and uptime was gone. Moving the ruby file back to this directory, I was able to restore uptime to ohai's output. I know the below plugin is in the right directory.

Does ohai just execute everything in this directory or is there some ohai list I need to add the file name to? Otherwise, my code or syntax must be wrong

[root@host plugins]# cat stuff_ohai_will_do.rb
  Ohai.plugin(:Packages) do
    provides 'warFiles'

 collect_data do
   warFiles Mash.new
   so = shell_out('find / -name "*.war"')
   warFiles[:file] = so.stdout
  end
end

Upvotes: 0

Views: 1344

Answers (1)

Carl_Friedrich_Gauss
Carl_Friedrich_Gauss

Reputation: 303

Found the issue ... In the Ohai documentation this line describes the :Name field:

Required. (:Name) is used to identify the plugin; when two plugins have the same (:Name), those plugins are joined together and run as if they were a single plugin. This value must be a valid Ruby class name, starting with a capital letter and containing only alphanumeric characters

I read valid Ruby class name not as a comment on the validity of the syntax, but as intending the Ruby class was an existing one (this is my first time attempting Ruby code!). I had taken the "Packages" names from an existing plugin in the directory. Once I changed it to the unique name of "War", it worked.

Upvotes: 1

Related Questions