Chef templates and how to do a for loop in ruby

chef-infra, ruby

Solution

Recipe

template "/etc/nginx/sites-available/my-site.conf" do
  variables :frontends_count => node["cpu"]["total"]
end

Template

upstream frontends {
<% @frontends_count.times do |i| %>
  server 127.0.0.1:805<%= i + 1 %>;
<% end %>
}

Problem

I am very new to ruby and chef. I am trying to create entries in an nginx.conf file based on the number of cores. ``` for i in <%= node["cpu"]["total"]%> upstream frontends { server 127.0.0.1:805x; } end ``` So..if 4 cores the file will look like this: ``` upstream frontends { server 127.0.0.1:8051; server 127.0.0.1:8052; server 127.0.0.1:8053; server 127.0.0.1:8054; } ```

Original source