Skip to content

Instantly share code, notes, and snippets.

@vjanelle
Last active December 14, 2015 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vjanelle/5019346 to your computer and use it in GitHub Desktop.
Save vjanelle/5019346 to your computer and use it in GitHub Desktop.
Generates facts to identify the interface a mac address is associated with
# Fact:
# macaddresses to interfaces
#
# Purpose:
# Make a fact to detail the interfaces for a normalized mac address
#
# Caveats:
# Only tested on linux, deals with 802.3ad bonded links, but I have not
# tested other kinds of interfaces
#
require 'facter/util/macaddress'
# Delete lo interface, as it doesn't have a mac
Facter::Util::IP.get_interfaces.delete_if {|interface| interface == 'lo' || interface =~ /sit\d+/ }.each do |interface|
# if we don't have a hardware address, skip
hwaddr = Facter::Util::IP.get_interface_value(interface, 'macaddress')
if hwaddr == nil
next
end
# If the device has a master, skip it
if Facter::Util::IP.get_bonding_master(interface) == nil
# mac_AA_BB_CC... => eth0
label = "mac_" + hwaddr.gsub(/:/,'_').downcase
Facter.add(label) do
setcode do
interface
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment