Skip to content

Instantly share code, notes, and snippets.

@natemccurdy
Last active May 26, 2018 21:53
Show Gist options
  • Save natemccurdy/b434e0b24401f5bb1decf394fbe1d503 to your computer and use it in GitHub Desktop.
Save natemccurdy/b434e0b24401f5bb1decf394fbe1d503 to your computer and use it in GitHub Desktop.
Datacenter Fact
# A fact that uses CIDR network ranges to determine a node's datacenter.
#
# gist url: https://gist.github.com/natemccurdy/b434e0b24401f5bb1decf394fbe1d503
#
require 'ipaddr'
Facter.add(:datacenter) do
setcode do
# This hash defines the map of network segments to datacenters.
# NOTE: To extend this fact, modify this hash with your segments and datacenters.
datacenter_map = {
'shire' => IPAddr.new('10.10.10.0/22'),
'rohan' => IPAddr.new('11.11.11.0/22'),
'lorien' => IPAddr.new('12.12.12.0/22'),
'mordor' => [
IPAddr.new('13.13.13.0/24'),
IPAddr.new('15.15.15.0/24')
]
}
# Set the initial datacenter value to nil.
# You could optionally change this to be 'none' or 'not_found' or something
# that indicates we couldn't figure it out.
datacenter = nil
# Iterate through all subnets and find the one that
# includes the default interface's IP address.
datacenter_map.each do |datacenter_name, subnets|
Array(subnets).each do |address|
datacenter = datacenter_name if address.include?(Facter.value(:ipaddress))
end
end
# Return the found datacenter.
datacenter
end
end
@vchepkov
Copy link

How would one check if any configured IP belongs to the subnet, not just the one in ipaddress fact?
I have issue with bridging interfaces, which are not 'main' from puppet's point of view

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