error using If/Else conditionals inside templates

1,583 views
Skip to first unread message

David Kramer

unread,
Feb 16, 2011, 3:52:49 AM2/16/11
to puppet...@googlegroups.com
I am running Puppet 2.6.4, and have run into a situation where I would like to use conditional statements inside a template ERB file.  I am trying to deploy apache virtual host configuration files, and have found that certain environments use SSL certificate files from one vendor and other environments use different formatted certificates from another vendor.  I would like to avoid maintaining two versions of virtual host files and would prefer to use a single template file with conditional logic using tags to decide which lines to use.   When I test the syntax of the Template I get Syntax OK.  When I try to run the run the catalog I get the following error:

puppet-agent[6010]: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse template foobar/vhost.conf.erb: Could not find value for 'development' at /etc/puppet/modules/foobar/manifests/vhost-conf.pp:6 on node www.example.com

Here is the snippet from my vhost.erb
.....

  Alias /js     /var/www/html/<%= hostname %>/static/js
  Alias /video  /var/www/html/<%= hostname %>/static/video
  Alias /pdf    /var/www/html/<%= hostname %>/static/pdf

  SSLEngine on

<% if tagged(development) %>

  SSLCertificateFile /etc/pki/tls/certs/<%= hostname %>.pem
  SSLCertificateKeyFile /etc/pki/tls/certs/<%= hostname %>.pem

<% else tagged(testing) %>

  SSLCertificateFile /etc/pki/tls/certs/<%= hostname %>.crt
  SSLCertificateKeyFile /etc/pki/tls/private/<%= hostname %>.key
  SSLCertificateChainFile /etc/pki/tls/certs/<%= hostname %>_bundle.crt

<% end %>

</VirtualHost>


Any thoughts would be greatly appreciated.  Also is it possible to use if else statements as well?

Cheers,

DK

Ben Hughes

unread,
Feb 16, 2011, 6:33:38 PM2/16/11
to puppet...@googlegroups.com
On Wed, Feb 16, 2011 at 12:52:49AM -0800, David Kramer wrote:

> <% if tagged(development) %>

In ERB the syntax is different I believe. The tagged() is in manifest
syntax.

In ERB land, you have access to tags via:

<% tags.each do |tag| -%>

So perhaps something like
<% if tags.include?( "development" ) %>

See http://docs.puppetlabs.com/guides/templating.html "Access to defined
tags and classes"

--
ben

Reply all
Reply to author
Forward
0 new messages