Skip to content

Instantly share code, notes, and snippets.

@jordansissel
Created October 30, 2010 10:06
Show Gist options
  • Save jordansissel/655161 to your computer and use it in GitHub Desktop.
Save jordansissel/655161 to your computer and use it in GitHub Desktop.
Using the puppet config parser for a configfile in your own project.
#!/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
% 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"
@jordansissel
Copy link
Author

Bugs found so far in this hack:

  • parser functions aren't run
  • defaults don't work, like: "Input { tags => ... }"
  • overrides don't work, like: "Input <| query |> { tags +> 'something'} " - results in this error:
/usr/lib/ruby/gems/1.8/gems/puppet-2.6.2/lib/puppet/parser/compiler.rb:21:in `compile': Puppet::Parser::Compiler failed with error NoMethodError: undefined method `merge' for []:Array on node default (Puppet::Error)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment