[英]Chef custom resource properties
我使用以下方法創建了一本食譜:
chef generate cookbook demo
chef generate attribute default
chef generate resource package
食譜\\default.rb :
demo_package 'Demo'
資源\\package.rb :
property :resource_prop1, String
property :resource_prop2, String
default_action :install
load_current_value do
puts
puts "the current value before change for 'test_prop1': #{resource_prop1}"
puts "the current value before change for 'test_prop2': #{resource_prop2}"
resource_prop1 = node['demo_resource']['test_prop1']
resource_prop2 = node['demo_resource']['test_prop2']
puts "the current value after change for 'test_prop1': #{resource_prop1}"
puts "the current value after change for 'test_prop2': #{resource_prop2}"
end
action :install do
puts "the current value after load properties for 'test_prop1': #{new_resource.resource_prop1}"
puts "the current value after load properties for 'test_prop2': #{new_resource.resource_prop2}"
end
屬性\\default.rb :
default['demo_resource'] = {}
default['demo_resource']['test_prop1'] = 'test value 1'
default['demo_resource']['test_prop2'] = 'test value 2'
我希望輸出看起來像這樣:
the current value before change for 'test_prop1':
the current value before change for 'test_prop2':
the current value after change for 'test_prop1': test value 1
the current value after change for 'test_prop2': test value 2
the current value after load properties for 'test_prop1': test value 1
the current value after load properties for 'test_prop2': test value 2
但我實際上得到了:
the current value before change for 'test_prop1':
the current value before change for 'test_prop2':
the current value after change for 'test_prop1': test value 1
the current value after change for 'test_prop2': test value 2
the current value after load properties for 'test_prop1':
the current value after load properties for 'test_prop2':
我試圖找出從操作塊中引用資源屬性的正確方法。 我究竟做錯了什么?
更新
我嘗試改變這一行:
puts "the current value after load properties for 'test_prop1': #{new_resource.resource_prop1}"
進入這個:
puts "the current value after load properties for 'test_prop1': #{resource_prop1}"
但我收到了這個錯誤:
NameError undefined local variable or method `resource_prop1' for #<#Class:0x0000000007b51178>:0x00000000084b3680> Did you mean? resources
更新 2
我能夠通過如下修改 local_current_value 塊來解決這個問題:
load_current_value do |package|
puts
puts "the current value before change for 'test_prop1': #{package.resource_prop1}"
puts "the current value before change for 'test_prop2': #{package.resource_prop2}"
package.resource_prop1 = node['demo_resource']['test_prop1']
package.resource_prop2 = node['demo_resource']['test_prop2']
puts "the current value after change for 'test_prop1': #{package.resource_prop1}"
puts "the current value after change for 'test_prop2': #{package.resource_prop2}"
end
自定義資源中有 2 個對象: current_resource
和new_resource
。 當您在配方中使用自定義資源時,您將創建new_resource
:
demo_package 'Demo' do
resource_prop1 'foo'
resource_prop2 'bar'
end
在load_current_value
您應該加載資源的當前狀態。 例如,如果您要創建一個具有某些路徑和所有者的文件,那么在load_current_resource
您應該獲取該文件及其當前所有者,以便稍后檢查資源是否已更改。 在您當前的方法中,您實際上分配了current_resource.resource_prop1
和current_resource.prop2
。
由於new_resource
和current_resource
是不同的對象,您在load_current_resource
中設置的值不會從current_resource
傳遞到new_resource
。
如果您需要在操作塊中加載值,請使用current_resource
:
action :install do
puts "the current value after load properties for 'test_prop1': #{current_resource.resource_prop1}"
puts "the current value after load properties for 'test_prop2': #{current_resource.resource_prop2}"
end
更新 2 的答案
當您將參數傳遞給load_corrent_resource
,該參數實際上引用了 new_resource。 這有時很有幫助,如果
有一個約定可以調用此參數desired
,如下所示:
load_current_value do |desired|
# will assign current_resource.resource_prop1 = calculate_value(new_resource.resource_prop1)
resource_prop1 calculate_value(desired.resource_prop1)
[...]
# will assign new_resource.resource_prop2
desired.resource_prop2 some_calculation
end
查看自定義資源,在您的情況下,它應該類似於
new_resource.resource_prop1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.