Skip to content

Instantly share code, notes, and snippets.

@kjetilho
Created September 16, 2020 06:28
Show Gist options
  • Save kjetilho/02839bac669f2ea65df7663bc59cf7e3 to your computer and use it in GitHub Desktop.
Save kjetilho/02839bac669f2ea65df7663bc59cf7e3 to your computer and use it in GitHub Desktop.
simple custom facts to use resolved addresses in Puppet code
Facter.add(:fqdndns_ipaddress4) do
setcode do
begin
# Use the system configured nameservers to run a query
addrs = []
begin
require 'resolv'
Resolv::DNS.open do |dns|
ress = dns.getresources(Facter.value('fqdn'), Resolv::DNS::Resource::IN::A)
addrs = ress.map { |r| r.address }
end
rescue
nil
end
if addrs.length == 1
addrs[0].to_s.downcase
else
# multiple addresses is considered inconclusive, so we return nothing
nil
end
end
end
end
Facter.add(:fqdndns_ipaddress6) do
setcode do
begin
# Use the system configured nameservers to run a query
addrs = []
begin
require 'resolv'
Resolv::DNS.open do |dns|
ress = dns.getresources(Facter.value('fqdn'), Resolv::DNS::Resource::IN::AAAA)
addrs = ress.map { |r| r.address }
end
rescue
nil
end
if addrs.length == 1
addrs[0].to_s.downcase
else
# multiple addresses is considered inconclusive, so we return nothing
nil
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment