Skip to content

Instantly share code, notes, and snippets.

@winks
Created May 30, 2012 14:47
Show Gist options
  • Save winks/2836769 to your computer and use it in GitHub Desktop.
Save winks/2836769 to your computer and use it in GitHub Desktop.
strawman - a simple puppet template checker
#!/usr/bin/env ruby
# strawman - a simple puppet template checker.
# use like:
# echo 'fqdn => test.example.org\n$webserver = ["host1", "host2"]' | strawman template.erb
# where fqdn is in facter format:
# key => value
# and $webserver is a global ruby var, like in your manifests
# seperated by newlines, delivered on STDIN
require 'erb'
if ARGV[0].nil? then
puts 'Usage: echo "fqdn => test.example.org\n$var = \'test\'" | ' + __FILE__ + ' <template.erb>'
exit
end
debug = false
facts = {}
facts['strawman'] = 'ip-1-2-3-4'
input = (STDIN.tty?) ? nil : STDIN.read.split("\n")
input.each do |line|
if line =~ /^\$/ then
line = $'
eval line, TOPLEVEL_BINDING
else
k, v = line.split(' => ')
facts[k] = v
end
end
template = File.read(ARGV[0])
STDERR.puts "Using these facts:" if debug
facts.each do |k,v|
STDERR.puts k + " : " + v if debug
global_ns_key = "\\$" + k
root_ns_key = "\\$::" + k
template = template.gsub /#{global_ns_key}/, "facts['" + k + "']"
template = template.gsub /#{root_ns_key}/, "facts['" + k + "']"
end
STDERR.puts template if debug
renderer = ERB.new(template, nil, '%<>-')
puts output = renderer.result(TOPLEVEL_BINDING)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment