Puppet Function: to_yaml

Defined in:
vendor_modules/stdlib/lib/puppet/functions/to_yaml.rb
Function type:
Ruby 4.x API

Summary

Convert a data structure and output it as YAML

Overview

to_yaml(Any $data, Optional[Hash] $options)String

Examples:

Output YAML to a file

file { '/tmp/my.yaml':
  ensure  => file,
  content => to_yaml($myhash),
}

Use options to control the output format

file { '/tmp/my.yaml':
  ensure  => file,
  content => to_yaml($myhash, {indentation => 4})
}

Parameters:

  • data (Any)

    The data you want to convert to YAML

  • options (Optional[Hash])

    A hash of options that will be passed to Ruby's Psych library. Note, this could change between Puppet versions, but at time of writing these are `line_width`, `indentation`, and `canonical`.

Returns:

  • (String)

    The YAML document



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 'vendor_modules/stdlib/lib/puppet/functions/to_yaml.rb', line 6

Puppet::Functions.create_function(:to_yaml) do
  # @param data
  #   The data you want to convert to YAML
  # @param options
  #   A hash of options that will be passed to Ruby's Psych library. Note, this could change between Puppet versions, but at time of writing these are `line_width`, `indentation`, and `canonical`.
  #
  # @example Output YAML to a file
  #   file { '/tmp/my.yaml':
  #     ensure  => file,
  #     content => to_yaml($myhash),
  #   }
  # @example Use options to control the output format
  #   file { '/tmp/my.yaml':
  #     ensure  => file,
  #     content => to_yaml($myhash, {indentation => 4})
  #   }
  #
  # @return [String] The YAML document
  dispatch :to_yaml do
    param 'Any', :data
    optional_param 'Hash', :options
  end

  def to_yaml(data, options = {})
    data.to_yaml(options.transform_keys(&:to_sym))
  end
end