出于各种原因,我有一个我想要使用的厨师配方将多个组添加到系统中。我已经定义了一个带有键/值结构的数据包,它提供了组名作为键,guid作为值,例如:
{
"id": "default_groups",
"group": {
"sales": "200",
"marketing": "800",
"csr": "1000",
"devel": "9000",
"scientists": "500"
}
}
我想在每个循环中使用组资源,但我似乎没有正确引用数据包中的数据:
user_groups = data_bag_item('groups', 'default_groups')
%w{"#{user_groups['group']}"}.each do |usr|
group "#{usr}.key" do
action :create
gid "#{usr}.value"
end
end
有人能解释我的语法错误吗?
厨师运行的错误输出如下:
ERROR: group["#{user_groups['group']}".key] (cookbook_groups::recipe_groups line 10) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '3'
---- Begin output of ["groupadd", "-g", "\"\#{user_groups['group']}\".value", "\"\#{user_groups['group']}\".key"] ----
STDOUT:
STDERR: groupadd: invalid group ID '"#{user_groups['group']}".value'
---- End output of ["groupadd", "-g", "\"\#{user_groups['group']}\".value", "\"\#{user_groups['group']}\".key"] ----
Ran ["groupadd", "-g", "\"\#{user_groups['group']}\".value", "\"\#{user_groups['group']}\".key"] returned 3
答案 0 :(得分:1)
你不能像这样组合字符串插值,字数组和数组/散列访问。
这可能有效:
user_groups = data_bag_item('groups', 'default_groups')
user_groups['group'].each do |key, value|
group key do
action :create
gid value
end
end
答案 1 :(得分:0)
显然,%w
没有按你的想法行事:
▶ %w{"#{user_groups['group']}"}
#⇒ ["\"\#{user_groups['group']}\""]
你可能只需要:
user_groups['group'].each
或者喜欢。