summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToshio Kuratomi <toshio@fedoraproject.org>2015-09-02 08:45:58 -0700
committerToshio Kuratomi <toshio@fedoraproject.org>2015-09-02 08:47:20 -0700
commit7ed746ad45fe2f9ffb4166a15948a7fbed611af7 (patch)
tree149e00b710dea2f18de158c949cdb778e3350307
parenta7231c220378554d44a611000bde2af2883fe064 (diff)
downloadansible-7ed746ad45fe2f9ffb4166a15948a7fbed611af7.tar.gz
Fix preserve_trailing_newlines (broken by 7f5080f64ab4a82648cb746990587c1aaff3f61d )
Fix for one half of hte bug reported in #12198
-rw-r--r--lib/ansible/template/__init__.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py
index 624a7c87a6..f805223d7d 100644
--- a/lib/ansible/template/__init__.py
+++ b/lib/ansible/template/__init__.py
@@ -316,6 +316,10 @@ class Templar:
def _do_template(self, data, preserve_trailing_newlines=False, fail_on_undefined=None, overrides=None):
+ # For preserving the number of input newlines in the output (used
+ # later in this method)
+ data_newlines = self._count_newlines_from_end(data)
+
if fail_on_undefined is None:
fail_on_undefined = self._fail_on_undefined_errors
@@ -377,8 +381,15 @@ class Templar:
# characters at the end of the input data, so we use the
# calculate the difference in newlines and append them
# to the resulting output for parity
+ #
+ # jinja2 added a keep_trailing_newline option in 2.7 when
+ # creating an Environment. That would let us make this code
+ # better (remove a single newline if
+ # preserve_trailing_newlines is False). Once we can depend on
+ # that version being present, modify our code to set that when
+ # initializing self.environment and remove a single trailing
+ # newline here if preserve_newlines is False.
res_newlines = self._count_newlines_from_end(res)
- data_newlines = self._count_newlines_from_end(data)
if data_newlines > res_newlines:
res += '\n' * (data_newlines - res_newlines)