如何获取 Chef 节点的网络属性?

How can I get the network attribute for my Chef node?

我找到了获取特定 IP 地址的方法:

node[:network][:interfaces][:eth1][:addresses].detect{|k,v| v[:family] == "inet" }.first

...但我需要找到 ip 的 "network" - 即 10.0.1.0/24

有什么想法吗?

我会选择 node['network']['interfaces']['eth1']['routes'].select {|k,v| v['src'] == node['ipaddress'] }['destination'] 因为在路线下你会有这样的东西:

  "routes": [
        {
          "destination": "default",
          "family": "inet",
          "via": "172.30.4.250"
        },
        {
          "destination": "172.30.4.0/22",
          "family": "inet",
          "scope": "link",
          "proto": "kernel",
          "src": "172.30.5.235"
        }
      ]

所以 src 是你的 ip 的目的地就是你要找的。

您可以用您的方法替换问题中的 node['ipaddress'],如下所示:

selected_ip = node[:network][:interfaces][:eth1][:addresses].detect{|k,v| v[:family] == "inet" }.first
node['network']['interfaces']['eth1']['routes'].select {|k,v| v['src'] == selected_ip }['destination']