Getting the last inactive vboxnet interface with Ruby
This could possibly be the most esoteric blog post I’ve ever written, but here it goes. So, for reasons even we have difficulty comprehending (hint: related to PXE booting in a Vagrant/Virtualbox environment) we had to find out which inactive vboxnet (hostonly) interface had the highest index. We wanted the code to (hopefully) run on all platforms (Linux, MacOS, Windows) so we wanted to write this code in Ruby. So, this was the brutal but working end-result:
Creating custom facts programmatically
Writing static facts is fairly easy even with a low level of Ruby skills. Below is an example of a fact that returns true or false depending on whether the node has a /boot partition:
Facter.add(:has_bootfs) do
setcode do
if Facter.value(:mountpoints)['/boot'].nil?
false
else
true
end
end
end
As you can see above the required information was already inside the “mountpoints” fact of Facter.
Using the fact is easy:
Getting the interface that matches an IP in Puppet
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: