What version are you running? And what is the output if you try to call the 
function as you have it now?

On 06 Oct 2014, at 19:07, José Luis Ledesma <[email protected]> wrote:

> Credits go to Nan Liu:
> 
> If you are developing facts, it's much easier to just drop into IRB and get 
> everything working there rather than doing round trip debugging between 
> puppet and facter:
> 
> irb(main):001:0> require 'facter'
> 
> => true
> 
> irb(main):002:0> Facter.value("hostname")
> 
> => "demo-1"
> 
> irb(main):003:0> Facter.value("hostname").split('-')
> 
> => ["demo", "1"]
> 
> irb(main):006:0>  Facter.add('network_geo') do
> 
> irb(main):007:1*   setcode do
> 
> irb(main):008:2*     hostname_array =  Facter.value(:hostname).split('-')
> 
> irb(main):009:2>
> 
> irb(main):010:2*     # debug info
> 
> irb(main):011:2*     puts "My network is #{hostname_array}"
> 
> irb(main):012:2>     hostname_array.first
> 
> irb(main):013:2>   end
> 
> irb(main):014:1> end
> 
> irb(main):015:0> Facter.value(:network_geo)
> 
> My network is ["demo", "1"]
> 
> => "demo"
> 
> If you run your existing fact in irb, you'll see the output is nil instead:
> 
> Facter.value(:network_geo)
> 
> My network is ["demo", "1"]
> 
> => nil
> 
> Hth,
> 
> El 06/10/2014 18:07, "Mark Rosedale" <[email protected]> escribió:
> Still haven't been able to hunt down the cause of this issue. Is there a way 
> to test the code with the puppet wrapping on the cli? 
> 
> On Friday, October 3, 2014 3:41:57 PM UTC-4, Mark Rosedale wrote:
> 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]> 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].
> > 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/e2857e58-e52f-44ef-a30b-368fb6e4b9ae%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/CAF_B3dfyfjunB5UYZF2q577S-bL6QQPqCg2_TVL5p4ZekZf%3D9w%40mail.gmail.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/46F9BC6D-BA3F-4B7A-82B9-5711BEE7148B%40superseb.nl.
For more options, visit https://groups.google.com/d/optout.

Reply via email to