我编写了一个非常简单的自定义 ohai 插件。它应该只检查系统上是否有任何带有 .war 扩展名的文件。厨师客户端运行似乎执行得很好(re-run ohai and merge results into node attributes
)。但是,如果我从刀工作站编辑所有节点属性,则我设置的 warFiles 混搭没有任何内容。在 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 运行的输出:
[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 文件移出目录,重新运行 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