Skip to content

reliantsolutions/hiera-resources

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 

Repository files navigation

Overview

hiera_resources has 1 job in life; create Puppet resources from a hash returned by Hiera. The hash returned should match the structure of the hash required by Puppet's create_resources function. Examples for using this with YAML and Redis (serialized) backends are included below.

Credit

This version of hiera_resources is basically a complete refactoring based on this excellent blog post by Robin Bowes.

Setup for both YAML and Redis backends

Ensure the following gem versions are installed:

This function should exist in a place where puppet can find it. ~/.puppet/var/lib/puppet/parser/functions is certainly fine for testing purposes.

Create a Hiera configuration in ~/.puppet/hiera.yaml

---
:hierarchy:
  - common
:backends:
  - yaml
  - redis
:yaml:
  :datadir: /tmp/hiera/data
$ mkdir -p /tmp/hiera/data

Create some YAML data in /tmp/hiera/data/common.yaml

---
messages1:
  notify:
    title 1:
      message: this is the first message stored in YAML
    title 2:
      message: this is the second message stored in YAML

Creating Puppet resources from the YAML backend

Apply a manifest

$ puppet apply -e "hiera_resources('messages1')"
notice: This is the second message stored in YAML.
notice: /Stage[main]//Notify[title 2]/message: defined 'message' as 'This is the second message stored in YAML.'
notice: This is the first message stored in YAML.
notice: /Stage[main]//Notify[title 1]/message: defined 'message' as 'This is the first message stored in YAML.'

Creating Puppet resources from the Redis backend

Make sure Redis is running on localhost:6379 (or tweak the call to Redis.new below)

Fire up your favorite ruby REPL and add a few serialized Puppet resources into a Redis key.

require 'redis'
require 'json'

resources = { 'notify' => {
  'title 1' => {
    'message' => 'This is the first message stored in Redis.'
    },
  'title 2' => {
    'message' => 'This is the second message stored in Redis.'
    }
  }
}

r = Redis.new
r.set 'common:messages2', resources.to_json

Configure deserialization in ~/.puppet/hiera.yaml (use :yaml instead of :json if appropriate).

:redis:
  :deserialize: :json

Now apply a manifest

$ puppet apply -e "hiera_resources('messages2')"
notice: This is the second message stored in Redis.
notice: /Stage[main]//Notify[title 2]/message: defined 'message' as 'This is the second message stored in Redis.'
notice: This is the first message stored in Redis.
notice: /Stage[main]//Notify[title 1]/message: defined 'message' as 'This is the first message stored in Redis.'

Additional features

hiera_resources will accept a hash as a 2nd argument. When present, the hash will be used as a default if the key can not be found.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages