0

我想向 Ohai 添加额外的资源“已安装的 openssh 版本”,以便在我的 openssh 维护配方中使用它。

在 RHEL 5.11 Chef 12.4.1 Ohai 8.5.0 测试工作站上,我创建并测试了 Ohai 插件

$ 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

irb 中 ohai 插件的本地测试工作正常。现在我正在尝试检查 Chef 食谱中的资源可见性

$ 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']} **")

通过运行本地厨师客户端

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

为了使额外的插件可见行被添加到配置文件中

$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/'

(我知道这太明确了)

尽管使用打印 node['kernel'] 运行正常,但 openssh 版本未运行调试日志,显示:

[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"'

问题:

  1. 如何正确地为本地和远程执行提供额外的插件?如何检查它是否已煮好并准备好?

  2. 如何正确通知厨师客户端执行 ohai 附加插件以用于本地单一配方运行和远程运行?

欢迎任何解释和建议。

亚历克斯

4

2 回答 2

0

一些问题:首先查看https://github.com/coderanger/ohai-example以了解如何将 ohai 插件打包到说明书中以进行分发。其次,来自自定义插件的节点属性仍然需要通过node对象访问:node['openssh']['version']. 第三,请记住 Chef ( https://coderanger.net/two-pass/ ) 中的执行顺序是如何工作的,并且在插件加载并运行之前,自定义属性将不可用。

于 2016-01-29T00:40:29.550 回答
0

google 之前结帐主流!

这个项目描述了如何在 2017 年部署你的插件!

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

于 2017-10-12T14:45:17.147 回答