Puppet Function: loadyaml

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

Summary

Load a YAML file containing an array, string, or hash, and return the data in the corresponding native data type.

Overview

loadyaml()Array|String|Hash

The first parameter can be a file path or a URL. The second parameter is the default value. It will be returned if the file was not found or could not be parsed.

Examples:

Example Usage:

$myhash = loadyaml('/etc/puppet/data/myhash.yaml')
$myhash = loadyaml('https://example.local/my_hash.yaml')
$myhash = loadyaml('https://username:password@example.local/my_hash.yaml')
$myhash = loadyaml('no-file.yaml', {'default' => 'val

Returns:

  • (Array|String|Hash)

    The data stored in the YAML file, the type depending on the type of data that was stored.



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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'vendor_modules/stdlib/lib/puppet/parser/functions/loadyaml.rb', line 7

newfunction(:loadyaml, type: :rvalue, arity: -2, doc: <<-'DOC') do |args|
  @summary
    Load a YAML file containing an array, string, or hash, and return the data
    in the corresponding native data type.

  The first parameter can be a file path or a URL.
  The second parameter is the default value. It will be returned if the file
  was not found or could not be parsed.

  @return [Array|String|Hash]
    The data stored in the YAML file, the type depending on the type of data that was stored.

  @example Example Usage:
      $myhash = loadyaml('/etc/puppet/data/myhash.yaml')
      $myhash = loadyaml('https://example.local/my_hash.yaml')
      $myhash = loadyaml('https://username:password@example.local/my_hash.yaml')
      $myhash = loadyaml('no-file.yaml', {'default' => 'value'})
DOC

  raise ArgumentError, 'Wrong number of arguments. 1 or 2 arguments should be provided.' unless args.length >= 1
  require 'yaml'
  require 'open-uri'
  begin
    if args[0].start_with?('http://', 'https://')
      username = ''
      password = ''
      if (match = args[0].match(%r{(http\://|https\://)(.*):(.*)@(.*)}))
        # If URL is in the format of https://username:password@example.local/my_hash.yaml
        protocol, username, password, path = match.captures
        url = "#{protocol}#{path}"
      elsif (match = args[0].match(%r{(http\:\/\/|https\:\/\/)(.*)@(.*)}))
        # If URL is in the format of https://username@example.local/my_hash.yaml
        protocol, username, path = match.captures
        url = "#{protocol}#{path}"
      else
        url = args[0]
      end
      begin
        contents = OpenURI.open_uri(url, http_basic_authentication: [username, password])
      rescue OpenURI::HTTPError => err
        res = err.io
        warning("Can't load '#{url}' HTTP Error Code: '#{res.status[0]}'")
        args[1]
      end
      if RUBY_PLATFORM != "java" && Gem::Version.new(Psych::VERSION) <= Gem::Version.new('3.0.2')
        YAML.safe_load(contents, [], [], true) || args[1]
      else
        YAML.safe_load(contents, aliases: true) || args[1]
      end
    elsif File.exist?(args[0])
      # Read the file first rather than calling YAML.load_file as ruby2.7
      # doesn't support the aliases option on YAML.load_file
      contents = File.read(args[0])
      if RUBY_PLATFORM != "java" && Gem::Version.new(Psych::VERSION) <= Gem::Version.new('3.0.2')
        YAML.safe_load(contents, [], [], true) || args[1]
      else
        YAML.safe_load(contents, aliases: true) || args[1]
      end
    else
      warning("Can't load '#{args[0]}' File does not exist!")
      args[1]
    end
  rescue StandardError => e
    raise e unless args[1]
    args[1]
  end
end