2017-03-15 93 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']