diff --git a/files/lineinfile.py b/files/lineinfile.py index b9fc628e10c..480811dbbfa 100644 --- a/files/lineinfile.py +++ b/files/lineinfile.py @@ -242,8 +242,11 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, # Don't do backref expansion if not asked. new_line = line - if lines[index[0]] != new_line + os.linesep: - lines[index[0]] = new_line + os.linesep + if not new_line.endswith(os.linesep): + new_line += os.linesep + + if lines[index[0]] != new_line: + lines[index[0]] = new_line msg = 'line replaced' changed = True elif backrefs: