Skip to content

Instantly share code, notes, and snippets.

@nanliu
Created August 7, 2012 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanliu/3289446 to your computer and use it in GitHub Desktop.
Save nanliu/3289446 to your computer and use it in GitHub Desktop.
Puppet::Parser::Functions::newfunction(:module_file, :type => :rvalue, :doc => "
Function module_file:
load file from relative or absolute path, relative path will search puppet modules files directory.
") do |args|
raise Puppet::ParseError, "module_file(): wrong number of arguments #{args.length}, expecting (filename)." if args.length != 1
raise Puppet::ParseError, "module_file(): does not accept empty file path." unless args[0]
file = args[0]
begin
# Perform relative lookup in modules/files dir.
if file != File.expand_path(file)
env = Puppet[:environment]
mod_name, file = file.split(File::SEPARATOR, 2)
mod = Puppet::Module.find(mod_name, env)
raise Puppet::Error "module_file(): invalid module name #{mod_name}" unless mod
path = mod.path
file = File.join(path, "files", file)
end
result = File.read(file)
rescue Exception => e
raise Puppet::ParseError, "module_file(): failed to read #{file}."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment