自定义ohai插件运行但不更新属性

时间:2016-10-27 20:16:24

标签: chef chef-recipe ohai-gem

我写了一个非常简单的自定义ohai插件。它应该检查系统上是否有任何带有.war扩展名的文件。厨师客户端运行似乎执行得很好(re-run ohai and merge results into node attributes)。但是,如果我编辑所有节点属性,从我的刀工作站,我设置的warFiles mash没有任何内容。不要在Chef Manage GUI中看到额外的属性。该食谱被命名为"库存"。为什么我的属性不存储???

我的"插件"这里只包含一个ruby文件:

 [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

我的metadata.rb除了预配置的东西外还有这两行:

depends 'ohai'
depends 'chef-client'

然后是这里的食谱:

 [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

这是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

=====更新=====

我发现了插件目录(/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/ohai-8.20.0/lib/ohai/plugins)。直接在chef节点上工作,我已将我的插件ruby文件直接放在此目录中。我知道它是正确的目录,因为我将uptime.rb文件从dir中移出,重新运行了Chef-client,然后运行了ohai | grep -i uptime ...并且正常运行时间消失了。将ruby文件移回此目录,我能够恢复ohai的输出正常运行时间。我知道下面的插件位于正确的目录中。

ohai只执行此目录中的所有内容,还是有一些ohai列表我需要添加文件名?否则,我的代码或语法必须是错误的

[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

1 个答案:

答案 0 :(得分:1)

发现问题...在Ohai文档中,此行描述了:Name字段:

  

必需。 (:Name)用于标识插件;当两个插件具有相同的(:Name)时,这些插件将连接在一起并像单个插件一样运行。该值必须是有效的Ruby类名,以大写字母开头,仅包含字母数字字符

我读valid Ruby class name并不是对语法有效性的评论,而是因为打算Ruby类是现有的(这是我第一次尝试Ruby代码!)。我采取了"包"来自目录中现有插件的名称。一旦我将其更改为" War"的唯一名称,它就有效了。