Puppet pattern remove last comma

I have the following doll example pattern:

{ "servers" : [ { "port" : 9200, "host" : "localhost", "queries" : [ <% @markets.each do |market| -%> { "outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ], "obj" : "solr/market_<%= market %>:type=queryResultCache,id=org.apache.solr.search.LRUCache", "attr" : [ "hits","hitratio" ] }, <% end -%> ], "numQueryThreads" : 2 } ], } 

Applying it with market = ['UK', 'FR', 'IT'], I get the following:

 { "servers" : [ { "port" : 9200, "host" : "localhost", "queries" : [ { "outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ], "obj" : "solr/market_UK:type=queryResultCache,id=org.apache.solr.search.LRUCache", "attr" : [ "hits","hitratio" ] }, { "outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ], "obj" : "solr/market_FR:type=queryResultCache,id=org.apache.solr.search.LRUCache", "attr" : [ "hits","hitratio" ] }, { "outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ], "obj" : "solr/market_IT:type=queryResultCache,id=org.apache.solr.search.LRUCache", "attr" : [ "hits","hitratio" ] }, ], "numQueryThreads" : 2 } ], } 

The problem is the last comma, which makes it an invalid solr configuration.

Instead of using market.each, I could use market.map and join (','). but how to use the card in this case?

I can use the card as follows:

 <%= @markets.map{ |market| "hello_"+market }.join(',') -%> 

this will print hello_UK,hello_FR,hello_IT (note that we do not have a comma after hello_IT), but I need something like this:

 { "servers" : [ { "port" : 9200, "host" : "localhost", "queries" : [ <% @markets.map |market| -%> { "outputWriters" : [ { "@class" : "com.googlecode.jmxtrans.model.output.StdOutWriter" } ], "obj" : "solr/market_<%= market %>:type=queryResultCache,id=org.apache.solr.search.LRUCache", "attr" : [ "hits","hitratio" ] }, <% }.join(',') -%> ], "numQueryThreads" : 2 } ], } 

this does not work.

so how to make it work? or how to change my puppet pattern to remove the last comma?

+7
ruby puppet erb
source share
2 answers

This is a Ruby problem. Since this is an array, just change .each to .each_with_index. You can then wrap the last comma in the check to see if the value of the current index is less than the size of the index. So,

 <% @markets.each_with_index |market, i| -%> 

and then

 }<%= ',' if i < (@markets.size - 1) %> 
+17
source share

I saw this solution form on the puppet lab website ( https://ask.puppetlabs.com/question/4195/joining-array-from-hieraconcat-other-value-in-erb/ ).

 <%= quorum.map{ |srv| "#{srv}:#{portNum}" }.join(',') %> 

This code allows you to take an array and create a variation of each element, and then combine them together with the corresponding connection text (not including the false comma at the end).

+2
source share

All Articles