Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

My end goal here is to include a site-specific snippet of configuration into a common base template. My first thought was to call the template function from within the base template, e.g.:

Manifest:

...
$domain = "www.example.com"
...

Template:

server {
    # Common configuration items here.
    ...

    # Load site-specific configurations here.
    <%= scope.function_template("sites/$domain/config.erb") %>
}

However, it doesn't seem that it's possible to use a variable in the function call (please correct me if I am wrong). There's also the problem that a site-specific template must exist.

So, my next thought was then to use a variable in the manifest containing the site-specific configuration content loaded from a template.

Manifest:

...
$domain = "www.example.com"
$site_specific_content = template("sites/$domain/config.erb")
....

Template:

server {
    # Common configuration items here.
    ...

    # Load site-specific configurations here.
    <%= site_specific_content %>
}

However, I haven't been able to find a way to render the template if it exists, but set the variable to empty string if the template does not exist. (Though a bit inelegant, I guess a workaround here could be to just create an empty site-specific template for the sites that need no additional configuration.)

Any ideas?

share|improve this question
    
Gary, just checking in -- did my answer help out? –  larsks Apr 23 '12 at 16:10

1 Answer 1

up vote 0 down vote accepted

However, it doesn't seem that it's possible to use a variable in the function call (please correct me if I am wrong). There's also the problem that a site-specific template must exist.

It's possible to use a variable in the function call, but remember that your function argument is a ruby string, not a Puppet template. You could do this:

<%= scope.function_template("sites/" + domain + "/config.erb") %>

Or use Ruby's string substitution:

<%= scope.function_template("sites/#{domain}/config.erb") %>

...but this will still produce an error in the event that the defined template does not exist.

To prevent the error, you can use Ruby's exception handling mechanism inside your template, like this:

<%=
  begin
    scope.function_template("sites/#{domain}/config.erb")
  rescue
  end
%>

Ta da! There may be a better way of doing this; I'm neither a Ruby nor a Puppet expert, and I just made this up just now.

share|improve this answer
    
...incidentally, coming up with this solution has just solved a headache I've been working on today. So that's awesome. –  larsks Apr 20 '12 at 21:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.