我有一个Puppet模板的3.8.1版本,这是抛出一个错误,并拒绝工作。
这是我的原始代码:
<%= (1..5).select{ |i| "elastic%d" %[i] != @hostname }.map{|x, i| "elastic%d" %[x]} %>这将创建一个不包括当前主机名的主机名数组。
我正在尝试参数化我的模板,这样它就可以根据集群中服务器的数量来很好地缩放事情:
<%= (1..@(scope.lookupvar('mymodule::elastic_instances'))).select{ |i| "elastic%d" %[i] != @hostname }.map{|x, i| "elastic%d" %[x]} %>这会引发以下异常:
Error: Could not run: /home/tk/puppet/modules/mymodule/templates/elasticsearch/elasticsearch-template.conf.erb:329: `@(' is not allowed as an instance variable name
/home/tk/puppet/modules/mymodule/templates/elasticsearch/elasticsearch-template.conf.erb:329: syntax error, unexpected end-of-input
; _erbout.concat(( (1..@(scope.lookupvar('mymodule...我还尝试了以下几种选择:
<%= (1..@(scope.lookupvar('mymodule::elastic_instances'))).select{ |i| "elastic%d" %[i] != @hostname }.map{|x, i| "elastic%d" %[x]} %>
<%= (1..(scope.lookupvar('mymodule::elastic_instances'))).select{ |i| "elastic%d" %[i] != @hostname }.map{|x, i| "elastic%d" %[x]} %>
<%= (1..scope.lookupvar('mymodule::elastic_instances')).select{ |i| "elastic%d" %[i] != @hostname }.map{|x, i| "elastic%d" %[x]} %>有没有一种我可以调用的手动方法来代替Ruby的语法糖呢?
发布于 2015-06-22 23:43:15
事实证明,由Puppet返回的变量是一个字符串,而不是int,尽管我在Puppet中将它声明为int。
用石膏和一些手动的东西,我让它起作用了:
<%= (Range.new(1, Integer(scope.lookupvar('mymodule::elastic_instances'))) ... %>一切都如期而至。
发布于 2015-06-23 09:50:21
如果您使用的是PuppetDB,您可能想看看傀儡数据库-查询模块。
使用它,您可以从DB中检索一个主机名列表,这样模板就可以像
<% @elastic_search_hostnames.each do -%>
...
<% end -%>https://stackoverflow.com/questions/30991469
复制相似问题