【问题标题】:Custom ohai plugin runs but doesn't update attributes自定义 ohai 插件运行但不更新属性
【发布时间】:2016-10-27 20:16:23
【问题描述】:

我编写了一个非常简单的自定义 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

【问题讨论】:

  • 您使用的是哪个版本的 [ohai 食谱]()?较新版本提供ohai_plugin 资源。你最近更新了吗?较新的版本(IIRC v3)删除了logic to automatically add the plugin_path to the Chef config
  • ohai 4.2.2是超市自动拉出来的;那是最新版本。当我查看我正在测试的节点时,文件被复制到那里,但它在/var/chef/cache/cookbooks/inventory/files/default 中。我不确定这是否是插件的正确路径。这就像那个文件的内容永远不会被执行。我根本没有看到正在运行的“查找”命令。让我觉得它在错误的位置,因为 ohai 没有看到它。我在 /etc/chef 中没有看到 ohai 插件的任何其他路径。
  • 从您的日志输出中可以看出,该文件甚至被放到了/stuff_ohai_will_do.rb,因为node['ohai']['plugin_path']nil。它曾经转到/etc/chef/ohai_plugins,但现在必须将 IIRC 显式添加到配置中。我们还没有升级,因此我只能建议使用新的ohai_plugin 资源或 pin ohai < 3
  • 我恢复到 ohai 2.1.0 并没有解决问题。你知道我在哪里可以获得关于新 ohai_plugin 资源的文档吗?或者比这个更新的示例插件(github.com/rackerlabs/ohai-plugins/tree/master/plugins == Chef 文档中的链接)? Google 不喜欢下划线,也没有找到“ohai_plugin”的相关文档。
  • 这里是包含文档的仓库:github.com/chef-cookbooks/ohai

标签: chef-infra chef-recipe ohai-gem


【解决方案1】:

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

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

我阅读valid Ruby class name 并不是对语法有效性的评论,而是因为 Ruby 类是一个现有的类(这是我第一次尝试 Ruby 代码!)。我从目录中的现有插件中获取了“包”名称。一旦我将其更改为“战争”的唯一名称,它就起作用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    相关资源
    最近更新 更多