1 | require 'puppet/parser/functions'
|
---|
2 |
|
---|
3 | Puppet::Parser::Functions.newfunction(:ensure_resources,
|
---|
4 | :type => :statement,
|
---|
5 | :doc => <<-'ENDOFDOC'
|
---|
6 | Takes a resource type, title (only hash), and a list of attributes that describe a
|
---|
7 | resource.
|
---|
8 |
|
---|
9 | user { 'dan':
|
---|
10 | gid => 'mygroup',
|
---|
11 | ensure => present,
|
---|
12 | }
|
---|
13 |
|
---|
14 | An hash of resources should be passed in and each will be created with
|
---|
15 | the type and parameters specified if it doesn't already exist.
|
---|
16 |
|
---|
17 | ensure_resources('user', {'dan' => { gid => 'mygroup', uid => '600' } , 'alex' => { gid => 'mygroup' }}, {'ensure' => 'present'})
|
---|
18 |
|
---|
19 | From Hiera Backend:
|
---|
20 |
|
---|
21 | userlist:
|
---|
22 | dan:
|
---|
23 | gid: 'mygroup'
|
---|
24 | uid: '600'
|
---|
25 | alex:
|
---|
26 | gid: 'mygroup'
|
---|
27 |
|
---|
28 | Call:
|
---|
29 | ensure_resources('user', hiera_hash('userlist'), {'ensure' => 'present'})
|
---|
30 |
|
---|
31 | ENDOFDOC
|
---|
32 | ) do |vals|
|
---|
33 | type, title, params = vals
|
---|
34 | raise(ArgumentError, 'Must specify a type') unless type
|
---|
35 | raise(ArgumentError, 'Must specify a title') unless title
|
---|
36 | params ||= {}
|
---|
37 |
|
---|
38 | if title.is_a?(Hash)
|
---|
39 | resource_hash = Hash(title)
|
---|
40 | resources = resource_hash.keys
|
---|
41 |
|
---|
42 | Puppet::Parser::Functions.function(:ensure_resource)
|
---|
43 | resources.each { |resource_name|
|
---|
44 | if resource_hash[resource_name]
|
---|
45 | params_merged = params.merge(resource_hash[resource_name])
|
---|
46 | else
|
---|
47 | params_merged = params
|
---|
48 | end
|
---|
49 | function_ensure_resource([ type, resource_name, params_merged ])
|
---|
50 | }
|
---|
51 | else
|
---|
52 | raise(Puppet::ParseError, 'ensure_resources(): Requires second argument to be a Hash')
|
---|
53 | end
|
---|
54 | end
|
---|