Skip to content

Commit 6b98054

Browse files
committed
allow erb and epp templates
1 parent 9028ad3 commit 6b98054

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

manifests/manage.pp

+35-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@
4141
# notify: 'Service[sshd]'
4242
# '/etc/motd':
4343
# ensure: 'file'
44-
# template: 'profile/motd.epp'
44+
# epp:
45+
# template: 'profile/motd.epp'
46+
# context: {}
47+
# '/etc/information':
48+
# ensure: 'file'
49+
# erb:
50+
# template: 'profile/information.erb'
4551
# package:
4652
# example:
4753
# ensure: installed
@@ -55,18 +61,41 @@
5561
$resources.each |$title, $attributes| {
5662
case $type {
5763
'file': {
58-
if 'template' in $attributes and 'content' in $attributes {
59-
fail("You can not set 'content' and 'template' for file ${title}")
64+
# sanity checks
65+
# epp, erb and content are exclusive
66+
if 'epp' in $attributes and 'content' in $attributes {
67+
fail("You can not set 'epp' and 'content' for file ${title}")
68+
}
69+
if 'erb' in $attributes and 'content' in $attributes {
70+
fail("You can not set 'erb' and 'content' for file ${title}")
71+
}
72+
if 'erb' in $attributes and 'epp' in $attributes {
73+
fail("You can not set 'erb' and 'epp' for file ${title}")
6074
}
61-
if 'template' in $attributes {
62-
$content = epp($attributes['template'])
75+
76+
if 'epp' in $attributes {
77+
if 'template' in $attributes['epp'] {
78+
if 'context' in $attributes['epp'] {
79+
$content = epp($attributes['epp']['template'], $attributes['epp']['context'])
80+
} else {
81+
$content = epp($attributes['epp']['template'])
82+
}
83+
} else {
84+
fail("No template configured for epp for file ${title}")
85+
}
86+
} elsif 'erb' in $attributes {
87+
if 'template' in $attributes['erb'] {
88+
$content = template($attributes['erb']['template'])
89+
} else {
90+
fail("No template configured for erb for file ${title}")
91+
}
6392
} elsif 'content' in $attributes {
6493
$content = $attributes['content']
6594
} else {
6695
$content = undef
6796
}
6897
file { $title:
69-
* => $attributes - 'template' - 'content',
98+
* => $attributes - 'erb' - 'epp' - 'content',
7099
content => $content,
71100
}
72101
}

spec/classes/manage_spec.rb

+14-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@
2727
'notify' => 'Service[sshd]'
2828
},
2929
'/etc/motd' => {
30-
'template' => 'stdlib/manage_spec.epp'
30+
'epp' => {
31+
'template' => 'profile/motd.epp'
32+
}
33+
},
34+
'/etc/information' => {
35+
'erb' => {
36+
'template' => 'profile/information.erb'
37+
}
3138
}
3239
},
3340
'package' => {
@@ -41,12 +48,16 @@
4148
end
4249

4350
Puppet::Functions.create_function(:epp) do
44-
return 'I am a template'
51+
return 'I am an epp template'
52+
end
53+
Puppet::Functions.create_function(:template) do
54+
return 'I am an erb template'
4555
end
4656

4757
it { is_expected.to compile }
4858
it { is_expected.to contain_file('/etc/motd.d/hello').with_content('I say Hi').with_notify('Service[sshd]') }
49-
it { is_expected.to contain_file('/etc/motd').with_content(%r{I am a template}) }
59+
it { is_expected.to contain_file('/etc/motd').with_content(%r{I am an epp template}) }
60+
it { is_expected.to contain_file('/etc/information').with_content(%r{I am an erb template}) }
5061
it { is_expected.to contain_package('example').with_ensure('installed').that_subscribes_to(['Service[sshd]', 'File[/etc/motd.d]']) }
5162
end
5263
end

0 commit comments

Comments
 (0)