Skip to content

Commit c4876b8

Browse files
committed
Allow usage of file templates with stdlib::manage
This one only covers epp templates.
1 parent 76e9ddc commit c4876b8

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

manifests/manage.pp

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,21 @@
1414
#
1515
# @example
1616
# class { 'stdlib::manage':
17-
# 'create_resources' => {
18-
# 'file' => {
17+
# 'create_resources' => {
18+
# 'file' => {
1919
# '/etc/motd.d/hello' => {
20-
# 'content' => 'I say Hi',
21-
# 'notify' => 'Service[sshd]',
20+
# 'content' => 'I say Hi',
21+
# 'notify' => 'Service[sshd]',
22+
# },
23+
# '/etc/motd' => {
24+
# 'ensure' => 'file',
25+
# 'template' => 'profile/motd.epp',
2226
# }
2327
# },
24-
# 'package' => {
25-
# 'example' => {
26-
# 'ensure' => 'installed',
27-
# 'subscribe' => ['Service[sshd]', 'Exec[something]'],
28+
# 'package' => {
29+
# 'example' => {
30+
# 'ensure' => 'installed',
31+
# 'subscribe' => ['Service[sshd]', 'Exec[something]'],
2832
# }
2933
# }
3034
# }
@@ -35,6 +39,9 @@
3539
# '/etc/motd.d/hello':
3640
# content: I say Hi
3741
# notify: 'Service[sshd]'
42+
# '/etc/motd':
43+
# ensure: 'file'
44+
# template: 'profile/motd.epp'
3845
# package:
3946
# example:
4047
# ensure: installed
@@ -46,7 +53,27 @@
4653
) {
4754
$create_resources.each |$type, $resources| {
4855
$resources.each |$title, $attributes| {
49-
create_resources($type, { $title => $attributes })
56+
case $type {
57+
'file': {
58+
if 'template' in $attributes and 'content' in $attributes {
59+
fail("You can not set content and template fir file ${title}")
60+
}
61+
if 'template' in $attributes {
62+
$content = epp($attributes['template'])
63+
} elsif 'content' in $attributes {
64+
$content = $attributes['content']
65+
} else {
66+
$content = undef
67+
}
68+
file { $title:
69+
* => $attributes - 'template' - 'content',
70+
content => $content,
71+
}
72+
}
73+
default: {
74+
create_resources($type, { $title => $attributes })
75+
}
76+
}
5077
}
5178
}
5279
}

spec/classes/manage_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
'/etc/motd.d/hello' => {
2626
'content' => 'I say Hi',
2727
'notify' => 'Service[sshd]'
28+
},
29+
'/etc/motd' => {
30+
'template' => 'stdlib/manage_spec.epp'
2831
}
2932
},
3033
'package' => {
@@ -39,6 +42,7 @@
3942

4043
it { is_expected.to compile }
4144
it { is_expected.to contain_file('/etc/motd.d/hello').with_content('I say Hi').with_notify('Service[sshd]') }
45+
it { is_expected.to contain_file('/etc/motd').with_content(%r{I am a template}) }
4246
it { is_expected.to contain_package('example').with_ensure('installed').that_subscribes_to(['Service[sshd]', 'File[/etc/motd.d]']) }
4347
end
4448
end

templates/manage_spec.epp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I am a template

0 commit comments

Comments
 (0)