summaryrefslogtreecommitdiff
path: root/load_vars.rb
blob: 9befba945cb033837039911551d76bf1838d09bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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