Created
October 30, 2010 10:06
-
-
Save jordansissel/655161 to your computer and use it in GitHub Desktop.
Using the puppet config parser for a configfile in your own project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
require "rubygems" | |
require "puppet" # gem puppet | |
require "ap" # gem awesome_print | |
# Need to define a type for each type we want to have. | |
# Otherwise, unknown types are errors in puppet. | |
Puppet::Type.newtype(:input) do | |
newproperty(:name) do | |
desc "Input URL" | |
end # property :name | |
newproperty(:tags) do | |
desc "Tags for an input" | |
end # property 'tags' | |
end # type 'input' | |
# This is where we put the puppet manifest code. You could just use a file | |
# here, but I'm inlining it for easy reading. | |
Puppet[:code] = <<EOF | |
input { | |
[ "/var/log/messages", "/var/log/auth.log" ]: | |
tags => ["linux-syslog"]; | |
[ "/var/log/apache2/access.log" ]: | |
tags => ["apache-access"]; | |
} | |
# even custom defines work. | |
define fancypants() { | |
input { | |
"/tmp/$name": | |
tags => ["fancypants"]; | |
"/tmp/$name.1": | |
tags => ["fancypants"]; | |
"/tmp/$name.2": | |
tags => ["fancypants"]; | |
} | |
} | |
fancypants { | |
"hello": ; | |
} | |
EOF | |
# Now compile a catalog from our code. | |
node = Puppet::Node.find("default") | |
catalog = Puppet::Resource::Catalog.find("default", :use_node => node) | |
# Each vertex is a resource. | |
catalog.vertices.each do |resource| | |
next if ["Class", "Stage"].include?(resource.type ) | |
puts "Found resource '#{resource.to_s}' with tags: #{resource[:tags].inspect}" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% ruby puppet-parser-elsewhere.rb | |
Found resource 'Input[/tmp/hello.2]' with tags: "fancypants" | |
Found resource 'Input[/var/log/auth.log]' with tags: "linux-syslog" | |
Found resource 'Input[/tmp/hello.1]' with tags: "fancypants" | |
Found resource 'Input[/tmp/hello]' with tags: "fancypants" | |
Found resource 'Fancypants[hello]' with tags: nil | |
Found resource 'Input[/var/log/apache2/access.log]' with tags: "apache-access" | |
Found resource 'Input[/var/log/messages]' with tags: "linux-syslog" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bugs found so far in this hack: