0

I want to add additional resource 'version of installed openssh' to Ohai to use it in my openssh maintaining recipe.

On RHEL 5.11 Chef 12.4.1 Ohai 8.5.0 test workstation I have created and tested Ohai plugin

$ cat cookbooks/test/files/default/plugins/openssh.rb

Ohai.plugin(:Openssh) do

  provides "openssh"

Ohai::Log.debug('plugin start')

  def create_objects

    openssh Mash.new

  end


  collect_data do

    create_objects

    openssh[:version] = 'ssh -V 2>&1 |head -1| cut -d, -f1| cut -d_ -f2 '

  end

end

Local test of ohai plugin in irb is working fine. Now I'm trying to check resource visibility in Chef recipe

$ cat test/recipes/default.rb

file "#{ENV['HOME']}/x.txt" do

  content 'HELLO WORLD'

end

output="#{Chef::JSONCompat.to_json_pretty(node.to_hash)}"

file '/tmp/node.json' do

  content output

end

Chef::Log.info("============ test cookbook ** #{openssh['version']} **")

\#Chef::Log.info("============ test cookbook ** #{node['kernel']} **")

by running local chef-client

$ chef-client -z -m test/recipes/default.rb

To make additional plugin visible line is added to config files

$grep Ohai ~/.chef/*.rb
~/.chef/client.rb:Ohai::Config[:plugin_path] << '~/chef/cookbooks/test/files/default/plugins/'

~/.chef/knife.rb:Ohai::Config[:plugin_path] << '~/chef/cookbooks/test/files/default/plugins/'

(I understand that this is too explicit )

Although running with printing node['kernel'] is working fine , openssh version is not running with debug log that shows:

[2016-01-27T11:48:21-08:00] DEBUG: Cookbooks detail: []

[2016-01-27T11:48:21-08:00] DEBUG: Cookbooks to compile: []

[2016-01-27T11:48:21-08:00] DEBUG: **Loading Recipe File XXX/cookbooks/test/recipes/default.rb**

[2016-01-27T11:48:21-08:00] DEBUG: Resources for generic file resource enabled on node include: [Chef::Resource::File]

[2016-01-27T11:48:21-08:00] DEBUG: Resource for file is Chef::Resource::File

[2016-01-27T11:48:21-08:00] DEBUG: Resources for generic file resource enabled on node include: [Chef::Resource::File]

[2016-01-27T11:48:21-08:00] DEBUG: Resource for file is Chef::Resource::File

[2016-01-27T11:48:21-08:00] DEBUG: Resources for generic openssh resource enabled on node include: []

[2016-01-27T11:48:21-08:00] DEBUG: **Dynamic resource resolver FAILED to resolve a resource for openssh**

[2016-01-27T11:48:21-08:00] DEBUG: Re-raising exception: NameError - No resource, method, or local variable named `openssh' for `Chef::Recipe "XXX/cookbooks/test/recipes/default.rb"'

Questions:

  1. How properly chef out additional plugin to recipe for local and remote execution? How to check that it is cheffed out and ready?

  2. How properly notify chef-client to execute ohai additional plugin for local single recipe run and for remote run as well?

Any explanations and suggestions are welcomed.

Alex

coderanger
  • 52,400
  • 4
  • 52
  • 75
Alex Timo
  • 1
  • 2

2 Answers2

0

A few issues: first check out https://github.com/coderanger/ohai-example to see how to package an ohai plugin in a cookbook for distribution. Second, node attributes from custom plugins still need to be accessed via the node object: node['openssh']['version']. Third, remember how execution ordering works in Chef (https://coderanger.net/two-pass/) and that the custom attributes won't be available until after the plugin is loaded and run.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • thanks @ coderanger, my greping through /tmp/node.json showed no traces of openssh – Alex Timo Jan 29 '16 at 19:16
  • You generated the `content` value at compile time, which might be before the plugin has been loaded. Move that to lazy evaluation. – coderanger Jan 29 '16 at 19:55
  • thanks @ coderanger, somehow it is helped, mostly node['openssh']['version'] note. Somehow my output of node key-values was not showing even in dynamicaly build output="#{Chef::JSONCompat.to_json_pretty(node.to_hash)}" to /tmp/node.json file. – Alex Timo Jan 30 '16 at 00:51
  • Some development. After local successful testing I was trying to make remote execution. It is failing with error *ERROR: undefined method `[]' for nil:NilClass for line 14>> Chef::Log.info("============ test cookbook \*\* #{node['openssh']['version']} \*\*")* which is running OK in local test. Also there is no related plugin (test/files/default/plugins/openssh.rb) found on remote node in /var/chef/cach/cookbooks/test – Alex Timo Feb 03 '16 at 21:09
0

Checkout mainstream before google!

This project describes how to deploy you plugin in 2017 year!

https://github.com/chef-cookbooks/ohai

vskubriev
  • 826
  • 1
  • 11
  • 21