Puppet Function: hash2kv

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

Summary

Converts a puppet hash to key/value (SHELLVAR) file string.

Overview

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

Examples:

Call the function with the $input and $options hashes

hash2ini($input, $options)

Parameters:

  • input (Hash)

    The hash to be converted to K/V file

  • options (Optional[Hash])

    A hash of options to control K/V file format

Returns:

  • (String)

    A K/V file 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
30
# File 'lib/puppet/functions/hash2kv.rb', line 2

Puppet::Functions.create_function(:hash2kv) do
  # @param input The hash to be converted to K/V file
  # @param options A hash of options to control K/V file format
  # @return [String] A K/V file formatted string
  # @example Call the function with the $input and $options hashes
  #   hash2ini($input, $options)
  dispatch :kv do
    param 'Hash', :input
    optional_param 'Hash', :options
  end

  def kv(input, options = {})
    settings = {
      'header'            => '# THIS FILE IS CONTROLLED BY PUPPET',
      'key_val_separator' => '=',
      'quote_char'        => '"',
    }

    settings.merge!(options)

    output = []
    output << settings['header'] << nil
    input.each do |k, v|
      output << "#{k}#{settings['key_val_separator']}#{settings['quote_char']}#{v}#{settings['quote_char']}"
    end
    output << nil
    output.join("\n")
  end
end