0

我正在尝试为具有以下属性的厨师制作食谱:

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>

循环哈希数组的正确方法是什么?

4

1 回答 1

3

好的,我想通了。正确的语法是通过访问哈希键hash[:key],即

$HTTP["host"] =~ "<%= host[:host] %>" {
    proxy.balance = "round-robin" proxy.server = (
         "" => (
             "play" => (
               "host" => "<%= host[:server] %>",
               "port" => <%= host[:port] %>
             )
         )
       )
  }
  <%   end -%>
  <% else -%>
  <% end -%>
于 2012-12-04T14:20:11.527 回答