Puppet: System Administration Automated

Support

The show-node-objects script

This script accepts a host name and produces a list of objects in manifest syntax associated with that host. It is meant to be run on the puppetmaster host. By default only the 'hostname' fact is supplied but other facts can be specified by using the '--fact` option (multiple times if needed). If the argument to '--fact' does not contain a '=' sign it is interpreted as a file containing facts in YAML format as output by 'facter --yaml'.

#!/usr/bin/ruby
# -*- Mode: ruby; indent-tabs-mode: nil -*-
# vim:expandtab softtabstop=2 shiftwidth=2
#
# show-node-objects -- show the list of objects associated with a given node
#

require 'getoptlong'
require 'yaml'
require 'puppet'

options = [
  [ '--fact', '-F', GetoptLong::REQUIRED_ARGUMENT ]
]
# Add all of the config parameters as valid options.
Puppet.config.addargs(options)

result = GetoptLong.new(*options)

manifest = Puppet[:manifest]
facts = {}
result.each do |opt, arg|
  case opt
  when '--fact'
    fact = arg
    if fact =~ /=/
      key, value = fact.split('=', 2)
      facts[key] = value
    else
      File.open(arg, 'r') do |file|
        YAML.load(file).each do |key, value|
          facts[key] = value
        end
      end
    end
  when '--manifest'
    manifest = arg
  else
    Puppet.config.handlearg(opt, arg)
  end
end

hostname = ARGV.shift
facts['hostname'] = hostname

Puppet.config.parse("/etc/puppet/puppet.conf")

master_opts = {
  :Local => true,
  :Manifest => manifest,
  :NodeSources => [:external, :code],
  :UseNodes => true,
}
master = Puppet::Network::Handler.master.new(master_opts)

objects = master.getconfig(facts, 'yaml', hostname)
objects.flatten.each do |obj|
  puts obj.to_manifest
end

exit