Skip to content

Instantly share code, notes, and snippets.

@robinbowes
Created June 22, 2012 08:54
Show Gist options
  • Save robinbowes/2971463 to your computer and use it in GitHub Desktop.
Save robinbowes/2971463 to your computer and use it in GitHub Desktop.
module Puppet::Parser::Functions
newfunction(:hiera_hash, :type => :rvalue) do |*args|
if args[0].is_a?(Array)
args = args[0]
end
key = args[0]
default = args[1]
override = args[2]
configfile = File.join([File.dirname(Puppet.settings[:config]), "hiera.yaml"])
raise(Puppet::ParseError, "Hiera config file #{configfile} not readable") unless File.exist?(configfile)
raise(Puppet::ParseError, "You need rubygems to use Hiera") unless Puppet.features.rubygems?
require 'hiera'
require 'hiera/scope'
config = YAML.load_file(configfile)
config[:logger] = "puppet"
hiera = Hiera.new(:config => config)
if self.respond_to?("{}")
hiera_scope = self
else
hiera_scope = Hiera::Scope.new(self)
end
answer = hiera.lookup(key, default, hiera_scope, override, :hash)
return answer if answer == default
raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?
answer
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment