From c73be8fdbd3ed726604f644a1cb66f0f99889baa Mon Sep 17 00:00:00 2001 From: Krzysztof Wilczynski Date: Sun, 13 Mar 2011 23:37:16 +0000 Subject: Changing the name of the file to reflect change in the function name. --- load_variables.rb | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 load_variables.rb (limited to 'load_variables.rb') diff --git a/load_variables.rb b/load_variables.rb new file mode 100644 index 0000000..ddfc0ef --- /dev/null +++ b/load_variables.rb @@ -0,0 +1,72 @@ +# +# load_vars.rb +# +# This script will allow for loading variables from an external YAML +# file and expose them for further use inside the Puppet manifest file ... +# +# For example: +# +# Given following content of the data.yaml file: +# +# --- +# host1.example.com: +# foo: bar +# baz: quux +# question: 42 +# host2.example.com: +# abc: def +# this: that +# darth: vader +# +# Then calling load_vars in Puppet manifest file as follows: +# +# load_vars("/etc/puppet/data.yaml", $fqdn) +# +# Will result in addition of variables $foo, $baz and $question +# for matching host name as per the variable $fqdn ... +# +# Another example which uses per-host file: +# +# Given following content of the file data-host1.example.com.yaml: +# +# --- +# foo: bar +# +# Then when we call load_vars like this: +# +# load_vars("/etc/puppet/data-$fqdn.yaml") +# +# This will result in a variable $foo being added and ready for use. +# + +module Puppet::Parser::Functions + newfunction(:load_vars, :type => :statement) do |arguments| + + raise(Puppet::ParseError, "Wrong number of arguments " + + "given (#{arguments.size} for 2)") if arguments.size < 2 + + data = {} + + file = arguments[0] + key = arguments[1] if arguments[1] + + if File.exists?(file) + + begin + data = YAML.load_file(file) + rescue => error + raise(Puppet::ParseError, "Unable to load data " + + "from the file `%s': %s" % file, error.to_s) + end + + raise(Puppet::ParseError, "Data in the file `%s' " + + "is not a hash" % file) unless data.is_a?(Hash) + + data = ((data[key] and data[key].is_a?(Hash)) ? data[key] : {}) if key + end + + data.each { |param, value| setvar(param, strinterp(value)) } + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3