我正在尝试为厨师制作食谱,其中包括以下属性:
default['web-server']['hosts'] = [ { host: "some-ip", server: "some-server", port: "9000" } ]
我的想法是迭代它来创建配置文件(在本例中为lighttpd),如下所示:
<% if node['web-server']['hosts'].length > 0 -%>
<% node['web-server']['hosts'].each do |host| -%>
$HTTP["host"] =~ "<%= host.host %>" {
proxy.balance = "round-robin" proxy.server = (
"" => (
"play" => (
"host" => "<%= host.server %>",
"port" => <%= host.port %>
)
)
)
}
<% end -%>
<% else -%>
<% end -%>
但是,在测试节点上运行chef-client
时,我会收到:
FATAL: Chef::Mixin::Template::TemplateError: undefined method `host' for #<Mash:0x00000002b9a878>
循环哈希数组的正确方法是什么?
答案 0 :(得分:3)
好的,我想出来了。正确的语法是通过hash[:key]
访问哈希密钥,即
$HTTP["host"] =~ "<%= host[:host] %>" {
proxy.balance = "round-robin" proxy.server = (
"" => (
"play" => (
"host" => "<%= host[:server] %>",
"port" => <%= host[:port] %>
)
)
)
}
<% end -%>
<% else -%>
<% end -%>