From c4876b8890b030a5e2cece7c5bba0bc2e25a2f86 Mon Sep 17 00:00:00 2001 From: Martin Alfke Date: Tue, 26 Mar 2024 09:53:52 +0100 Subject: [PATCH 1/5] Allow usage of file templates with stdlib::manage This one only covers epp templates. --- manifests/manage.pp | 45 +++++++++++++++++++++++++++++-------- spec/classes/manage_spec.rb | 4 ++++ templates/manage_spec.epp | 1 + 3 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 templates/manage_spec.epp diff --git a/manifests/manage.pp b/manifests/manage.pp index 1324187a7..508e91e49 100644 --- a/manifests/manage.pp +++ b/manifests/manage.pp @@ -14,17 +14,21 @@ # # @example # class { 'stdlib::manage': -# 'create_resources' => { -# 'file' => { +# 'create_resources' => { +# 'file' => { # '/etc/motd.d/hello' => { -# 'content' => 'I say Hi', -# 'notify' => 'Service[sshd]', +# 'content' => 'I say Hi', +# 'notify' => 'Service[sshd]', +# }, +# '/etc/motd' => { +# 'ensure' => 'file', +# 'template' => 'profile/motd.epp', # } # }, -# 'package' => { -# 'example' => { -# 'ensure' => 'installed', -# 'subscribe' => ['Service[sshd]', 'Exec[something]'], +# 'package' => { +# 'example' => { +# 'ensure' => 'installed', +# 'subscribe' => ['Service[sshd]', 'Exec[something]'], # } # } # } @@ -35,6 +39,9 @@ # '/etc/motd.d/hello': # content: I say Hi # notify: 'Service[sshd]' +# '/etc/motd': +# ensure: 'file' +# template: 'profile/motd.epp' # package: # example: # ensure: installed @@ -46,7 +53,27 @@ ) { $create_resources.each |$type, $resources| { $resources.each |$title, $attributes| { - create_resources($type, { $title => $attributes }) + case $type { + 'file': { + if 'template' in $attributes and 'content' in $attributes { + fail("You can not set content and template fir file ${title}") + } + if 'template' in $attributes { + $content = epp($attributes['template']) + } elsif 'content' in $attributes { + $content = $attributes['content'] + } else { + $content = undef + } + file { $title: + * => $attributes - 'template' - 'content', + content => $content, + } + } + default: { + create_resources($type, { $title => $attributes }) + } + } } } } diff --git a/spec/classes/manage_spec.rb b/spec/classes/manage_spec.rb index a34fc7296..d6958423f 100644 --- a/spec/classes/manage_spec.rb +++ b/spec/classes/manage_spec.rb @@ -25,6 +25,9 @@ '/etc/motd.d/hello' => { 'content' => 'I say Hi', 'notify' => 'Service[sshd]' + }, + '/etc/motd' => { + 'template' => 'stdlib/manage_spec.epp' } }, 'package' => { @@ -39,6 +42,7 @@ 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_package('example').with_ensure('installed').that_subscribes_to(['Service[sshd]', 'File[/etc/motd.d]']) } end end diff --git a/templates/manage_spec.epp b/templates/manage_spec.epp new file mode 100644 index 000000000..9258d279e --- /dev/null +++ b/templates/manage_spec.epp @@ -0,0 +1 @@ +I am a template From 3031f97c0f0f96e773f4c3837c351938800b7e5d Mon Sep 17 00:00:00 2001 From: Martin Alfke Date: Tue, 26 Mar 2024 12:57:07 +0100 Subject: [PATCH 2/5] replace template with epp function stub fix misspelling in fail function --- manifests/manage.pp | 2 +- spec/classes/manage_spec.rb | 3 +++ templates/manage_spec.epp | 1 - 3 files changed, 4 insertions(+), 2 deletions(-) delete mode 100644 templates/manage_spec.epp diff --git a/manifests/manage.pp b/manifests/manage.pp index 508e91e49..eeea09f60 100644 --- a/manifests/manage.pp +++ b/manifests/manage.pp @@ -56,7 +56,7 @@ case $type { 'file': { if 'template' in $attributes and 'content' in $attributes { - fail("You can not set content and template fir file ${title}") + fail("You can not set 'content' and 'template' for file ${title}") } if 'template' in $attributes { $content = epp($attributes['template']) diff --git a/spec/classes/manage_spec.rb b/spec/classes/manage_spec.rb index d6958423f..75168eb8d 100644 --- a/spec/classes/manage_spec.rb +++ b/spec/classes/manage_spec.rb @@ -39,6 +39,9 @@ } } end + Puppet::Functions.create_function(:'epp') do + return 'I am a 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]') } diff --git a/templates/manage_spec.epp b/templates/manage_spec.epp deleted file mode 100644 index 9258d279e..000000000 --- a/templates/manage_spec.epp +++ /dev/null @@ -1 +0,0 @@ -I am a template From 9028ad38213dee89910e9fc1091f4e4053272ad4 Mon Sep 17 00:00:00 2001 From: Martin Alfke Date: Tue, 26 Mar 2024 13:02:53 +0100 Subject: [PATCH 3/5] rubocop --- spec/classes/manage_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/classes/manage_spec.rb b/spec/classes/manage_spec.rb index 75168eb8d..b1422266f 100644 --- a/spec/classes/manage_spec.rb +++ b/spec/classes/manage_spec.rb @@ -39,7 +39,8 @@ } } end - Puppet::Functions.create_function(:'epp') do + + Puppet::Functions.create_function(:epp) do return 'I am a template' end From 6b9805486778f3bed58be110caa1196064ffb8b9 Mon Sep 17 00:00:00 2001 From: Martin Alfke Date: Wed, 27 Mar 2024 08:50:07 +0100 Subject: [PATCH 4/5] allow erb and epp templates --- manifests/manage.pp | 41 +++++++++++++++++++++++++++++++------ spec/classes/manage_spec.rb | 17 ++++++++++++--- 2 files changed, 49 insertions(+), 9 deletions(-) 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 From bd6f29f8046c22c224cd2d57f87aa423ad0cc143 Mon Sep 17 00:00:00 2001 From: Martin Alfke Date: Wed, 27 Mar 2024 08:55:47 +0100 Subject: [PATCH 5/5] update puppet strings documentation --- manifests/manage.pp | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/manifests/manage.pp b/manifests/manage.pp index be898e4e6..82c39bc8d 100644 --- a/manifests/manage.pp +++ b/manifests/manage.pp @@ -10,28 +10,38 @@ # # @param create_resources # A hash of resources to create -# NOTE: functions, such as `template` or `epp`, are not evaluated. +# NOTE: functions, such as `template` or `epp`, are not directly evaluated +# but processed as Puppet code based on epp and erb hash keys. # # @example # class { 'stdlib::manage': -# 'create_resources' => { -# 'file' => { -# '/etc/motd.d/hello' => { -# 'content' => 'I say Hi', -# 'notify' => 'Service[sshd]', -# }, -# '/etc/motd' => { -# 'ensure' => 'file', -# 'template' => 'profile/motd.epp', +# 'create_resources' => { +# 'file' => { +# '/etc/motd.d/hello' => { +# 'content' => 'I say Hi', +# 'notify' => 'Service[sshd]', +# }, +# '/etc/motd' => { +# 'ensure' => 'file', +# 'epp' => { +# 'template' => 'profile/motd.epp', # } # }, -# 'package' => { -# 'example' => { -# 'ensure' => 'installed', -# 'subscribe' => ['Service[sshd]', 'Exec[something]'], +# '/etc/information' => { +# 'ensure' => 'file', +# 'erb' => { +# 'template' => 'profile/informaiton.erb', # } # } +# }, +# 'package' => { +# 'example' => { +# 'ensure' => 'installed', +# 'subscribe' => ['Service[sshd]', 'Exec[something]'], +# } # } +# } +# } # # @example # stdlib::manage::create_resources: