簡體   English   中英

自定義ohai插件運行但不更新屬性

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM