Sha256: d81f0bd3975c444ab2abf1ecdf02210c2db5a2c45ae8a8a42148158286ce2781

Contents?: true

Size: 1.35 KB

Versions: 81

Compression:

Stored size: 1.35 KB

Contents

# The `yaml_data` is a hiera 5 `data_hash` data provider function.
# See [the configuration guide documentation](https://docs.puppet.com/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 = YAML.load(content, path)
        if data.is_a?(Hash)
          Puppet::Pops::Lookup::HieraConfig.symkeys_to_string(data)
        else
          Puppet.warning("#{path}: file does not contain a valid yaml hash")
          {}
        end
      rescue YAML::SyntaxError => ex
        # Psych errors includes the absolute path to the file, so no need to add that
        # to the message
        raise Puppet::DataBinding::LookupError, "Unable to parse #{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

81 entries across 81 versions & 3 rubygems

Version Path
puppet-retrospec-1.8.0 vendor/pup410/lib/puppet/functions/yaml_data.rb
puppet-retrospec-1.7.0 vendor/pup410/lib/puppet/functions/yaml_data.rb
puppet-4.10.12 lib/puppet/functions/yaml_data.rb
puppet-4.10.12-x86-mingw32 lib/puppet/functions/yaml_data.rb
puppet-4.10.12-x64-mingw32 lib/puppet/functions/yaml_data.rb
puppet-4.10.12-universal-darwin lib/puppet/functions/yaml_data.rb
puppet-4.10.11 lib/puppet/functions/yaml_data.rb
puppet-4.10.11-x86-mingw32 lib/puppet/functions/yaml_data.rb
puppet-4.10.11-x64-mingw32 lib/puppet/functions/yaml_data.rb
puppet-4.10.11-universal-darwin lib/puppet/functions/yaml_data.rb
puppet-4.10.10 lib/puppet/functions/yaml_data.rb
puppet-4.10.10-x86-mingw32 lib/puppet/functions/yaml_data.rb
puppet-4.10.10-x64-mingw32 lib/puppet/functions/yaml_data.rb
puppet-4.10.10-universal-darwin lib/puppet/functions/yaml_data.rb
bolt-0.15.0 vendored/puppet/lib/puppet/functions/yaml_data.rb
bolt-0.14.0 vendored/puppet/lib/puppet/functions/yaml_data.rb
bolt-0.13.0 vendored/puppet/lib/puppet/functions/yaml_data.rb
puppet-retrospec-1.6.1 vendor/pup410/lib/puppet/functions/yaml_data.rb
bolt-0.12.0 vendored/puppet/lib/puppet/functions/yaml_data.rb
bolt-0.11.0 vendored/puppet/lib/puppet/functions/yaml_data.rb