I'm trying to write in some logic to a erb template in a chef cookbook. I have the following, which I thought would work. At the moment the attribute there is nil, but it's not skipping the whole block like I thought it would. How do I get the top statement to cause the template reader to skip the whole block?
<% unless node['base']['logstash-forwarder']['nginx'].nil? %>
<%= "{" %>
<%= "\"paths\": [" %>
<% node['base']['logstash-forwarder']['nginx'].each do |path| %>
<% unless path.equal? node['base']['logstash-forwarder']['nginx'].last %>
<%= "\"#{path}\"," %>
<% end %>
<% end %>
<%= "\"#{node['base']['logstash-forwarder']['nginx'].last}\"" %>
<%= " ]," %>
<%= "\"fields\": { \"type\": \"nginx-access\" }" %>
<%= "}" %>
<% end %>
Your exact code above works precisely as expected in ERB, see:
So the problem is either that Chef does something weird (seems unlikely) or your
node
isn't asnil?
as you think it is.Update
Reading between the lines, especially at your
.each
and.last
calls, could it be possible that yournode['base']['logstash-forwarder']['nginx']
is not actuallynil
but rather[]
?If so, change your
.nil?
check to a.empty?