Ask Your Question
0

Custom datacenter fact

asked 2018-04-06 15:18:06 -0500

UBPClaw gravatar image

I was looking at this article trying to create a custom fact for datacenter. Issue I have is that the code looks at the network as a /24 space. Id like to just look for the /16 network instead of defining multiple /24 networks. How do I consolidate the two lines for DC1 in to one that just looks at 10.0?

Facter.add(:datacenter) do
setcode do
network=Facter.value(:network)
case network
when '10.0.2.0'
  'DC1'
when '10.0.3.0'
  'DC1'
else
  'unknown'
end
edit retag flag offensive close merge delete

Comments

Would this work? Facter.add(:datacenter) do setcode do network=Facter.value(:network) if network =~ /^10\.0\./ 'DC1' else 'DC2' end end end

UBPClaw gravatar imageUBPClaw ( 2018-04-06 15:28:01 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-04-07 14:39:06 -0500

JohnsonEarls gravatar image

updated 2018-04-07 14:46:09 -0500

One solution, if all your datacenters are /16 networks:

require 'ipaddr'
Facter.add(:datacenter) do
  setcode do
    network = IPAddr.new(Facter.value(:network))
    case network.mask(16).to_s
    when '10.0.0.0'
      'DC1'
    when '10.1.0.0'
      'DC2'
    else
      'unknown'
    end
  end
end

Another possibility:

require 'ipaddr'
Facter.add(:datacenter) do
  network = IPAddr.new(Facter.value(:network))
  setcode do
    [
      [ IPAddr.new('10.0.0.0/6'), 'DC1' ],
      [ IPAddr.new('192.168.0.0/24'), 'DC2' ],
      [ IPAddr.new('172.16.32.0/20'), 'DC3' ],
      [ IPAddr.new('0.0.0.0/0'), 'unknown' ],
    ].select { |dcinfo|
      dcinfo[0] === network
    }.map { |dcinfo| dcinfo[1] }[0]
  end
end
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-04-06 15:18:06 -0500

Seen: 168 times

Last updated: Apr 07