resolve hostname via custom fact

20 views
Skip to first unread message

Helmut Schneider

unread,
Dec 23, 2018, 10:25:08 AM12/23/18
to puppet...@googlegroups.com
Hi,

I want to resolve a hostname via a custom fact:

require "resolv"

Facter.add("puppet_master_ip") do
setcode do
Resolv::DNS.open(:nameserver => ['8.8.8.8']) do |dns|
ip = dns.getaddresses("www.puppet.org")
end
end
end

How do I get the output? I just want to get the first IP.

helmut@h2786452:~$ facter puppet_master_ip
[

]
helmut@h2786452:~$ facter puppet_master_ip --debug --trace
2018-12-23 16:22:57.816747 INFO puppetlabs.facter - executed with
command line: puppet_master_ip --debug --trace.
2018-12-23 16:22:57.820403 INFO leatherman.ruby:138 - ruby loaded from
"/opt/puppetlabs/puppet/lib/libruby.so.2.1.0".
2018-12-23 16:22:57.880073 INFO leatherman.ruby:187 - using ruby
version 2.1.9
2018-12-23 16:22:57.880231 INFO puppetlabs.facter - requested queries:
puppet_master_ip.
2018-12-23 16:22:57.880317 DEBUG puppetlabs.facter - fact
"facterversion" has resolved to "3.6.10".
2018-12-23 16:22:57.880364 DEBUG puppetlabs.facter - fact
"aio_agent_version" has resolved to "1.10.14".
2018-12-23 16:22:57.881923 DEBUG leatherman.file_util:65 - Error
reading file: No such file or directory
2018-12-23 16:22:57.882710 DEBUG puppetlabs.facter - loading all custom
facts.
2018-12-23 16:22:57.882751 DEBUG puppetlabs.facter - loading custom
fact directories from config file
2018-12-23 16:22:57.883863 DEBUG puppetlabs.facter - searching for
custom facts in /opt/puppetlabs/puppet/lib/ruby/site_ruby/2.1.0/facter.
2018-12-23 16:22:57.884066 INFO puppetlabs.facter - loading custom
facts from
/opt/puppetlabs/puppet/lib/ruby/site_ruby/2.1.0/facter/external_ip4.rb.
2018-12-23 16:22:57.962679 INFO puppetlabs.facter - loading custom
facts from
/opt/puppetlabs/puppet/lib/ruby/site_ruby/2.1.0/facter/puppet_master_ip.
rb.
2018-12-23 16:22:58.295668 DEBUG puppetlabs.facter - fact
"external_ip4" has resolved to "81.169.210.177".
2018-12-23 16:22:58.555197 DEBUG puppetlabs.facter - fact
"puppet_master_ip" has resolved to [

].
2018-12-23 16:22:58.555380 DEBUG puppetlabs.facter - skipping external
facts for "/home/helmut/.puppetlabs/opt/facter/facts.d": No such file
or directory
2018-12-23 16:22:58.555445 DEBUG puppetlabs.facter - skipping external
facts for "/home/helmut/.facter/facts.d": No such file or directory
2018-12-23 16:22:58.555478 DEBUG puppetlabs.facter - no external facts
were found.
[

]
helmut@h2786452:~$

Thank you!

Ben Ford

unread,
Dec 23, 2018, 1:45:15 PM12/23/18
to puppet...@googlegroups.com
I want to resolve a hostname via a custom fact:
require "resolv"
Facter.add("puppet_master_ip") do
  setcode do
    Resolv::DNS.open(:nameserver => ['8.8.8.8']) do |dns|
      ip = dns.getaddresses("www.puppet.org")
    end
  end
end
How do I get the output? I just want to get the first IP.

Ruby has a habit that it picked up from its Perl ancestor of implicitly returning the last expression from a block or function. That's a neat shortcut, but that's also why you see so much Ruby code that just seems to stop and doesn't show returning of data. Because it's the last expression evaluated, your fact is simply returning an array of Resolv objects, which Facter doesn't know what to do with.

To make your code work, you just need to do two things:

require "resolv"
Facter.add("puppet_master_ip") do
  setcode do
    ip = nil         # Declare your variable outside the block to keep its scope available
    Resolv::DNS.open(:nameserver => ['8.8.8.8']) do |dns|
      ip = dns.getaddresses("www.puppet.org")
    end
    ip.first.to_s    # implicitly return the string value of the first item
  end
end


You should also put your fact in a module and let Puppet pluginsync it automatically. You'll need to run facter with the -p flag. https://puppet.com/docs/puppet/latest/plugins_in_modules.html#adding-plug-ins-to-a-module

Cheers!

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/xn0lj1k498fj8t8000%40news.gmane.org.
For more options, visit https://groups.google.com/d/optout.

Helmut Schneider

unread,
Dec 24, 2018, 7:49:52 AM12/24/18
to puppet...@googlegroups.com
Ben Ford wrote:

> > I want to resolve a hostname via a custom fact:
> > require "resolv"
> > Facter.add("puppet_master_ip") do
> > setcode do
> > Resolv::DNS.open(:nameserver => ['8.8.8.8']) do |dns|
> > ip = dns.getaddresses("www.puppet.org")
> > end
> > end
> > end
> > How do I get the output? I just want to get the first IP.
>
> Ruby has a habit that it picked up from its Perl ancestor of
> implicitly returning the last expression from a block or function.
> That's a neat shortcut, but that's also why you see so much Ruby code
> that just seems to stop and doesn't show returning of data. Because
> it's the last expression evaluated, your fact is simply returning an
> array of Resolv objects, which Facter doesn't know what to do with.
>
> To make your code work, you just need to do two things:
>
> require "resolv"
> Facter.add("puppet_master_ip") do
> setcode do
>
> * ip = nil # Declare your variable outside the block to
> keep its scope available* Resolv::DNS.open(:nameserver =>
> ['8.8.8.8']) do |dns| ip = dns.getaddresses("www.puppet.org")
> end
>
> * ip.first.to_s # implicitly return the string value of the
> first item* end
> end
>
> You should also put your fact in a module and let Puppet pluginsync it
> automatically. You'll need to run facter with the -p flag.
>
https://puppet.com/docs/puppet/latest/plugins_in_modules.html#adding-plug-ins-to-a-module
>
> Cheers!

Thanks a lot and happy holidays!

helmut@h2786452:~$ facter puppet_master_ip
52.24.136.51
helmut@h2786452:~$

Reply all
Reply to author
Forward
0 new messages