Ruby 在ERB中的灵活范围内迭代

Ruby 在ERB中的灵活范围内迭代,ruby,puppet,erb,Ruby,Puppet,Erb,我在3.8.1版上有一个傀儡ERB模板,它抛出了一个错误并拒绝工作 这是我的原始代码: <%= (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] != @host

我在3.8.1版上有一个傀儡ERB模板,它抛出了一个错误并拒绝工作

这是我的原始代码:

<%= (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]} %>
<%= (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的语法糖吗

结果表明,Puppet返回的变量是字符串而不是int,尽管我在Puppet中将其声明为int

用石膏和一些手工材料,我让它工作起来:

<%= (Range.new(1, Integer(scope.lookupvar('mymodule::elastic_instances'))) ... %>


这使事情按预期运行。

如果您使用的是PuppetDB,您可能需要查看

使用它,您可以从数据库中检索主机名列表,这样模板就可以像这样迭代它们

<% @elastic_search_hostnames.each do -%>
...
<% end -%>

...

scope.lookupvar('mymodule::elastic_instances')返回的值是多少?如果你想用的话,也许是你的朋友在这里!
<%= (Range.new(1, Integer(scope.lookupvar('mymodule::elastic_instances'))) ... %>
<% @elastic_search_hostnames.each do -%>
...
<% end -%>