Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using custom_fragment template with variables from apache::vhost (logroot, servername, ...)? #450

Closed
antaflos opened this issue Nov 12, 2013 · 5 comments

Comments

@antaflos
Copy link
Contributor

I'm afraid my Puppet/Ruby-foo is not strong enough to understand how variable scoping works in case of the custom_fragment parameter in apache::vhost. Using puppetlabs-apache 0.9.0 (plus fix for issue #358, so ceb37bb) on Ubuntu 12.04 with Puppet 3.3.1 and Ruby 1.9.3p429.

It seems the template specified in custom_fragment has no access to variables like @servername or @logroot, set by the apache::vhost defined type. Is this correct, or am I doing it wrong?

Consider this example:

apache::vhost { "worker_app.example.com_{::fqdn}":
  servername      => $::fqdn,
  access_log      => false,
  ssl             => false,
  custom_fragment => template('themodule/vhost_settings.erb')
}

With vhost_settings.erb containing:

...
CustomLog <%= @logroot -%>/<%= @servername -%>_access.log combined env=!from_proxy
...

In this case the vhost_settings.erb template, when applied, looks like this, which is obviously no good:

CustomLog /bar.example.com_access.log combined env=!from_proxy

As you can see the @logroot variable is completely undefined (null, nil, empty, or whatever applies here), while @servername is set to something I don't quite understand, but it is not the servername set in the apache::vhost declaration above. It's probably set to a value from another @servername variable in a previous scope/context, but I have no idea how or why.

So my questions are essentially:

  • Is this the expected behaviour or am I doing anything wrong?
  • If this is the expected behaviour, what is the correct way to access the variables in the declared apache::vhost type from the supplied custom_fragment template?
@antaflos antaflos reopened this Nov 13, 2013
@antaflos
Copy link
Contributor Author

Sorry, misclicked.

@igalic
Copy link
Contributor

igalic commented Nov 13, 2013

The problem with this is that parser functions, such as template() are not only evaluated on the server, but they are evaluated very early in the run. As such the template calls upon variables that are outside of it's scope.

@antaflos
Copy link
Contributor Author

Or worse, it seems, as the called upon variables may contain values from earlier in the run. This is probably why @servername is set to bar.example.com in the example above.

It seems I can work around this by explicitly setting local-scope variables that are then used in the template:

$servername = $::fqdn
$logroot = $::apache::logroot # or a custom path

apache::vhost { "worker_app.example.com_{::fqdn}":
  servername      => $servername,
  logroot         => $logroot,
  access_log      => false,
  ssl             => false,
  custom_fragment => template('themodule/vhost_settings.erb')
}

Not pretty, though.

@igalic
Copy link
Contributor

igalic commented Nov 14, 2013

Do you think you could turn this information into a pull request for the README?

@chelnak
Copy link
Contributor

chelnak commented Mar 8, 2022

Hello! We are doing some house keeping and noticed that this issue has been open for a long time.

We're going to close it but please do raise another issue if the issue still persists. 😄

@chelnak chelnak closed this as completed Mar 8, 2022
traylenator pushed a commit to traylenator/puppetlabs-apache that referenced this issue Jun 7, 2022
…bs#450)

Allows attaching the service to one or multiple networks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants