2017-03-15 95 views
1

我已经找到方法来获得一个特定的IP地址:如何获取我的厨师节点的网络属性?

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

...但我需要找到“网络”为IP - 即10.0.1.0/24

任何想法?

+1

我会去'节点[“网络”] [“接口”] [“eth1的”] ['路线:

你会被你的方法的问题是这样的更换node['ipaddress'] '] .select {| k,v | v ['src'] == node ['ipaddress']} ['destination']'(您可以用问题中的方法替换节点['ipaddress']] – Tensibai

+0

可能想将其移至答案 – coderanger

+0

Forgive我,但你失去了我的“你可能会替换节点['ipaddress']你的方法有问题,但我确实得到了这个工作,但是:<%=”#{(node [:network] [:interfaces] [: eth1] [:addresses] .detect {| k,v | v [:family] ==“inet”} .first).split(“。”)[0 ..- 2] .join(“。”)} .0/24“%> – TyMac

回答

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是你在找什么。

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']