Properly count newlines appearing at the end of templates after rendering
Fixes #4633
This commit is contained in:
parent
cda3f06117
commit
959a156195
2 changed files with 18 additions and 2 deletions
|
@ -5,3 +5,11 @@ def isprintable(instring):
|
||||||
isprintable = set(instring).issubset(printset)
|
isprintable = set(instring).issubset(printset)
|
||||||
return isprintable
|
return isprintable
|
||||||
|
|
||||||
|
def count_newlines_from_end(str):
|
||||||
|
i = len(str)
|
||||||
|
while i > 0:
|
||||||
|
if str[i-1] != '\n':
|
||||||
|
break
|
||||||
|
i -= 1
|
||||||
|
return len(str) - i
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,8 @@ import pwd
|
||||||
import ast
|
import ast
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
from ansible.utils.string_functions import count_newlines_from_end
|
||||||
|
|
||||||
class Globals(object):
|
class Globals(object):
|
||||||
|
|
||||||
FILTERS = None
|
FILTERS = None
|
||||||
|
@ -495,8 +497,14 @@ def template_from_file(basedir, path, vars):
|
||||||
except jinja2.exceptions.UndefinedError, e:
|
except jinja2.exceptions.UndefinedError, e:
|
||||||
raise errors.AnsibleUndefinedVariable("One or more undefined variables: %s" % str(e))
|
raise errors.AnsibleUndefinedVariable("One or more undefined variables: %s" % str(e))
|
||||||
|
|
||||||
if data.endswith('\n') and not res.endswith('\n'):
|
# The low level calls above do not preserve the newline
|
||||||
res = res + '\n'
|
# 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
|
||||||
|
res_newlines = count_newlines_from_end(res)
|
||||||
|
data_newlines = count_newlines_from_end(data)
|
||||||
|
if data_newlines > res_newlines:
|
||||||
|
res += '\n' * (data_newlines - res_newlines)
|
||||||
|
|
||||||
if isinstance(res, unicode):
|
if isinstance(res, unicode):
|
||||||
# do not try to re-template a unicode string
|
# do not try to re-template a unicode string
|
||||||
|
|
Loading…
Reference in a new issue