Add content parameter to httpd::vhost

Evaluating the template from the vhost defined type rather than the
module where it originates causes problems when dereferencing the
variables in the ERB file. If they are not accessed via the internal
scope object, they can't be found when using puppet 4. The scope object
is also useless when the variables are defined in a defined type and not
a class.

This patch adds a new parameter, $content, which overrides the $template
parameter. If provided, $content indicates the literal string content
for the vhost, as opposed to a reference to a template that needs to be
rendered. This can be used like this:

  $content = template('example/example.vhost.erb')
  httpd::vhost { 'vhostname':
    content => $content,
    priority => 50
  }

This way the template is evaluated when the template() function is
called and has access to variables in that scope.

Change-Id: Ibe3c609d92f3321f43f4794062a64b119b07a1d0
This commit is contained in:
Colleen Murphy 2018-05-28 14:58:02 -07:00
parent 9e47c78140
commit e5996fd275
1 changed files with 9 additions and 1 deletions

View File

@ -10,6 +10,8 @@
# a firewall should be configured. # a firewall should be configured.
# - The $template option specifies whether to use the default template or # - The $template option specifies whether to use the default template or
# override # override
# - The $content option specifies the exact content of the vhost file;
# overrides the template parameter
# - The $priority of the site # - The $priority of the site
# - The $serveraliases of the site # - The $serveraliases of the site
# - The $options for the given vhost # - The $options for the given vhost
@ -41,6 +43,7 @@ define httpd::vhost(
$servername = $httpd::params::servername, $servername = $httpd::params::servername,
$ssl = $httpd::params::ssl, $ssl = $httpd::params::ssl,
$template = $httpd::params::template, $template = $httpd::params::template,
$content = undef,
$vhost_name = $httpd::params::vhost_name, $vhost_name = $httpd::params::vhost_name,
) { ) {
@ -87,9 +90,14 @@ define httpd::vhost(
} }
} }
if $content != undef {
$_content = $content
} else {
$_content = template($template)
}
file { "${priority}-${name}.conf": file { "${priority}-${name}.conf":
path => "${httpd::params::vdir}/${priority}-${name}.conf", path => "${httpd::params::vdir}/${priority}-${name}.conf",
content => template($template), content => $_content,
owner => 'root', owner => 'root',
group => 'root', group => 'root',
mode => '0755', mode => '0755',