2011-07-25 19:42:27 +02:00
|
|
|
module Puppet::Parser::Functions
|
2016-04-26 20:51:43 +02:00
|
|
|
newfunction(:loadyaml, :type => :rvalue, :arity => -2, :doc => <<-'ENDHEREDOC') do |args|
|
|
|
|
Load a YAML file containing an array, string, or hash, and return the data
|
|
|
|
in the corresponding native data type.
|
|
|
|
The second parameter is the default value. It will be returned if the file
|
|
|
|
was not found or could not be parsed.
|
2011-07-25 19:42:27 +02:00
|
|
|
|
2016-04-26 20:51:43 +02:00
|
|
|
For example:
|
2011-07-25 19:42:27 +02:00
|
|
|
|
2016-04-26 20:51:43 +02:00
|
|
|
$myhash = loadyaml('/etc/puppet/data/myhash.yaml')
|
|
|
|
$myhash = loadyaml('no-file.yaml', {'default' => 'value'})
|
|
|
|
ENDHEREDOC
|
2011-07-25 19:42:27 +02:00
|
|
|
|
2016-04-26 20:51:43 +02:00
|
|
|
raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless args.length >= 1
|
|
|
|
require 'yaml'
|
2011-07-25 19:42:27 +02:00
|
|
|
|
2016-04-26 20:51:43 +02:00
|
|
|
if File.exists?(args[0])
|
|
|
|
begin
|
|
|
|
YAML::load_file(args[0]) || args[1]
|
|
|
|
rescue Exception => e
|
|
|
|
if args[1]
|
|
|
|
args[1]
|
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 11:35:42 +02:00
|
|
|
else
|
2016-04-26 20:51:43 +02:00
|
|
|
warning("Can't load '#{args[0]}' File does not exist!")
|
|
|
|
args[1]
|
2014-09-02 11:35:42 +02:00
|
|
|
end
|
2011-07-25 19:42:27 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|