Coffee & Beer

Rantings and Ravings of the technical sort

Updated: Puppet Facts for Puppet Classes

Just the other day on Google+ I got a comment form someone who had found my old “Puppet facts about puppet classes” post and had used it. Sadly, I had gone through a few revisoins after that post and never followed up. There as a bit of a memory leak, and I decided I wanted things done a little different. Instead of creating a fact per-class (and having n fact if the clas wasn’t used), I’d rather have a list of the classes, as one fact, I can regex/etc on. Our group has recently started creating facts like this as json arrays so we can prase the data easy later, and its a bit more readable even if not.

(puppet_classes_2.rb) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/ruby
#Get puppet classes, from /var/lib/puppet/classes.txt
#
require 'facter'
require 'json'
begin
        Facter.hostname
rescue
        Facter.loadfacts()
end
hostname = Facter.value('hostname')

classes_txt = "/var/lib/puppet/classes.txt"

if File.exists?(classes_txt) then
        f = File.new(classes_txt)
        classes = Array.new()
        f.readlines.each do |line|
                line = line.chomp.to_s
                line = line.sub(" ","_")
                classes.push(line)
        end
        classes.delete("settings")
        classes.delete("#{hostname}")
        Facter.add("puppet_classes") do
                setcode do
                        classes.to_json
                end
        end
end

thus, a node would have a fact like:

1
puppet_classes => ["base","salt::minion","ssh::service"]

Comments