Skip to content

Instantly share code, notes, and snippets.

@paaloeye
Created April 18, 2014 07:34
Show Gist options
  • Save paaloeye/11029584 to your computer and use it in GitHub Desktop.
Save paaloeye/11029584 to your computer and use it in GitHub Desktop.
require 'yaml'
module Puppet::Parser::Functions
newfunction(:generate_role, :type => :rvalue) do |args|
fact = args[0]
path = args[1]
default = args[2]
begin
db = YAML.load_file(path)
found = db.detect { |key,value| value.include?(fact) }
if found
found.first
else
if default
default
else
raise Puppet::ParseError, "Function[generate_role] given host(#{fact}) isn't found in #{path}"
end
end
rescue Psych::SyntaxError => e
raise Puppet::ParseError, "Invalid format in #{path}: #{e.message} - #{e.backtrace}}"
rescue StandardError => e
raise Puppet::ParseError, "UnknownError: #{e.message} - #{e.backtrace}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment