gpt4 book ai didi

chef-infra - 如何在 Chef Recipe 中动态查找主机的 IP

转载 作者:行者123 更新时间:2023-12-01 05:17:08 24 4
gpt4 key购买 nike

我需要找到一个节点的 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!

最佳答案

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

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 地址。

关于chef-infra - 如何在 Chef Recipe 中动态查找主机的 IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12227017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com