> > 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! On Sun, Dec 23, 2018 at 7:25 AM Helmut Schneider <jumpe...@gmx.de> wrote: > 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! > > -- > 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+unsubscr...@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. > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/CACkW_L5Mrusp%3DhZ%2BFUKswkaKv38fPRAvA2vqNtviC8AMEV0j%2Bw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.