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

时间:2017-03-15 16:54:08

标签: chef chef-recipe

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

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

...但我需要找到"网络"对于ip - 即10.0.1.0/24

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我会去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']