Skip if insertbefore BOF until later (#41767)

If a line match is found in the file and no regexp is specified, insertbefore would improperly try to add a line if set to BOF.

Add tests for this scenario.
This commit is contained in:
Sam Doran 2018-06-25 16:24:41 -04:00 committed by GitHub
parent 93537425a0
commit eaae1318f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View file

@ -291,8 +291,8 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
msg = '' msg = ''
changed = False changed = False
# Regexp matched a line in the file
b_linesep = to_bytes(os.linesep, errors='surrogate_or_strict') b_linesep = to_bytes(os.linesep, errors='surrogate_or_strict')
# Regexp matched a line in the file
if index[0] != -1: if index[0] != -1:
if backrefs: if backrefs:
b_new_line = m.expand(b_line) b_new_line = m.expand(b_line)
@ -303,13 +303,12 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
if not b_new_line.endswith(b_linesep): if not b_new_line.endswith(b_linesep):
b_new_line += b_linesep b_new_line += b_linesep
# If a regexp is specified and a match is found anywhere in the file, do # If no regexp was given and a line match is found anywhere in the file,
# not insert the line before or after. # insert the line appropriately if using insertbefore or insertafter
if regexp is None and m: if regexp is None and m:
# Insert lines # Insert lines
if insertafter and insertafter != 'EOF': if insertafter and insertafter != 'EOF':
# Ensure there is a line separator after the found string # Ensure there is a line separator after the found string
# at the end of the file. # at the end of the file.
if b_lines and not b_lines[-1][-1:] in (b('\n'), b('\r')): if b_lines and not b_lines[-1][-1:] in (b('\n'), b('\r')):
@ -327,7 +326,7 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
msg = 'line added' msg = 'line added'
changed = True changed = True
elif insertbefore: elif insertbefore and insertbefore != 'BOF':
# If the line to insert before is at the beginning of the file # If the line to insert before is at the beginning of the file
# use the appropriate index value. # use the appropriate index value.
if index[1] == 0: if index[1] == 0:

View file

@ -36,18 +36,27 @@
line: "New line at the beginning" line: "New line at the beginning"
insertbefore: "BOF" insertbefore: "BOF"
backup: yes backup: yes
register: result register: result1
- name: insert a line at the beginning of the file again
lineinfile:
dest: "{{ output_dir }}/test.txt"
state: present
line: "New line at the beginning"
insertbefore: "BOF"
register: result2
- name: assert that the line was inserted at the head of the file - name: assert that the line was inserted at the head of the file
assert: assert:
that: that:
- result is changed - result1 is changed
- "result.msg == 'line added'" - result2 is not changed
- "result.backup != ''" - result1.msg == 'line added'
- result1.backup != ''
- name: stat the backup file - name: stat the backup file
stat: stat:
path: "{{ result.backup }}" path: "{{ result1.backup }}"
register: result register: result
- name: assert the backup file matches the previous hash - name: assert the backup file matches the previous hash