Reusing Puppet Providers

Last night I was thinking about writing scripts to manage services, packages etc in a heterogeneous environment. This is hard because the operating systems all behave differently.

This is of course a problem that Puppet solves with it’s providers system, after some chatting with Luke on IRC I now have a pretty nice solution.

Assuming you don’t mind shell scripting in Ruby here’s a pretty decent bit of Ruby to manage a service on many different environments.

require 'puppet'
 
service = ARGV.shift
action = ARGV.shift
 
ARGV.length > 0 ? hasstatus = true : hasstatus = false
 
begin
    svc = Puppet::Type.type(:service).new(:name => service, 
        :hasstatus => hasstatus).provider
 
    svc.send action
 
    puts("#{service} #{action} status: #{svc.status}")
rescue Exception => e
    puts("Could not #{action} service #{service}: #{e}")
end
# service.rb httpd stop true
httpd stop status: stopped
 
# service.rb httpd start true
httpd start status: running

You’d probably want to put in support for the pattern parameter to keep certain broken Operating Systems happy, but this is pretty nice and platform independent.

Tags: , ,

7 Responses to “Reusing Puppet Providers”

  1. Joe McDonagh 21. Oct, 2009 at 14:19 #

    Wow, that is actually pretty ingenious, would have never even thought of this.

  2. Hanno Liem 07. Nov, 2009 at 10:18 #

    Nice, elegant solution. I guess this is what you are using in mcollective?

  3. rip 07. Nov, 2009 at 10:20 #

    Hanno,

    Yeah the service and package agents for mcollective use this, it’s working great. The code above though is 0.25.x specific I think someone on the Puppet list mentioned how to make it work with 0.24.x

  4. Bau 21. Nov, 2009 at 12:44 #

    Hi,
    I am new to Puppet.I want get client system details…Please help me to do..

    Thanks
    Babu

  5. tjoe 24. Dec, 2009 at 00:59 #

    I get this:

    Could not start service httpd: private method `new’ called for Puppet::Type::Service:Class

    what’s wrong ?

  6. rip 24. Dec, 2009 at 01:19 #

    As mentioned in the comments the code is only for 0.25.x.

    See line 41 and 42 of this code for 0.24.x version.

Trackbacks/Pingbacks

  1. Puppet resources on demand | R.I.Pienaar - 07. Jul, 2010

    [...] time ago I wrote how to reuse Puppet providers in your Ruby script, I’ll take that a bit further here and show you to create any kind of [...]

Leave a Reply