Wil, 

Thanks for the reply. I will look up the module functions you reference. 
Though, part  of this was writing the custom function as a learning 
exercise. 

So if I have a file with the following in it and run it through ruby it 
works. 

require 'resolv'
Resolv::DNS.open do |dns|
  ress = dns.getresources "google.com", Resolv::DNS::Resource::IN::A
  if ress.any?
    puts dns.getaddress("google.com")
  end
end

To reference Mike's question. What I found was that 'if ress.any?' works 
and that is what allows me to protect against malformed host names or a 
failure to find an A record. 

So I've already chopped out the puppet stuff, and this code works. So I'm 
thinking that it must be something with the require statement, where it 
isn't finding the library I need. But I'm not sure what I can do from here. 

On Friday, October 3, 2014 2:00:27 AM UTC-4, Wil Cooley wrote:
>
>
> On Oct 2, 2014 10:23 AM, "Mark Rosedale" <[email protected] <javascript:>> 
> wrote:
> >
> > I have the following custom function that I'm trying to write. 
> >
>
> Sorry this isn't an answer to your question, but I recently published a 
> module of functions wrapping the standard system get*by* functions (well, 
> wrapping Ruby's interfaces to the standard system functions):  
> https://forge.puppetlabs.com/wcooley/name_service_lookups
>
> This includes `gethostbyname` and `gethostbyaddr`; the main benefit over 
> querying DNS directly is that it understands nsswitch, so /etc/hosts works 
> and any other host database you might want to use.
>
> But assuming you want to implement this anyway an exercise in learning 
> Ruby & Puppet extensions, you might start by removing all the Puppet stuff 
> and just get the lookups working as a simple standalone Ruby script.
>
> Wil
>
> require 'resolv'
> >
> > module Puppet::Parser::Functions
> >   newfunction(:getIpAddr, :type => :rvalue) do |arguments|
> >     Resolv::DNS.open do |dns|
> >       ress = dns.getresources(arguments[0], Resolv::DNS::Resource::IN::A)
> >       if ress.any?
> >         dns.getaddress(arguments[0])
> >         #break
> >       else
> >         raise(Puppet::ParseError, "No valid A Record found for host 
> #{aguments[0]}")
> >       end
> >     end
> >     #return value
> >   end
> > end
> >
> > I'm getting the following error when I run this function. 
> >
> > Error: Could not retrieve catalog from remote server: Error 400 on 
> SERVER: undefined local variable or method `dns' for 
> #<Puppet::Parser::Scope:0x7f5054f07110> at 
> >
> >
> > -- 
> > 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 [email protected] <javascript:>.
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/puppet-users/02de838d-6cb2-495a-883d-98fb15782df9%40googlegroups.com
> .
> > 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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/c949b38c-cf60-4bfe-897f-94c14a84c4f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to