Puppet Function: loadyaml

Defined in:
puppet/modules/stdlib/lib/puppet/parser/functions/loadyaml.rb
Function type:
Ruby 3.x API

Overview

loadyaml()Any

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.

For example:

$myhash = loadyaml('/etc/puppet/data/myhash.yaml')
$myhash = loadyaml('no-file.yaml', {'default' => 'value'})

Returns:

  • (Any)


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'puppet/modules/stdlib/lib/puppet/parser/functions/loadyaml.rb', line 2

newfunction(:loadyaml, :type => :rvalue, :arity => -2, :doc => <<-'ENDHEREDOC') do |args|

  raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless args.length >= 1
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.

For example:

  $myhash = loadyaml('/etc/puppet/data/myhash.yaml')
  $myhash = loadyaml('no-file.yaml', {'default' => 'value'})
ENDHEREDOC
  require 'yaml'

  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
  else
    warning("Can't load '#{args[0]}' File does not exist!")
    args[1]
  end

end