Created
April 18, 2014 07:34
-
-
Save paaloeye/11029584 to your computer and use it in GitHub Desktop.
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
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