summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Barczyński <romke@estrefa.pl>2011-03-03 02:48:35 +0100
committerRoman Barczyński <romke@estrefa.pl>2011-03-03 02:48:35 +0100
commitb305bbeac7a0560a271f34026f936b88b88da477 (patch)
tree5a194646458efb7cc2f7a49cdfcba190568b91cd
initial version
-rw-r--r--load_vars.rb56
1 files changed, 56 insertions, 0 deletions
diff --git a/load_vars.rb b/load_vars.rb
new file mode 100644
index 0000000..9befba9
--- /dev/null
+++ b/load_vars.rb
@@ -0,0 +1,56 @@
+# vim: set ts=2 sw=2 et :
+#
+# load_data loads varibles from external yaml file.
+#
+# EXAMPLE 1:
+# data.yaml:
+# --
+# host1.client.com:
+# abc: def
+# foo: bar
+# test: other
+# host2.client.com:
+# abc: abc
+# foo: baz
+# test: other2
+#
+# 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
+#
+#
+# EXAMPLE 2:
+# data-host1.clent.com.yaml
+# abc: def
+#
+# load_vars("/etc/puppet/data-$fqdn.yaml")
+# will add variable $abc
+
+Puppet::Parser::Functions.newfunction :load_vars, :type => :statement do |args|
+ file = args[0]
+ data = {}
+ if args[1]
+ key = args[1]
+ end
+
+ if FileTest.exist?(file) # file exists
+
+ data = YAML.load_file(file)
+ raise ArgumentError, "Data in %s is not a hash" % file unless data.is_a?(Hash)
+ # data is a hash for sure
+
+ if key
+ # if we have key then use it:
+ if data[key].is_a?(Hash)
+ data = data[key]
+ else
+ data = {}
+ end
+ end
+
+ end
+ # add values from hash:
+ data.each do |param, value|
+ setvar(param, strinterp(value))
+ end
+end