Thanks Jeff, I copy-pasted the code and it just worked. Here is my function:
require 'uri' module Puppet::Parser::Functions newfunction(:loadyaml, :doc => <<-'ENDHEREDOC') do |args| Load a YAML file containing a hash, set the hash values. Support puppet urls For example: loadyaml('/etc/puppet/data/myhash.yaml') loadyaml('puppet:///modules/name/myhash.yaml') ENDHEREDOC unless args.length == 1 raise Puppet::ParseError, ("loadyaml(): wrong number of arguments (#{args.length}; must be 1)") end params = nil path = args[0] unless Puppet::Util.absolute_path?(path) uri = URI.parse(URI.escape(path)) raise Puppet::ParseError, ("Cannot use relative URLs '#{path}'") unless uri.absolute? raise Puppet::ParseError, ("Cannot use opaque URLs '#{path}'") unless uri.hierarchical? raise Puppet::ParseError, ("Cannot use URLs of type '#{uri.scheme}' as source for fileserving") unless %w{puppet}.include?(uri.scheme) Puppet.info "loading parameters from #{path}" content = Puppet::FileServing::Content.indirection.find(path) params = YAML.load(content.content) else params = YAML.load_file(path) end params.each do |param, value| setvar(param, value) end end end 2012/11/14 Jeff McCune <j...@puppetlabs.com>: > On Tue, Nov 13, 2012 at 5:25 AM, woosley. xu. <redic...@gmail.com> wrote: >> I am thinking about loading some paramters from a external file for my >> souces. Eg, >> >> class test { >> >> ## set parameters. >> load_from_yaml("puppet:///modules/test/config.yaml") >> } > > Ah, cool idea. I think this would be a really valuable function to > include in the stdlib [1] module. > > While it's very difficult to get the filesystem path, it seems like > you really only need the contents of the file. If you have a > puppet:// URI, you can speak to the Puppet fileserve using the REST > API to get the file contents. This would avoid the need to read the > file from disk yourself and it will work well even in multiple-master > deployment scenarios since you're speaking to the API rather than to > the underlying filesystem. > > The static compiler does just this. It reads the contents of the file > using the FileServer API in the store_content method: > https://github.com/puppetlabs/puppet/blob/master/lib/puppet/indirector/catalog/static_compiler.rb#L160-161 > > You might be able to do something similar in your parser function. > Given a URI, pass the string form to > Puppet::FileServing::Content.indirection.find and you should get back > a Ruby instance that models the file metadata and contains the > content. > > Hope this helps, > -Jeff > > [1] https://forge.puppetlabs.com/puppetlabs/stdlib > > -- > You received this message because you are subscribed to the Google Groups > "Puppet Users" group. > To post to this group, send email to puppet-users@googlegroups.com. > To unsubscribe from this group, send email to > puppet-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/puppet-users?hl=en. > -- woosley.xu. http://twitter.com/redicaps -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.