下面的代码按预期工作并产生以下内容:
==> default: foo_definition: a
==> default: foo_definition: foo
==> default: foo_definition: fiz
==> default: bar_definition: b
==> default: bar_definition: bar
==> default: bar_definition: biz
食谱/foobar/recipes/default.rb:
node.default[:configs][:a][:def_name] = 'foo_name'
node.default[:configs][:a][:message] = 'fiz'
node.default[:configs][:b][:def_name] = 'bar_name'
node.default[:configs][:b][:message] = 'biz'
#def_map = {
# 'foo_name' => foo_definition,
# 'bar_name' => bar_definition
#}
for config_name in node[:configs].keys do
def_name = node[:configs][config_name][:def_name]
if def_name == 'foo_name'
foo_definition config_name do
config_name config_name
end
elsif def_name == 'bar_name'
bar_definition config_name do
config_name config_name
end
else
raise Exception('Unknown def #{def_name}')
end
end
食谱/foobar/definitions/default.rb:
define :foo_definition, :config_name => nil do
config_name = params[:config_name]
print("foo_definition: #{config_name}")
print("foo_definition: #{node[:configs][config_name][:def_name]}")
print("foo_definition: #{node[:configs][config_name][:message]}")
end
define :bar_definition, :config_name => nil do
config_name = params[:config_name]
print("bar_definition: #{config_name}")
print("bar_definition: #{node[:configs][config_name][:def_name]}")
print("bar_definition: #{node[:configs][config_name][:message]}")
end
我想使用哈希来查找配置中命名的特定定义。但是,如果我取消注释 def_map,Chef 会抱怨:
==> default: NoMethodError
==> default: -------------
==> default: undefined method `[]' for nil:NilClass
==> default:
==> default:
==> default: Cookbook Trace:
==> default: ---------------
==> default: /tmp/vagrant-chef/2e4e554824398d9416eee0b4bd47e8b9/cookbooks/foobar/definitions/default.rb:4:in 'block in from_file'
有谁可以解释为什么?
最终,我希望能够在食谱中做这样的事情:
node.default[:configs][:a][:def_name] = 'foo_name'
node.default[:configs][:a][:message] = 'fiz'
node.default[:configs][:b][:def_name] = 'bar_name'
node.default[:configs][:b][:message] = 'biz'
def_map = {
'foo_name' => foo_definition,
'bar_name' => bar_definition
}
for config_name in node[:configs].keys do
def_name = node[:configs][config_name][:def_name]
if def_map.key?(def_name)
named_def = def_map[def_name]
named_def config_name do
config_name config_name
end
else
raise Exception('Unknown def #{def_name}')
end
end