From 7ed746ad45fe2f9ffb4166a15948a7fbed611af7 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Wed, 2 Sep 2015 08:45:58 -0700 Subject: [PATCH] Fix preserve_trailing_newlines (broken by 7f5080f64ab4a82648cb746990587c1aaff3f61d ) Fix for one half of hte bug reported in #12198 --- lib/ansible/template/__init__.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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)