Skip to content

Commit

Permalink
allow erb and epp templates
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxmea committed Mar 27, 2024
1 parent 9028ad3 commit 6b98054
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
41 changes: 35 additions & 6 deletions manifests/manage.pp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@
# notify: 'Service[sshd]'
# '/etc/motd':
# ensure: 'file'
# template: 'profile/motd.epp'
# epp:
# template: 'profile/motd.epp'
# context: {}
# '/etc/information':
# ensure: 'file'
# erb:
# template: 'profile/information.erb'
# package:
# example:
# ensure: installed
Expand All @@ -55,18 +61,41 @@
$resources.each |$title, $attributes| {
case $type {
'file': {
if 'template' in $attributes and 'content' in $attributes {
fail("You can not set 'content' and 'template' for file ${title}")
# sanity checks
# epp, erb and content are exclusive
if 'epp' in $attributes and 'content' in $attributes {
fail("You can not set 'epp' and 'content' for file ${title}")
}
if 'erb' in $attributes and 'content' in $attributes {
fail("You can not set 'erb' and 'content' for file ${title}")
}
if 'erb' in $attributes and 'epp' in $attributes {
fail("You can not set 'erb' and 'epp' for file ${title}")
}
if 'template' in $attributes {
$content = epp($attributes['template'])

if 'epp' in $attributes {
if 'template' in $attributes['epp'] {
if 'context' in $attributes['epp'] {
$content = epp($attributes['epp']['template'], $attributes['epp']['context'])
} else {
$content = epp($attributes['epp']['template'])
}
} else {
fail("No template configured for epp for file ${title}")
}
} elsif 'erb' in $attributes {
if 'template' in $attributes['erb'] {
$content = template($attributes['erb']['template'])
} else {
fail("No template configured for erb for file ${title}")
}
} elsif 'content' in $attributes {
$content = $attributes['content']
} else {
$content = undef
}
file { $title:
* => $attributes - 'template' - 'content',
* => $attributes - 'erb' - 'epp' - 'content',
content => $content,
}
}
Expand Down
17 changes: 14 additions & 3 deletions spec/classes/manage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@
'notify' => 'Service[sshd]'
},
'/etc/motd' => {
'template' => 'stdlib/manage_spec.epp'
'epp' => {
'template' => 'profile/motd.epp'
}
},
'/etc/information' => {
'erb' => {
'template' => 'profile/information.erb'
}
}
},
'package' => {
Expand All @@ -41,12 +48,16 @@
end

Puppet::Functions.create_function(:epp) do
return 'I am a template'
return 'I am an epp template'
end
Puppet::Functions.create_function(:template) do
return 'I am an erb template'
end

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

0 comments on commit 6b98054

Please sign in to comment.