2015-06-22 46 views
0

我在V3.8.1上有一個Puppet ERB模板,它拋出一個錯誤並拒絕工作。迭代ERB中的靈活範圍

這裏是我的原代碼:

<%= (1..5).select{ |i| "elastic%d" %[i] != @hostname }.map{|x, i| "elastic%d" %[x]} %> 

這將創建主機名不包括當前主機的主機名的數組。

我想我的參數模板,所以它可以根據我在我的集​​羣服務器的數量比例很好的事情:

<%= ([email protected](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((([email protected](scope.lookupvar('mymodule... 

我也嘗試了以下替代方案:

<%= ([email protected](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的語法糖?

+0

你可以分享價值'scope.lookupvar(「mymodule中:: elastic_instances」 )'回報?如果你想使用它,也許['pry'](https://github.com/pry/pry)就是你的朋友! – vee

回答

2

事實證明,由Puppet返回的變量是一個字符串而不是int,儘管我在Puppet中聲明瞭它爲int。

隨着演員和一些手工的東西,我得到它的工作:

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

那得到的東西如預期工作。

0

如果您使用的是PuppetDB,您可能需要查看puppetdb-query module

利用這一點,你可以檢索數據庫的主機名的列表,從而使模板可以只是重複他們像

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