2017-01-09 74 views
0

我有一個自定義資源。當該資源收斂時,我想將由所述資源安裝的軟件包存儲到node.run_state [:installed_pa​​ckages]中。但是當我在另一個配方(相同的客戶端運行)中稍後閱讀該值時,它似乎爲零。廚師中的node.run_state是否應該在整個廚師 - 客戶端運行中生存?

我唯一的想法是,這是在資源申報時評估?如果是這樣,我將如何實現我的目標?

這裏是我的自定義資源的把項目進入run_state一個片段:

# Push the desired package into the run_state 
    if (node.run_state[:installed_packages] == nil) then 
     node.run_state[:installed_packages] = Set.new 
    end 
    node.run_state[:installed_packages].add("/tmp/#{rpm_name}.rpm") 

這裏就是我在使用這些值的模板資源:

template '/tmp/my_script.sh' do 
    source 'my_script.sh.erb' 
    owner 'root' 
    group 'root' 
    mode '0755' 
    variables({ 
     :packages => node.run_state[:installed_packages] || Array.new, 
    }) 

    action :nothing 
end 

回答

1

很難從講就是你展示的內容,但你認爲「稍後」可能是「之前」。使用lazy {}助手來延遲評估值,直到稍後。有關何時發生特定位代碼的更多詳細信息,請參閱https://coderanger.net/two-pass/

+0

這正是我所需要的。對於遇到此問題的其他人,這裏是懶惰的廚師文檔: https://docs.chef.io/resource_common.html#lazy-evaluation –