HSAR
HSAR

Reputation: 159

How to remove extra whitespace in a ruby template from puppet

I want to put a few values in a file using puppet, but when I do the following in a ruby template:

    <% @_zoo_cfgs.each do |zooconfig| -%>
    <%=zooconfig -%>
    <% end -%>

I get the below content in the generated file:

server.1=a1-dev-mem333.lab.lynx-connected.com:2888:3888(trailing whitespace)

But I want the below content:

server.1=a1-dev-mem333.lab.lynx-connected.com:2888:3888(no trailing whitespace)

with no extra whitespace in the file before or after the value. Could anyone please let me know how could this be done?

Upvotes: 4

Views: 4894

Answers (2)

um-FelixFrank
um-FelixFrank

Reputation: 113

If the whitespace is part of the value from your variable, use Ruby string functions to get rid of it, e.g.

<%= zooconfig.rstrip %>

Upvotes: 0

John Bollinger
John Bollinger

Reputation: 180351

When you close an ERB tag with -%>, any newline immediately following the closing tag is suppressed. If the next character is anything else, however, such as a space character, then there is no suppression. Perhaps more significantly in your case, leading whitespace on the following line of your template is not suppressed under any circumstances.

There are at least two ways to handle the situation.

  • Get rid of the indentation (and ensure no tailing whitespace in the template):
<% @_zoo_cfgs.each do |zooconfig| -%>
<%=zooconfig -%>
<% end -%>
  • Get rid of the iteration altogether, maybe something like this:
<%# assumes that @_zoo_cfgs is an array: -%>
<%= @_zoo_cfgs.join("\n") -%>

Upvotes: 5

Related Questions