gpt4 book ai didi

ruby - puppet 的 ERB 模板中的 for 循环导致语法错误

转载 作者:数据小太阳 更新时间:2023-10-29 08:04:58 39 4
gpt4 key购买 nike

我正在尝试在 puppet 模板中的数组上包含一个 for 循环。我一直在关注 http://docs.puppetlabs.com/guides/templating.html 上的信息但在 puppet 客户端上它没有说:

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

据我所知,这与 ERB 解析问题有关。

erb 验证

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

欢迎提供任何线索。谢谢。

站点配置.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 -%>

...

初始化.pp:

...
define site (
$ensure = 'present',
$listen_config = "*:80",
$admin_mail = "nobody@example.com",
$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'" ) }
}
}
...

最佳答案

唉,alias 是一个 syntactic keyword在 ruby 中。您不能创建或引用名为 alias 的词法变量:

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

来自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.

因此,我认为您的解决方案是听从他们的建议,并始终使用实例变量形式 @alias 而不是词法变量形式。

关于ruby - puppet 的 ERB 模板中的 for 循环导致语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14382170/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com