Skip to content

Instantly share code, notes, and snippets.

@agaffney
Created October 1, 2015 19:17
Show Gist options
  • Save agaffney/e0eb583eb77f925e4a3a to your computer and use it in GitHub Desktop.
Save agaffney/e0eb583eb77f925e4a3a to your computer and use it in GitHub Desktop.
Custom ipaddress fact that ignores Docker (and other) interfaces
# The builtin ipaddress fact is very naive and will pick the IP address of the
# first (alphabetically) interface. This fact changes that behavior and only
# grabs the IP off an interface matching /^(eth|bond|vlan)\d/. This excludes
# interfaces types such as TUN, bridges, and Docker.
Facter.add("ipaddress") do
has_weight 100
setcode do
# This allows the fact to work from the commandline
begin
Facter.interfaces
rescue
Facter.loadfacts()
end
ipaddress = nil
facts = []
# Iterate over interfaces and add the fact with their IP address to the list to check
interfaces = Facter.value('interfaces').split(",")
interfaces.each do |interface|
# Skip if it's not a "real" network interface
if ! interface.match(/^(eth|bond|vlan)\d/)
next
end
facts.push('ipaddress_' + interface.downcase)
end
facts.each do |fact|
ipaddr = Facter.value(fact)
if ipaddr.nil? || ipaddr == '127.0.0.1'
next
end
ipaddress = ipaddr
# Stop looking once we find a good address
break
end
ipaddress
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment