Puppet Function: hash2yaml

Defined in:
lib/puppet/functions/hash2yaml.rb
Function type:
Ruby 4.x API

Summary

Converts a puppet hash to YAML string.

Overview

hash2yaml(Hash $input, Optional[Hash] $options)String

Examples:

Call the function with the $input hash

hash2yaml($input)

Parameters:

  • input (Hash)

    The hash to be converted to YAML

  • options (Optional[Hash])

    A hash of options to control YAML file format

Returns:

  • (String)

    A YAML formatted string



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
# File 'lib/puppet/functions/hash2yaml.rb', line 2

Puppet::Functions.create_function(:hash2yaml) do
  # @param input The hash to be converted to YAML
  # @param options A hash of options to control YAML file format
  # @return [String] A YAML formatted string
  # @example Call the function with the $input hash
  #   hash2yaml($input)
  dispatch :yaml do
    param 'Hash', :input
    optional_param 'Hash', :options
  end

  require 'yaml'

  def yaml(input, options = {})
    settings = {
      'header' => '',
    }

    settings.merge!(options)

    output = if settings['header'].to_s.empty?
               input.to_yaml
             else
               "#{settings['header']}\n#{input.to_yaml}"
             end
    output
  end
end