Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
different ways to get interface IPs
$ grep 'node\[:network\]\[:interfaces\].' \#chef.log
10:15 < mkent_> node[:network][:interfaces][:eth1][:addresses]
22:24 <+Damm> msf, just pulling in the node[:network][:interfaces] attributes
20:44 < randybias> node[:network][:interfaces][:eth0][:addresses]
09:13 < sinBot> so fujin if I wanted to use that in an erb template, it'd be <%= @node[:network][:interfaces]["en1"]["addresses"].select{address}.flatten.to_str %> ?
12:27 < cwj> if i have an ipv4 ip address set on eth0, will it always be in @node[:network][:interfaces][:eth0][1] ?
02:19 < pluesch0r> however, i don't seem to be able to access @node[:network][:interfaces]... from inside the attributes file.
19:52 <@jtimberman> or node[:network][:interfaces][:eth0][:addresses][0]
20:09 < seryl> well, it's searchable. I'm trying the @node[:network][:interfaces][:eth0][:addresses][0] route, but getting blanks right now, playing around with it in chef solo
20:29 < kallistec> pp node[:network][:interfaces].current_attribute
11:15 < cwj> well if you already have the attributes from the node, you would access it through something like @node[:network][:interfaces][:eth0][:addresses]
13:11 < galstrom> also.. if i am looping through node[:network][:interfaces] i can access each interfaces sub hash with "[:eth0]" at the end
08:08 < shutterman> In a default installation, one client has an attribute node[:network][:interfaces][:eth1][:addresses][:192.168.0.1][:family][:inet]. I want to extract that IP address and use in a recipe, instead of creating a script to get this information which is already available through Chef. I thought that somehow I could have a variables clause in the recipe as :myeth1address => node[:network][:interfaces][:eth1][:addresses]... but I can't find a way to extract t
08:10 < renchap> node[:network][:interfaces][:eth1][:addresses].each { |ip| puts ip } ?
08:17 < renchap> node[:network][:interfaces][:eth1][:addresses].find { |name, options| options[:family] == 'inet' }.each { |ip| ... }
08:20 < renchap> node[:network][:interfaces][:em1][:addresses].find_all { |name, options| options[:family] == 'inet' }.each { |ip,options| puts ip }
08:46 < shutterman> renchap: Got it working: :myeth1ipaddress => node[:network][:interfaces][:eth1][:addresses].keys.first -- Thanks a lot for your help pointing me the correct way of doing it.
19:20 < larrywright> node[:network][:interfaces][:eth1]
19:21 < kallistec> larrywright: I get chef > pp node[:network][:interfaces][:eth0][:addresses].keys
19:23 < larrywright> pp node[:network][:interfaces][:eth1][:addresses].keys.first
19:26 < kallistec> pp node[:network][:interfaces][:eth0][:addresses].to_hash.select {|addr, info| info["family"] == "inet"}.flatten.first
13:19 < ryanschwartz> hi all. can anyone tell me why @node[:network][:interfaces] lists IP addresses in numerical order, rather than alias order?
02:03 < asdf_> package "ipcalc" do action :install end template "/etc/network/interfaces" do source "interfaces.erb" variables({ :iface => node[:network][:default_interface], :ip => node[:ipaddress], :broadcast => node[:network][:interfaces][node[:network][:default_interface]][:addresses][node[:ipaddress]][:broadcast], :gateway => `ip route | grep default | cut -d' ' -f3`.strip, :netmask => node[:network][:interfaces][node[:n
16:05 <@kallistec> btm: node[:network][:interfaces][:en1][:addresses].select {|addr, addr_info| addr_info["family"] == "inet" }
15:09 <@kallistec> rcreasey: node[:network][:interfaces][:en1][:addresses].to_hash.find {|addr, addr_info| addr_info['family'] == 'inet'}.first
19:12 < benblack> node[:network][:interfaces][:eth0][:addresses]
19:21 < benblack> but, again, do you see it with knife node show? is it not available with node[:network][:interfaces][:eth0]?
19:31 < larrywright> the best I can do is node[:network][:interfaces][:eth1]
19:32 < larrywright> node[:network][:interfaces][:eth1][:addresses]
19:32 < larrywright> chef:recipe > node[:network][:interfaces][:eth1][:addresses].keys
19:33 < benblack> node[:network][:interfaces][:en1][:addresses].map {|i| i.first if i.last["family"].eql?("inet") }.compact.first
16:07 < rcreasey> specifically: node[:network][:interfaces][:lo:0][:addresses].first
16:13 <@kallistec> rcreasey: node[:network][:interfaces][:lo0][:addresses].find {|addr, addr_info| addr_info[:family] == "inet"}.first
@chakatz
Copy link

chakatz commented Jul 11, 2013

This works:
chef > iface_name="eth1"
=> "eth1"
chef > node["network"]["interfaces"][iface_name]["addresses"].keys[1]
=> "10.28.16.20"

And this works:
chef > node["network"]["interfaces"].keys
=> ["lo", "eth0", "eth1"]

So why doesn't this work?:
chef > node["network"]["interfaces"].keys.each { |iface_name| node["network"]["interfaces"][iface_name]["addresses"].keys[1] }
=> ["lo", "eth0", "eth1"]

I was expecting the last example to return the IP addresses for each interface. Is there a way to get that to work?

@shuangwu
Copy link

shuangwu commented May 20, 2014

@chakatz, you need to replace each with map.

@fujin
Copy link

fujin commented Apr 1, 2016

I still end up checking this every now and then.

@ts82
Copy link

ts82 commented May 26, 2016

Hi, great examples. I was wondering what would be the correct syntax to get the names of all ethernet interfaces into a hash to later iterate over it to e.g. write the NIC config files for each eth* interface.
And - where could I dig into a documentation with the filtering stuff of the ohai attribute tree like the select statement in:
node[:network][:interfaces][:en1][:addresses].select {|addr, addr_info| addr_info["family"] == "inet" }

Thx & best regards

@RulerOf
Copy link

RulerOf commented Jan 25, 2019

This returns the IPv4 address of the default NIC

node['network']['interfaces'][node['network']['default_interface']]['addresses'].select{|k,v| v['family'] == "inet"}.keys.first

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment