Merge git://github.com/kwilczynski/puppet-functions
This commit is contained in:
commit
5caaf18c10
1 changed files with 55 additions and 42 deletions
87
load_vars.rb
87
load_vars.rb
|
@ -1,56 +1,69 @@
|
||||||
# vim: set ts=2 sw=2 et :
|
#!/usr/bin/env ruby
|
||||||
#
|
#
|
||||||
# load_data loads varibles from external yaml file.
|
# load_vars.rb
|
||||||
#
|
#
|
||||||
# EXAMPLE 1:
|
# This script will allow for loading variables from an external YAML
|
||||||
# data.yaml:
|
# file and expose them for further use inside the Puppet manifest file ...
|
||||||
# --
|
#
|
||||||
# host1.client.com:
|
# For example:
|
||||||
# abc: def
|
#
|
||||||
|
# Given following content of the data.yaml file:
|
||||||
|
#
|
||||||
|
# ---
|
||||||
|
# host1.example.com:
|
||||||
# foo: bar
|
# foo: bar
|
||||||
# test: other
|
# baz: quux
|
||||||
# host2.client.com:
|
# question: 42
|
||||||
# abc: abc
|
# host2.example.com:
|
||||||
# foo: baz
|
# abc: def
|
||||||
# test: other2
|
# this: that
|
||||||
|
# darth: vader
|
||||||
|
#
|
||||||
|
# Then calling load_vars in Puppet manifest file as follows:
|
||||||
#
|
#
|
||||||
# load_vars("/etc/puppet/data.yaml", $fqdn)
|
# load_vars("/etc/puppet/data.yaml", $fqdn)
|
||||||
# will try to find matching $fqdn key in data.yaml
|
|
||||||
# and, if found, will add variables $abc $foo and $test
|
|
||||||
#
|
#
|
||||||
|
# Will result in addition of variables $foo, $baz and $question
|
||||||
|
# for matching host name as per the variable $fqdn ...
|
||||||
#
|
#
|
||||||
# EXAMPLE 2:
|
# Another example which uses per-host file:
|
||||||
# data-host1.clent.com.yaml
|
#
|
||||||
# abc: def
|
# 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")
|
# load_vars("/etc/puppet/data-$fqdn.yaml")
|
||||||
# will add variable $abc
|
#
|
||||||
|
# This will result in a variable $foo being added and ready for use.
|
||||||
|
#
|
||||||
|
|
||||||
|
module Puppet::Parser::Functions
|
||||||
|
newfunction(:load_vars, :type => :statement) do |args|
|
||||||
|
data = {}
|
||||||
|
|
||||||
Puppet::Parser::Functions.newfunction :load_vars, :type => :statement do |args|
|
|
||||||
file = args[0]
|
file = args[0]
|
||||||
data = {}
|
key = args[1] if args[1]
|
||||||
if args[1]
|
|
||||||
key = args[1]
|
|
||||||
end
|
|
||||||
|
|
||||||
if FileTest.exist?(file) # file exists
|
if File.exists?(file)
|
||||||
|
|
||||||
|
begin
|
||||||
data = YAML.load_file(file)
|
data = YAML.load_file(file)
|
||||||
raise ArgumentError, "Data in %s is not a hash" % file unless data.is_a?(Hash)
|
rescue => error
|
||||||
# data is a hash for sure
|
raise(Puppet::ParseError,
|
||||||
|
"Unable to load data from the file `%s': %s" % file, error.to_s)
|
||||||
if key
|
|
||||||
# if we have key then use it:
|
|
||||||
if data[key].is_a?(Hash)
|
|
||||||
data = data[key]
|
|
||||||
else
|
|
||||||
data = {}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
raise(Puppet::ParseError,
|
||||||
|
"Data in the file `%s' is not a hash" % file) unless data.is_a?(Hash)
|
||||||
|
|
||||||
|
data = data[key] if key and data[key].is_a?(Hash)
|
||||||
end
|
end
|
||||||
# add values from hash:
|
|
||||||
data.each do |param, value|
|
data.each { |param, value| setvar(param, strinterp(value)) }
|
||||||
setvar(param, strinterp(value))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# vim: set ts=2 sw=2 et
|
||||||
|
|
Loading…
Reference in a new issue