簡體   English   中英

Chef - 讀出節點屬性並將其存儲在同一廚師客戶端運行中的另一個節點屬性中失敗

[英]Chef - Read out node attribute and store it in another node attribute in same chef client run fails

我嘗試在 chef-client 運行時讀出當前的配方名稱,並將其存儲在一個配方中的變量或節點屬性中。 直到現在我才找到一種將它存儲到節點屬性中的方法,但它總是失敗。 這是我的代碼:

  ruby_block "Fetch Recipe Name From Run List" do
    block do
      Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
      s = shell_out("echo \"#{node['expanded_run_list']}\" | awk -F '::' '{print substr($3, 1, length($3)-1)}'" )
      node.default['sftp-selfmade']['extracted_recipe'] = s.stdout
    end
  end

  extracted_recipe = node['sftp-selfmade']['extracted_recipe']

  # To debug the output of the node attribute.
  execute 'TEST' do
    command "echo \"TEST #{extracted_recipe}\""
  end

Output:

  * execute[TEST] action run
    [execute] TEST 
    - execute echo "TEST "

Output 應該是:

- execute echo "TEST <Name-Of-Extracted-Recipe>"

我嘗試了很多事情,還將s.stdout output 存儲在一個變量中,但這會在編譯階段引發NoMethodError 還嘗試使用更強的值,例如node.override - 這有效,但只能通過首先設置node.normal並將其設置為node.override但這不是一個令人滿意的解決方案,每次在說明書代碼中再次執行此操作以部署到新主機。 還嘗試了重新加載OHAI的解決方案。 但這也沒有用。 在一個全新的主機上,它在第二次廚師客戶端運行后也不起作用,如果屬性已經設置然后在第一次運行后。

有人可以幫助我嗎?

找到了以下解決方案:

expandedrecipe = node['expanded_run_list'].select{ |e| e.include? 'sftp-selfmade' }.first.split('::').last

這就是訣竅。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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