Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip if insertbefore is using BOF until later in the module #41767

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/ansible/modules/files/lineinfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,

msg = ''
changed = False
# Regexp matched a line in the file
b_linesep = to_bytes(os.linesep, errors='surrogate_or_strict')
# Regexp matched a line in the file
if index[0] != -1:
if backrefs:
b_new_line = m.expand(b_line)
Expand All @@ -303,13 +303,12 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
if not b_new_line.endswith(b_linesep):
b_new_line += b_linesep

# If a regexp is specified and a match is found anywhere in the file, do
# not insert the line before or after.
# If no regexp was given and a line match is found anywhere in the file,
# insert the line appropriately if using insertbefore or insertafter
if regexp is None and m:

# Insert lines
if insertafter and insertafter != 'EOF':

# Ensure there is a line separator after the found string
# at the end of the file.
if b_lines and not b_lines[-1][-1:] in (b('\n'), b('\r')):
Expand All @@ -327,7 +326,7 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
msg = 'line added'
changed = True

elif insertbefore:
elif insertbefore and insertbefore != 'BOF':
# If the line to insert before is at the beginning of the file
# use the appropriate index value.
if index[1] == 0:
Expand Down
19 changes: 14 additions & 5 deletions test/integration/targets/lineinfile/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,27 @@
line: "New line at the beginning"
insertbefore: "BOF"
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
assert:
that:
- result is changed
- "result.msg == 'line added'"
- "result.backup != ''"
- result1 is changed
- result2 is not changed
- result1.msg == 'line added'
- result1.backup != ''

- name: stat the backup file
stat:
path: "{{ result.backup }}"
path: "{{ result1.backup }}"
register: result

- name: assert the backup file matches the previous hash
Expand Down