]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
initial version
authorRoman Barczyński <romke@estrefa.pl>
Thu, 3 Mar 2011 01:48:35 +0000 (02:48 +0100)
committerRoman Barczyński <romke@estrefa.pl>
Thu, 3 Mar 2011 01:48:35 +0000 (02:48 +0100)
load_vars.rb [new file with mode: 0644]

diff --git a/load_vars.rb b/load_vars.rb
new file mode 100644 (file)
index 0000000..9befba9
--- /dev/null
@@ -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