[英]Custom ohai plugin runs but doesn't update attributes
我写了一个非常简单的自定义ohai插件。 它应该只是检查系统上是否有带有.war扩展名的文件。 厨师客户端运行似乎执行得很好( re-run ohai and merge results into node attributes
)。 但是,从我的刀具工作站上,如果我编辑所有节点属性,那么对于我设置的warFiles mash不会有任何结果。 在Chef管理GUI中也看不到多余的属性。 该菜谱名为“库存”。 为什么不存储我的属性???
我的“插件”实际上只包含一个红宝石文件:
[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
和
我的meta.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
发现了问题...在Ohai文档中,此行描述了:Name
字段:
需要。 (
:Name
)用于标识插件; 当两个插件具有相同的(:Name
)时,这些插件将连接在一起并像单个插件一样运行。 此值必须是有效的Ruby类名称,以大写字母开头且仅包含字母数字字符
我读取valid Ruby class name
不是为了评论语法的有效性,而是打算将Ruby类作为现有的类使用(这是我第一次尝试Ruby代码!)。 我从目录中现有插件中获取了“软件包”名称。 一旦将其更改为“战争”的唯一名称,它就起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.