I was recently in a situation where I needed to figure out which interface name matched a particular, static IPv4 address. On a single or a handful of nodes this could have been hardcoded in Hiera, for example, but there were dozens of nodes. The way I solved this was by creating a custom fact that uses the IPv4 address and loops through the interfaces list to find the matching interface name:
# frozen_string_literal: true # Get the name of the interface that is bound to 192.168.122.10 Facter.add(:kvm_iface) do confine kernel: 'Linux' desired_ip = '192.168.122.10' interfaces_fact = Facter.value(:networking)['interfaces'] interfaces_list = interfaces_fact.keys interfaces_list.each do |interface| interface_ip = interfaces_fact[interface]['bindings'][0]['address'] next unless interface_ip == desired_ipdesired_interface = interface
setcode do
desired_interface
end
end end
In the end the code did not get used because we could rely on the interface name being static, but hopefully this code is helpful for somebody else.