Skip to content

Instantly share code, notes, and snippets.

@glarizza
Created December 5, 2013 15:58
Show Gist options
  • Save glarizza/7807981 to your computer and use it in GitHub Desktop.
Save glarizza/7807981 to your computer and use it in GitHub Desktop.
# <modulepath>/custom_functions/lib/puppet/parser/functions/extract.rb
require 'yaml'
Puppet::Parser::Functions.newfunction(:extract, :type => :rvalue, :doc => "YAML Extract") do |args|
raise Puppet::Error, "#extract: accepts only two arguments, you passed #{args.length}" if args.length != 2
file = args[0]
field = args[1]
begin
file_hash = YAML.load_file(file)
rescue Errno::ENOENT => e
raise Puppet::Error, "#extract: file #{file} not found, please try again"
return nil
end
file_hash[field]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment