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.
This version of hiera_resources is basically a complete refactoring based on this excellent blog post by Robin Bowes.
Ensure the following gem versions are installed:
- hiera >= 1.0.0
- hiera-puppet >= 1.0.0
- hiera-redis >= 1.0.0
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
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.'
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.'
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.