配方属性:
default['human']['jack']['arms'] = 2
default['human']['jack']['legs'] = 2
default['human']['jack']['heads'] = 1
在节点/角色中:
override['human']['jack']['legs'] = 1
同样在食谱中:
node.override['human']['jack']['legs'] = 1
那么,如果我的食谱不知道杰克将存在于节点/角色,我想要一个动态集合来处理大量条目。在默认情况下定义或合并的好策略是什么?
我不想提出解决方案所以我会使用组成的通配符作为我的例子,其中Jack和Jill不同但我不必定义默认值(如两臂每次我定义一个新实例时都会有一个头。
配方属性:
default['human'][*] = {
"arms" => 2,
"legs" => 2,
"heads" => 1
}
在节点/角色中:
default['human']['jack'] = {
"legs" => "1"
}
default['human']['jill'] = {
"superpower" => "flying"
}
答案 0 :(得分:0)
是的,厨师属性使用深度合并,您可以在配方中执行此操作,请参阅http://docs.opscode.com/essentials_node_object_deep_merge.html
在食谱中执行此操作:
node['nginx']['sites'].each_key do |site|
node.default['nginx']['sites'][site] = node['nginx']['site_defaults']
end
log JSON.pretty_generate(node['nginx']['sites'])
Cookbook属性:
default['nginx']['site_defaults']['listen'] = [80]
default['nginx']['site_defaults']['location'] = '/'
default['nginx']['site_defaults']['index'] = ['index.html','index.htm']
default['nginx']['sites']['api']['index'] = 'api.cgi'
在节点/角色中:
"nginx" : {
"sites" : {
"blog" : {
"location" : "/blog/",
"listen" : [443]
},
"wiki" : {
"index" : ["index.php"]
}
}
},
可生产
* log[{
"api": {
"listen": [
80
],
"location": "/",
"index": [
"index.html",
"index.htm"
]
},
"blog": {
"listen": [
443
],
"location": "/blog/",
"index": [
"index.html",
"index.htm"
]
},
"wiki": {
"listen": [
80
],
"location": "/",
"index": [
"index.php"
]
}
}] action write
请注意,如果执行此操作,则配方默认值优先于属性default,因此覆盖了api索引。以下是可行的。
force_default['nginx']['sites']['api']['index'] = 'api.cgi'
同样要小心Arrays,有时它们会被合并,其他时候被替换(上图)。