Sha256: 60e093444fa1fe91828b11de6f7b14a6e6bf625d39815b6a6b953ce7523cbc8d

Contents?: true

Size: 1.53 KB

Versions: 105

Compression:

Stored size: 1.53 KB

Contents

# The `yaml_data` is a hiera 5 `data_hash` data provider function.
# See [the configuration guide documentation](https://puppet.com/docs/puppet/latest/hiera_config_yaml_5.html#configuring-a-hierarchy-level-built-in-backends) for
# how to use this function.
#
# @since 4.8.0
#
require 'yaml'

Puppet::Functions.create_function(:yaml_data) do
  dispatch :yaml_data do
    param 'Struct[{path=>String[1]}]', :options
    param 'Puppet::LookupContext', :context
  end

  argument_mismatch :missing_path do
    param 'Hash', :options
    param 'Puppet::LookupContext', :context
  end

  def yaml_data(options, context)
    path = options['path']
    context.cached_file_data(path) do |content|
      begin
        data = Puppet::Util::Yaml.safe_load(content, [Symbol], path)
        if data.is_a?(Hash)
          Puppet::Pops::Lookup::HieraConfig.symkeys_to_string(data)
        else
          msg = _("%{path}: file does not contain a valid yaml hash" % { path: path })
          raise Puppet::DataBinding::LookupError, msg if Puppet[:strict] == :error && data != false
          Puppet.warning(msg)
          {}
        end
      rescue Puppet::Util::Yaml::YamlLoadError => ex
        # YamlLoadErrors include the absolute path to the file, so no need to add that
        raise Puppet::DataBinding::LookupError, _("Unable to parse %{message}") % { message: ex.message }
      end
    end
  end

  def missing_path(options, context)
    "one of 'path', 'paths' 'glob', 'globs' or 'mapped_paths' must be declared in hiera.yaml when using this data_hash function"
  end
end

Version data entries

105 entries across 105 versions & 2 rubygems

Version Path
puppet-6.4.5 lib/puppet/functions/yaml_data.rb
puppet-6.4.5-x86-mingw32 lib/puppet/functions/yaml_data.rb
puppet-6.4.5-x64-mingw32 lib/puppet/functions/yaml_data.rb
puppet-6.4.5-universal-darwin lib/puppet/functions/yaml_data.rb
puppet-6.4.4 lib/puppet/functions/yaml_data.rb
puppet-6.4.4-x86-mingw32 lib/puppet/functions/yaml_data.rb
puppet-6.4.4-x64-mingw32 lib/puppet/functions/yaml_data.rb
puppet-6.4.4-universal-darwin lib/puppet/functions/yaml_data.rb
puppet-6.9.0 lib/puppet/functions/yaml_data.rb
puppet-6.9.0-x86-mingw32 lib/puppet/functions/yaml_data.rb
puppet-6.9.0-x64-mingw32 lib/puppet/functions/yaml_data.rb
puppet-6.9.0-universal-darwin lib/puppet/functions/yaml_data.rb
puppet-6.8.1 lib/puppet/functions/yaml_data.rb
puppet-6.8.1-x86-mingw32 lib/puppet/functions/yaml_data.rb
puppet-6.8.1-x64-mingw32 lib/puppet/functions/yaml_data.rb
puppet-6.8.1-universal-darwin lib/puppet/functions/yaml_data.rb
puppet-6.8.0 lib/puppet/functions/yaml_data.rb
puppet-6.8.0-x86-mingw32 lib/puppet/functions/yaml_data.rb
puppet-6.8.0-x64-mingw32 lib/puppet/functions/yaml_data.rb
puppet-6.8.0-universal-darwin lib/puppet/functions/yaml_data.rb