From 7431ebf51ce6e909aa3a5e8f2b609c9eb5cee7ad Mon Sep 17 00:00:00 2001 From: Ben Vincent Date: Sun, 24 Dec 2023 14:12:54 +1100 Subject: [PATCH] feat: add region fact - add fact that maps primary ip subnet to a region code - defaults to 'lost' if there is no subnet to region mapping --- site/profiles/lib/facter/region.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 site/profiles/lib/facter/region.rb diff --git a/site/profiles/lib/facter/region.rb b/site/profiles/lib/facter/region.rb new file mode 100644 index 0000000..966fa10 --- /dev/null +++ b/site/profiles/lib/facter/region.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +# set region based on the subnet + +Facter.add('region') do + setcode do + # use facts['networking']['ip']to find the promary IP address + ip = Facter.value(:networking)['ip'] + + # subnet to region mapping + subnet_to_region = { + '198.18.17.0/24' => 'au-drw-1' + } + + require 'ipaddr' + + # Find the region for the IP + region = 'lost' # default to 'lost' if no region matches + subnet_to_region.each do |subnet, region_name| + if IPAddr.new(subnet).include?(IPAddr.new(ip)) + region = region_name + break + end + end + + region + end +end