【发布时间】:2020-03-27 03:16:43
【问题描述】:
我使用以下方法创建了一本食谱:
chef generate cookbook demo
chef generate attribute default
chef generate resource package
recipes\default.rb:
demo_package 'Demo'
resources\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
【问题讨论】:
-
欢迎来到 SO!我建议阅读“Writing The Perfect Question”和“How To s The Smart Way”。我们不在乎您的专业知识;对我们来说最重要的是写一个经过充分研究和提出的问题。
标签: ruby chef-infra