Forge Home

hashfile

pdk
Convert a puppet hash to YAML, JSON, INI file, or key-value

486 downloads

447 latest version

5.0 quality score

Version information

  • 1.3.1 (latest)
  • 1.3.0
released Aug 22nd 2021
This version is compatible with:
  • Puppet Enterprise 2021.3.x, 2021.2.x, 2021.1.x, 2021.0.x, 2019.8.x, 2019.7.x, 2019.5.x, 2019.4.x, 2019.3.x, 2019.2.x, 2019.1.x, 2019.0.x, 2018.1.x, 2017.3.x, 2017.2.x, 2016.4.x
  • Puppet >= 4.10.0 < 8.0.0
  • CentOS, OracleLinux, RedHat, Scientific, Debian, Ubuntu, windows, Solaris, SLES, Darwin, Fedora

Start using this module

  • r10k or Code Manager
  • Bolt
  • Manual installation
  • Direct download

Add this module to your Puppetfile:

mod 'southalc-hashfile', '1.3.1'
Learn more about managing modules with a Puppetfile

Add this module to your Bolt project:

bolt module add southalc-hashfile
Learn more about using this module with an existing project

Manually install this module globally with Puppet module tool:

puppet module install southalc-hashfile --version 1.3.1

Direct download is not typically how you would use a Puppet module to manage your infrastructure, but you may want to download the module in order to inspect the code.

Download

Documentation

southalc/hashfile — version 1.3.1 Aug 22nd 2021

Overview

Forked from puppet-hash2stuff by Mark McKinstry, which is no longer maintained.

This module will convert puppet hashes in to different formats commonly used for config files. It is used to overwrite an entire config file with one from puppet, if you are trying to manage bits and pieces of a config file, you want to use something like puppetlabs/inifile or augeas.

Usage

hash2ini

Converts a hash into an INI file format. Type: rvalue.

It is used when you want to overwrite an entire file with a hash of settings. If you want to manage bits and pieces of an INI file, you want puppetlabs/inifile.

Parameters

It accepts the following optional parameters passed to it in a hash as the second argument:

  • header: String you want at the top of the file saying it is controlled by puppet. Default: '# THIS FILE IS CONTROLLED BY PUPPET'
  • section_prefix: String that will appear before the section's name. Default: '['
  • section_suffix: String that will appear after the section's name. Default: ']'
  • key_val_separator: String to use between setting name and value (e.g., to determine whether the separator includes whitespace). Default: '='.
  • quote_char: Character or string to quote the entire value of the setting. Default: '"'

For example:

$config = {
  'main' => {
    'logging' => 'INFO',
    'limit'   => 314,
    'awesome' => true,
  },
  'dev' => {
    'logging'      => 'DEBUG',
    'log_location' => '/var/log/dev.log',
  }
}

file {'/etc/config.ini':
  ensure  => 'present',
  content => hash2ini($config)
}

will produce a file at /etc/config.ini that looks like:

# THIS FILE IS CONTROLLED BY PUPPET

[main]
logging="INFO"
limit="314"
awesome="true"

[dev]
logging="DEBUG"
log_location="/var/log/dev.log"

Or you can specify custom settings:

$settings = {
  'header'            => '; THIS FILE IS CONTROLLED BY PUPPET',
  'key_val_separator' => ' = ',
  'quote_char'        => '',
}

$php_config = {
  'PHP' => {
    'engine'                  => 'On',
    'realpath_cache_size'     => '32k',
    'zlib.output_compression' => 'On',
    'expose_php'              => 'Off',
  },
  'Date' => {
    'date.timezone'           => '"America/Detroit"',
  }
}

file {'/etc/php.ini':
  ensure  => 'present',
  content => hash2ini($php_config, $settings)
}

will produce a file at /etc/php.ini that looks like:

; THIS FILE IS CONTROLLED BY PUPPET

[PHP]
engine = On
realpath_cache_size = 32k
zlib.output_compression = On
expose_php = Off

[Date]
date.timezone = "America/Detroit"

hash2json

This function overlaps with to_json and to_json_pretty from puppetlab's stdlib.

Converts a hash into a JSON string. Type: rvalue.

It is used when you want to overwrite an entire file with a hash of settings. If you want to manage bits and pieces of an JSON file, you want augeas with the JSON lens.

For example:

$config = {
  'domain' => 'example.com',
  'mysql'  => {
    'hosts' => ['192.0.2.2', '192.0.2.4'],
    'user'  => 'root',
    'pass'  => 'setec-astronomy',
  },
  'awesome' => true,
}

file {'/etc/config.json':
  ensure  => 'present',
  content => hash2json($config)
}

will produce a file at /etc/config.json that looks like:

{
  "domain": "example.com",
  "mysql": {
    "hosts": [
      "192.0.2.2",
      "192.0.2.4"
    ],
    "user": "root",
    "pass": "setec-astronomy"
  },
  "awesome": true
}

hash2kv

Converts a hash into an key-value/shellvar string. Type: rvalue.

It is used when you want to overwrite an entire file with a hash of settings. If you want to manage bits and pieces of an key-value/shellvar style file, you probably want herculesteam/augeasproviders_shellvar.

Parameters

It accepts the following optional parameters passed to it in a hash as the second argument:

  • header: String you want at the top of the file saying it is controlled by puppet. Default: '# THIS FILE IS CONTROLLED BY PUPPET'.
  • key_val_separator: String to use between setting name and value (e.g., to determine whether the separator includes whitespace). Default: '='.
  • quote_char: Character or string to quote the entire value of the setting. Default: '"'.
  • quote_booleans: A boolean controlling whether or not to quote boolean values. Default: 'true'.
  • quote_numerics: A boolean controlling whether or not to quote numeric values. Default: 'true'.

For example:

$config = {
  'HOSTNAME'     => 'foo.example.com',
  'RSYNC_IONICE' => '3',
  'PORTS'        => '53 123 80',
}

file {'/etc/config.sh':
  ensure  => 'present',
  content => hash2kv($config)
}

will produce a file at /etc/config.sh that looks like:

# THIS FILE IS CONTROLLED BY PUPPET

HOSTNAME="foo.example.com"
RSYNC_IONICE="3"
PORTS="53 123 80"

Or you can specify custom settings:

$settings = {
  'header'            => '; THIS FILE IS CONTROLLED BY PUPPET',
  'key_val_separator' => ': ',
  'quote_char'        => '',
}

$config = {
  'HOSTNAME'     => 'foo.example.com',
  'RSYNC_IONICE' => '3',
  'PORTS'        => '53 123 80',
}

file {'/etc/config.kv':
  ensure  => 'present',
  content => hash2kv($php_config, $settings)
}

will produce a file at /etc/config.kv that looks like:

; THIS FILE IS CONTROLLED BY /dev/random

HOSTNAME: foo.example.com
RSYNC_IONICE: 3
PORTS: 53 123 80

hash2properties

Converts a hash into a Java properties file. Type: rvalue.

Parameters

It accepts the following optional parameters passed to it in a hash as the second argument:

  • header: String you want at the top of the file saying it is controlled by puppet. Default: '# THIS FILE IS CONTROLLED BY PUPPET'.
  • key_val_separator: String to use between setting name and value (e.g., to determine whether the separator includes whitespace). Default: '='.
  • quote_char: Character or string to quote the entire value of the setting. Default: '"'.
  • list_separator: Character to separate lists

For example:

$config = {
  'main' => {
    'logging' => 'INFO',
    'limit'   => 314,
    'awesome' => true,
    'nested'  => {
      'sublevel1' => 'value1',
      'subnested1' => {
        'node1' => 'leaf1',
        'node2' => 'leaf2',
      },
      'list' => [
        'item1',
        'item2',
      ]
    }
  },
  'dev' => {
    'logging'      => 'DEBUG',
    'log_location' => '/var/log/dev.log',
  }
}


file {'/etc/config.properties':
  ensure  => 'present',
  content => hash2properties($config)
}

will produce a file at /etc/config.properties that looks like:

# THIS FILE IS CONTROLLED BY PUPPET

dev.log_location=/var/log/dev.log
dev.logging=DEBUG
main.awesome=true
main.limit=314
main.logging=INFO
main.nested.list=item1,item2
main.nested.sublevel1=value1
main.nested.subnested1.node1=leaf1
main.nested.subnested1.node2=leaf2

Or you can specify custom settings:

settings = {
  'header'            => '# THIS FILE IS CONTROLLED BY /dev/random',
  'key_val_separator' => ': ',
  'quote_char'        => '"',
}

$config = {
  'main' => {
    'logging' => 'INFO',
    'limit'   => 314,
    'awesome' => true,
    'nested'  => {
      'sublevel1' => 'value1',
      'subnested1' => {
        'node1' => 'leaf1',
        'node2' => 'leaf2',
      },
      'list' => [
        'item1',
        'item2',
      ]
    }
  },
  'dev' => {
    'logging'      => 'DEBUG',
    'log_location' => '/var/log/dev.log',
  }
}

file {'/etc/config.properties':
  ensure  => 'present',
  content => hash2properites($config, $settings)
}

will produce a file at /etc/config.properties that looks like:

# THIS FILE IS CONTROLLED BY /dev/random

dev.log_location: "/var/log/dev.log"
dev.logging: "DEBUG"
main.awesome: "true"
main.limit: "314"
main.logging: "INFO"
main.nested.list: "item1,item2"
main.nested.sublevel1: "value1"
main.nested.subnested1.node1: "leaf1"
main.nested.subnested1.node2: "leaf2"

hash2yaml

This function overlaps with to_yaml from puppetlab's stdlib. This function does provide a header option to give a string saying the file is controlled by puppet.

Converts a hash into a YAML string. Type: rvalue.

It is used when you want to overwrite an entire file with a hash of settings. If you want to manage bits and pieces of an YAML file, you want augeas with the YAML lens.

Parameters

It accepts the following optional parameters passed to it in a hash as the second argument:

  • header: String you want at the top of the file saying it is controlled by puppet. Default: '""'.

For example:

$config = {
  'domain' => 'example.com',
  'mysql'  => {
    'hosts' => ['192.0.2.2', '192.0.2.4'],
    'user'  => 'root',
    'pass'  => 'setec-astronomy',
  },
  'awesome' => true,
}

file {'/etc/config.yaml':
  ensure  => 'present',
  content => hash2yaml($config)
}

will produce a file at /etc/config.yaml that looks like:

---
domain: example.com
mysql:
  hosts:
  - 192.0.2.2
  - 192.0.2.4
  user: root
  pass: setec-astronomy
awesome: true

Or you can specify custom settings:

$settings = {
  'header' => '# THIS FILE IS CONTROLLED BY PUPPET',
}

$config = {
  'domain' => 'example.com',
  'mysql'  => {
    'hosts' => ['192.0.2.2', '192.0.2.4'],
    'user'  => 'root',
    'pass'  => 'setec-astronomy',
  },
  'awesome' => true,
}

file {'/etc/config.yaml':
  ensure  => 'present',
  content => hash2yaml($config, $settings)
}

That will produce a file at /etc/config.yaml that looks like:

# THIS FILE IS CONTROLLED BY PUPPET
---
domain: example.com
mysql:
  hosts:
  - 192.0.2.2
  - 192.0.2.4
  user: root
  pass: setec-astronomy
awesome: true