Skip to content

Commit

Permalink
Fixes ansible#5679: lineinfile ignores newline in line argument
Browse files Browse the repository at this point in the history
  • Loading branch information
jirutka committed Jan 19, 2014
1 parent e3e6f02 commit 012e3ae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion library/files/lineinfile
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,11 @@ def main():
if ins_bef is None and ins_aft is None:
ins_aft = 'EOF'

present(module, dest, params['regexp'], params['line'],
# Replace the newline character with an actual newline. Don't replace
# escaped \\n, hence sub and not str.replace.
line = re.sub(r'\n', os.linesep, params['line'])

present(module, dest, params['regexp'], line,
ins_aft, ins_bef, create, backup, backrefs)
else:
if params['regexp'] is None and params.get('line', None) is None:
Expand Down
17 changes: 17 additions & 0 deletions test/TestRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,23 @@ def test_lineinfile(self):
result = self._run(*testcase)
assert result['failed']

# insert multiline at the end of the file
testline1 = '#12: The \\n character replaced with'
testline2 = 'an actual newline.'
testcase = ('lineinfile', [
"dest=%s" % sample,
"regexp='^#12: '",
"line='%s\n%s'" % (testline1, testline2)
])
result = self._run(*testcase)
assert result['changed']
assert result['msg'] == 'line added'
artifact = [x.strip() for x in open(sample)]
assert artifact[-2] == testline1
assert artifact[-1] == testline2
assert artifact.count(testline1) == 1
assert artifact.count(testline2) == 1

# cleanup
os.unlink(sample)

Expand Down

0 comments on commit 012e3ae

Please sign in to comment.