I’d start by looking at using a standard puppet module. If you place the subroutine (a.k.a “method” in Ruby parlance) into a ruby module (different than a puppet module), you could then reuse the method in a custom fact, external fact, function and type & provider. Taking this approach also has the benefit of letting Puppet deal with the distribution of the code for you using the plugignsync mechanism.
The reusable method could go into lib/puppet_x/my_name/my_stuff.rb as module PuppetX::MyName::MyThing; def my_method; end; end; end You could then use include PuppetX::MyName::MyThing in your various implementations.
The convention with PuppetX is to place your re-usable utility code under a module named after you or your organization to avoid namespace conflicts with other published modules. “MyStuff” could be a class or a module depending on what you’re accomplishing.
I recommend starting with defining “MyThing” as a class, then instantiating it where you need it. Ruby also allows methods to be composed via the include or extend statements, but I recommend avoiding that functionality unless a standard class doesn’t fit your needs for some reason. Include and extend can be more difficult to test, less clear to a reader, and less intuitive generally speaking than a standard class is.
Hope this helps,
—
Jeff McCune
Principal Consultant & Founder