ChristianSchaefer
ChristianSchaefer

Reputation: 98

for-loop in ERB-Template for puppet causes syntax-error

I'm trying to include a for-loop on an Array in a template for puppet. I have been following the information at http://docs.puppetlabs.com/guides/templating.html but on the puppet-client it fails saying:

 Could not retrieve catalog from remote server: wrong header line format

which is related to an ERB parsing problem as far as I could find out.

The erb-verification

#erb -P -x -T '-' /etc/puppet/modules/apache2/templates/site-config.erb  | ruby -c
-:22: syntax error, unexpected '.'
 ;  alias.each do |val|
         ^

Any clues welcome. Thanks.

site-config.erb:

# Virtual Host <%= name %>
NameVirtualHost <%= listen_config %>
<VirtualHost <%= listen_config %>>
    ## allgemeine Einstellungen wie für HTTP-<VirtualHost>
    ServerAdmin      <%= admin_mail %>
    ServerName       <%= server_name %>
    DocumentRoot     <%= document_root %>
    CustomLog        <%= custom_log %> 
    ErrorLog         <%= error_log %> 


    # Let Apache httpd serve static web application content
    <% alias.each do |val| -%>
    Alias <%= val %>
    <% end -%>

    ...

init.pp:

...
  define site (
    $ensure = 'present',
    $listen_config = "*:80",
    $admin_mail = "[email protected]",
    $server_name = "example.local",
    $document_root = "/var/www/",
    $custom_log = "/var/log/apache2/$name.log combined",
    $error_log = "/var/log/apache2/error_$name.log",
    $alias = [],
    $tomcat ="false",
    $jk_logfile ="/var/log/apache2/jk_$name.log",
    $jk_mount =["/$name worker1", "/$name/* worker1"],
    $jk_unmount = [],
    $tls = "false",
    $tls_priorities = "",
    $tls_certificate = "/etc/ssl/certs/apache2_$name.crt",
    $tls_key ="/etc/ssl/certs/apache2_$name.key,
    ) {
  case $ensure {
     'present' : {
        file { "/etc/apache2/sites-available/$name":
           owner   => root,
           group   => root,
           mode    => 644,
           content => template("site-config.erb"),
        }
        exec { "/usr/sbin/a2ensite $name":
           unless => "/bin/readlink -e ${apache2_sites}-enabled/$name",
           notify => Exec["reload-apache2"],
        }
     }
     'absent' : {
        exec { "/usr/sbin/a2dissite $name":
           onlyif => "/bin/readlink -e ${apache2_sites}-enabled/$name",
           notify => Exec["reload-apache2"],
           require => Package["apache2"],
        }
     }
     default: { err ( "Unknown ensure value: '$ensure'" ) }
  }
 }
...

Upvotes: 3

Views: 2814

Answers (1)

dbenhur
dbenhur

Reputation: 20408

Alas, alias is a syntactic keyword in Ruby. You can't make or reference a lexical variable named alias:

pry(main)> alias = 1
SyntaxError: unexpected '='
alias = 1
       ^

From the Puppet docs

Referencing Variables

Puppet passes all of the currently set variables (including facts) to templates when they are evaluated. There are several ways to access these variables:

All of the variables visible in the current scope are available as Ruby instance variables — that is, @fqdn, @memoryfree, @operatingsystem, etc. This style of reference works identically to using short (local) variable names in a Puppet manifest: @fqdn is exactly equivalent to $fqdn.

All of the variables visible in the current scope are also available as Ruby local variables — that is, fqdn, memoryfree, operatingsystem, etc., without the prepended @ sign. This style of reference can sometimes cause problems when variable names collide with Ruby method names; it’s generally better to use the @ style.

So, I think your solution is to follow their advice and always use the instance variable form @alias over the lexical variable form.

Upvotes: 2

Related Questions