3

我需要找到一个节点的 IP,如果有的话。问题是“cluster_nodes.first”没问题,但它没有任何方法,“.ip”不存在,cluster_nodes.first[:ip] 也不存在,我在 Chef Wiki 上找不到任何信息。

cluster_nodes = search(:node, "name:cluster1*")

if cluster_nodes.size > 0
  node[:primary_node] = cluster_nodes.first.ip
else
  node[:primary_node] = ''
end

错误:

[Sat, 01 Sep 2012 10:29:22 +0000] ERROR: Running exception handlers
[Sat, 01 Sep 2012 10:29:22 +0000] FATAL: Saving node information to /var/chef/cache/failed-run-data.json
[Sat, 01 Sep 2012 10:29:22 +0000] ERROR: Exception handlers complete
[Sat, 01 Sep 2012 10:29:22 +0000] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
[Sat, 01 Sep 2012 10:29:22 +0000] FATAL: ArgumentError: Attribute ip is not defined!
4

1 回答 1

0

haproxy recipe 它有一个很好的例子,这是从该食谱中摘录的片段:

cluster_nodes = search(:node, "name:cluster1*")
member = cluster_nodes.first if cluster_nodes

if cluster_nodes && cluster_nodes.size > 0
  primary_node = begin
    if member.attribute?('cloud')
      if node.attribute?('cloud') && (member['cloud']['provider'] == node['cloud']['provider'])
         member['cloud']['local_ipv4']
      else
        member['cloud']['public_ipv4']
      end
    else
      member['ipaddress']
    end
  end
else
  primary_node = ''
end

这个对我有用。第一个节点获取空值,第二个、第三个等获取 IP 地址。

于 2012-09-01T15:20:25.333 回答