Created
February 10, 2018 03:01
-
-
Save benwtr/02426b3e069daf59beb32b3a7efdd7f1 to your computer and use it in GitHub Desktop.
Replacement for hiera using mixlib-config and a few lines of code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a puppet function similar to the hiera() lookup function. | |
# | |
# It can load yaml, json or ruby config files | |
# | |
# It uses [mixlib-config](https://github.com/chef/mixlib-config/) so, when writing ruby config files | |
# you have a flexible DSL at your disposal, similar to the chef attributes DSL. | |
# | |
# You could also use ruby any way you like inside these configuration files, including calling out | |
# to external data sources. | |
# | |
# This is not more than a proof of concept. | |
# | |
require 'mixlib/config' | |
module Attributes | |
extend Mixlib::Config | |
end | |
ATTRIBUTES_ROOT = '/tmp/layers' | |
Puppet::Parser::Functions::newfunction(:attribute, | |
:type => :rvalue, | |
:arity => 1, | |
:doc => "Similar to a hiera() lookup call" | |
) do |args| | |
def add_layer_if_present(layer) | |
if (result = lookupvar layer) | |
"#{layer}/#{result}" | |
end | |
end | |
layers = [] | |
layers << "common" | |
layers << add_layer_if_present('datacenter') | |
layers << add_layer_if_present('cluster') | |
layers << add_layer_if_present('node') | |
layers.compact! | |
files = layers.map do |layer| | |
Dir.glob ATTRIBUTES_ROOT + "/" + layer + ".*" | |
end.flatten | |
files.each do |file| | |
next unless File.file? file | |
Attributes.from_file file | |
end | |
attributes = Attributes.save | |
Attributes.reset | |
lookup_attribute = args[0].to_sym | |
attributes[lookup_attribute] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment