diff --git a/manifests/manage.pp b/manifests/manage.pp index eeea09f60..be898e4e6 100644 --- a/manifests/manage.pp +++ b/manifests/manage.pp @@ -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 @@ -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, } } diff --git a/spec/classes/manage_spec.rb b/spec/classes/manage_spec.rb index b1422266f..ce78ec2f0 100644 --- a/spec/classes/manage_spec.rb +++ b/spec/classes/manage_spec.rb @@ -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' => { @@ -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