根据配方代码发送到 Chef 资源的可变数量的属性
Variable number of properties sent to Chef resource based off of recipe code
我想根据属性中可用的值将可变数量的属性传递给资源。
这是我想做的事情的想法。围绕属性的 if 逻辑是我想要的,但不确定是否可以做类似的事情...
rpms = [
{
"name": "name",
"version": "version",
"release": "release",
"arch": "arch"
},
{
"name": "name"
}
]
rpms.each do | package_info |
custom_package 'install' + package_info['name'] do
name package_info['name']
if defined?(package_info['version']) # Only pass that property if it is available
version package_info['version']
end
if defined?(package_info['release']) # Only pass that property if it is available
version package_info['release']
end
if defined?(package_info['arch']) # Only pass that property if it is available
version package_info['arch']
end
end
end
Chef 中有类似的东西吗?
您可以使用 Ruby's send
method 执行此操作,它调用第一个参数标识的方法,将指定的任何其他参数传递给它。
rpms = [
{
"name": "name",
"version": "version",
"release": "release",
"arch": "arch"
},
{
"name": "name"
}
]
rpms.each do |package_info|
custom_package "install ${package_info['name']}" do
package_info.each do |key, value|
send(key, value)
end
end
end
我想根据属性中可用的值将可变数量的属性传递给资源。
这是我想做的事情的想法。围绕属性的 if 逻辑是我想要的,但不确定是否可以做类似的事情...
rpms = [
{
"name": "name",
"version": "version",
"release": "release",
"arch": "arch"
},
{
"name": "name"
}
]
rpms.each do | package_info |
custom_package 'install' + package_info['name'] do
name package_info['name']
if defined?(package_info['version']) # Only pass that property if it is available
version package_info['version']
end
if defined?(package_info['release']) # Only pass that property if it is available
version package_info['release']
end
if defined?(package_info['arch']) # Only pass that property if it is available
version package_info['arch']
end
end
end
Chef 中有类似的东西吗?
您可以使用 Ruby's send
method 执行此操作,它调用第一个参数标识的方法,将指定的任何其他参数传递给它。
rpms = [
{
"name": "name",
"version": "version",
"release": "release",
"arch": "arch"
},
{
"name": "name"
}
]
rpms.each do |package_info|
custom_package "install ${package_info['name']}" do
package_info.each do |key, value|
send(key, value)
end
end
end