Skip to content

Instantly share code, notes, and snippets.

@romanbarczynski
Created March 2, 2011 18:21
Show Gist options
  • Save romanbarczynski/851408 to your computer and use it in GitHub Desktop.
Save romanbarczynski/851408 to your computer and use it in GitHub Desktop.
# 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.client.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|
data = {}
file = args[0]
key = args[1] if args[1]
if File.exists?(file)
data = YAML.load_file(file)
raise(ArgumentError, "Data in %s is not a hash" % file) unless data.is_a?(Hash)
data = data[key] if key and data[key].is_a?(Hash)
end
data.each { |param, value| setvar(param, strinterp(value)) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment