Hey all, I am trying to get a modified hostname (converting "blah-1234567" to "1234567") to use in my manifests and templates. I have it working for templates by using Ruby (*<%= hostname.split(''-'').pop.downcase %>*) but I haven''t been able to use it in manifests because they don''t process ERB. The way I see it I could do one of two things; create a custom function or create a custom fact. I think I would rather build a server-side function but I haven''t had much luck getting them to work. So I started looking at creating a custom fact but the Wiki/documentation is quite convoluted and I haven''t been able to gain any ground there either. Which method would be better, simpler, and less client-side intrusive? Does anyone have an example of a custom function that I could copy? Where on the server to place it? I tried creating my own function in /usr/local/lib/site_ruby/puppet/parser/functions/keyname.rb but it''s giving me a "...private method ''split'' called for ["blah-1234567"]:Array at ..." error. Or should I just use a facter fact? I tried to make sense of the Wiki but its just not sinking in. It doesn''t help that Puppet is in its growth stage right now so some of the information out there is no longer valid. My copy of "Pulling String with Puppet" is on its way, however I''m afraid that by the time it gets here it will be out of date. :( Anyway, I''m running Puppet 0.24.4 on Debian Etch on both client & server and here is my first try at a puppet function: # keyname.rb module Puppet::Parser::Functions newfunction(:keyname, :type => :rvalue) do |args| args.split(''-'').pop.downcase end end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com To unsubscribe from this group, send email to puppet-users-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en -~----------~----~----~----~------~----~------~--~---
James Bellenger
2008-May-02 18:44 UTC
[Puppet Users] Re: Functions vs. Module Plugins vs. Facts
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 You might also consider using the generate function to call an external script. We''re still developing our puppet framework, but something we''ve come to found handy is to use generate to invoke a shell/ruby/whatever program and to set it in a variable. Advantages that we''ve found for this are: 1. It keeps the template files from being cluttered with ruby snippets which is a big plus as the template seems to be the last place that I look when I''m asking ''how was this value computed?'' 2. The generated value is reusable in different templates and puppet subclasses 3. The value generation occurs outside of puppet and is usable to other non-puppet programs and scripts. We have a script like this that determines the puppetmaster url for a particular node and use it in both a xen role script as well as a puppet manifest. 4. It is not a custom fact, and does not inherit any issues from plugin syncing. James. Richard Hurt wrote: | Hey all, | | I am trying to get a modified hostname (converting "blah-1234567" to | "1234567") to use in my manifests and templates. I have it working for | templates by using Ruby (/<%= hostname.split(''-'').pop.downcase %>/) but | I haven''t been able to use it in manifests because they don''t process | ERB. The way I see it I could do one of two things; create a custom | function or create a custom fact. I think I would rather build a | server-side function but I haven''t had much luck getting them to work. | So I started looking at creating a custom fact but the | Wiki/documentation is quite convoluted and I haven''t been able to gain | any ground there either. | | Which method would be better, simpler, and less client-side intrusive? | Does anyone have an example of a custom function that I could copy? | Where on the server to place it? I tried creating my own function in | /usr/local/lib/site_ruby/puppet/parser/functions/keyname.rb but it''s | giving me a "...private method ''split'' called for ["blah-1234567"]:Array | at ..." error. Or should I just use a facter fact? I tried to make | sense of the Wiki but its just not sinking in. It doesn''t help that | Puppet is in its growth stage right now so some of the information out | there is no longer valid. My copy of "Pulling String with Puppet" is on | its way, however I''m afraid that by the time it gets here it will be out | of date. :( | | Anyway, I''m running Puppet 0.24.4 on Debian Etch on both client & server | and here is my first try at a puppet function: | | # keyname.rb | module Puppet::Parser::Functions | newfunction(:keyname, :type => :rvalue) do |args| | args.split(''-'').pop.downcase | end | end | | | -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.7 (GNU/Linux) iD8DBQFIG2D98TYLVDRcCYURAlAnAJwJjsmpYVxWKcIeLEqa1+qK+eomPACfbqFv RYIiJEWUxtVkIzxvw8q5kxw=pA6i -----END PGP SIGNATURE----- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com To unsubscribe from this group, send email to puppet-users-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en -~----------~----~----~----~------~----~------~--~---
Blake Barnett
2008-May-02 18:47 UTC
[Puppet Users] Re: Functions vs. Module Plugins vs. Facts
On May 2, 2008, at 11:25 AM, Richard Hurt wrote:> > # keyname.rb > module Puppet::Parser::Functions > newfunction(:keyname, :type => :rvalue) do |args| > args.split(''-'').pop.downcase > end > end >args is an Array, and I assume you want the numeric portion of "blah-1351515", so you just need: keyname = args[0].split(''-'')[1].downcase Assuming you''ll only ever have a single argument. (Use a variable to avoid the wonderful memory leak split can introduce.) -Blake --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com To unsubscribe from this group, send email to puppet-users-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en -~----------~----~----~----~------~----~------~--~---
Peter Meier
2008-May-02 18:56 UTC
[Puppet Users] Re: Functions vs. Module Plugins vs. Facts
Hi> Which method would be better, simpler, and less client-side intrusive? Does > anyone have an example of a custom function that I could copy? Where on the > server to place it? I tried creating my own function in > /usr/local/lib/site_ruby/puppet/parser/functions/keyname.rb but it''s giving > me a "...private method ''split'' called for ["blah-1234567"]:Array at ..." > error. Or should I just use a facter fact? I tried to make sense of the > Wiki but its just not sinking in. It doesn''t help that Puppet is in its > growth stage right now so some of the information out there is no longer > valid. My copy of "Pulling String with Puppet" is on its way, however I''m > afraid that by the time it gets here it will be out of date. :(i would simply add a custom fact. With that you can do all want you want and reuse this variable in any location (define, manifests, templates). greets pete --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com To unsubscribe from this group, send email to puppet-users-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en -~----------~----~----~----~------~----~------~--~---
James Turnbull
2008-May-03 02:27 UTC
[Puppet Users] Re: Functions vs. Module Plugins vs. Facts
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Richard Hurt wrote:> there is no longer valid. My copy of "Pulling String with Puppet" is on > its way, however I''m afraid that by the time it gets here it will be out > of date. :(As the book is POD - Print on Demand - I''ve been fixing all the errata and some issues with the changes Luke has been submitting. So it should be still up to date. Regards James Turnbull - -- James Turnbull (james@lovedthanlost.net) - -- Author of: - - Pulling Strings with Puppet (http://www.amazon.com/gp/product/1590599780/) - - Pro Nagios 2.0 (http://www.amazon.com/gp/product/1590596099/) - - Hardening Linux (http://www.amazon.com/gp/product/1590594444/) -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFIG82M9hTGvAxC30ARAnZEAKC/xDbGYV0V+x9etorQdlQGLYPwwwCeIpQ7 tggASVN0QAXSW5MEP/4+KXo=trQz -----END PGP SIGNATURE----- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com To unsubscribe from this group, send email to puppet-users-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en -~----------~----~----~----~------~----~------~--~---