Created
September 16, 2020 06:28
-
-
Save kjetilho/02839bac669f2ea65df7663bc59cf7e3 to your computer and use it in GitHub Desktop.
simple custom facts to use resolved addresses in Puppet code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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