Pulling information from puppet stored config DB, part 2

Monday, October 05 2009 @ 04:10 PM BST

Contributed by: robin

In my previous post, I started exploring how to pull information from a puppet stored config DB to use in capistrano, inspired by joe-mac's post.

Well, it seems that the query I used was overly complex. Here's the simplified version:

select h.name
from hosts h
join resources r on h.id = r.host_id
where r.restype = 'Class'
and r.title = 'site::profile::dnscache::local'
order by h.name;

Having established this, I then modified joe-mac's ruby script to look up based on Class rather than facts. Here's the modified script:

#!/usr/bin/env ruby

require 'getoptlong'
require 'puppet'
require 'rdoc/usage'

   :adapter  => 'mysql',
   :database => 'puppet',
   :host     => 'localhost',
   :password => 'secret',
   :username => 'puppet'

class Hosts < Puppet::Rails::Host; end

opts = GetoptLong.new(
   [ '--class',    '-c', GetoptLong::REQUIRED_ARGUMENT],
   [ '--help',     '-h', GetoptLong::NO_ARGUMENT ],
   [ '--print',    '-p', GetoptLong::REQUIRED_ARGUMENT]

printtype = "name"
opt_hash = Hash.new()
opts.each do |opt, arg|
   case opt
      when '--class'
         opt_hash['class'] = arg
      when '--help'
      when '--print'
         printtype = arg

query = "(resources.restype = \'Class\' AND resources.title = \'#{opt_hash['class']}\')"

puts Hosts.find(:all,
                :include    => [ :resources ],
                :conditions => query
               ).map { |host| host.send(printtype) }

Next step: build this into my capistrano configuration as per joe-mac's example.


Comments (0)