[backport 2.7] Lineinfile must not insert lines multiples times with insertbefore/insertafter (#50084)
* Change test suite to fit expected behaviour This reverts some changes from ansible/ansible@723daf3 If a line is found in the file, exactly or via regexp matching, it must not be added again. insertafter/insertbefore options are used only when a line is to be inserted, to specify where it must be added. (cherry picked from commit 31c11de2af000972ba13f584e82001a2bc9c062a) * Implement the change in behaviour mentioned in the previous commit (cherry picked from commit a4141cfa2e49973310bba8c0962ef82d66c35f29) * Fix comment to reflect what the code does (cherry picked from commit 150f5cb232f10e38509823cb5864a9fd800b6df5) * Set the correct return message. In these cases, the lines are added, not replaced. (cherry picked from commit 3216c31401d15906e2435422e71dfb96e30678c4) * Add a changelog (cherry picked from commit c39cf6b33269eac35c3c769de41e7c3dea642b68)
This commit is contained in:
parent
913b1b0194
commit
aa10266d55
3 changed files with 54 additions and 23 deletions
|
@ -0,0 +1,3 @@
|
||||||
|
---
|
||||||
|
bugfixes:
|
||||||
|
- "This reverts some changes from commit 723daf3. If a line is found in the file, exactly or via regexp matching, it must not be added again. `insertafter`/`insertbefore` options are used only when a line is to be inserted, to specify where it must be added."
|
|
@ -298,7 +298,7 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
||||||
msg = ''
|
msg = ''
|
||||||
changed = False
|
changed = False
|
||||||
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
|
# Exact line or 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)
|
||||||
|
@ -309,9 +309,9 @@ 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 no regexp was given and a line match is found anywhere in the file,
|
# If no regexp was given and no line match is found anywhere in the file,
|
||||||
# insert the line appropriately if using insertbefore or insertafter
|
# insert the line appropriately if using insertbefore or insertafter
|
||||||
if regexp is None and m:
|
if regexp is None and m is None:
|
||||||
|
|
||||||
# Insert lines
|
# Insert lines
|
||||||
if insertafter and insertafter != 'EOF':
|
if insertafter and insertafter != 'EOF':
|
||||||
|
@ -338,12 +338,12 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
|
||||||
if index[1] <= 0:
|
if index[1] <= 0:
|
||||||
if b_lines[index[1]].rstrip(b('\r\n')) != b_line:
|
if b_lines[index[1]].rstrip(b('\r\n')) != b_line:
|
||||||
b_lines.insert(index[1], b_line + b_linesep)
|
b_lines.insert(index[1], b_line + b_linesep)
|
||||||
msg = 'line replaced'
|
msg = 'line added'
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
elif b_lines[index[1] - 1].rstrip(b('\r\n')) != b_line:
|
elif b_lines[index[1] - 1].rstrip(b('\r\n')) != b_line:
|
||||||
b_lines.insert(index[1], b_line + b_linesep)
|
b_lines.insert(index[1], b_line + b_linesep)
|
||||||
msg = 'line replaced'
|
msg = 'line added'
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
elif b_lines[index[0]] != b_new_line:
|
elif b_lines[index[0]] != b_new_line:
|
||||||
|
|
|
@ -491,6 +491,14 @@
|
||||||
register: _multitest_1
|
register: _multitest_1
|
||||||
with_items: "{{ test_regexp }}"
|
with_items: "{{ test_regexp }}"
|
||||||
|
|
||||||
|
- name: Assert that the line is added once only
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- _multitest_1.results.0 is changed
|
||||||
|
- _multitest_1.results.1 is not changed
|
||||||
|
- _multitest_1.results.2 is not changed
|
||||||
|
- _multitest_1.results.3 is not changed
|
||||||
|
|
||||||
- name: Do the same thing again to check for changes
|
- name: Do the same thing again to check for changes
|
||||||
lineinfile:
|
lineinfile:
|
||||||
path: "{{ output_dir }}/testmultiple.txt"
|
path: "{{ output_dir }}/testmultiple.txt"
|
||||||
|
@ -499,14 +507,13 @@
|
||||||
register: _multitest_2
|
register: _multitest_2
|
||||||
with_items: "{{ test_regexp }}"
|
with_items: "{{ test_regexp }}"
|
||||||
|
|
||||||
- name: Assert that the file was changed the first time but not the second time
|
- name: Assert that the line is not added anymore
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- item.0 is changed
|
- _multitest_2.results.0 is not changed
|
||||||
- item.1 is not changed
|
- _multitest_2.results.1 is not changed
|
||||||
with_together:
|
- _multitest_2.results.2 is not changed
|
||||||
- "{{ _multitest_1.results }}"
|
- _multitest_2.results.3 is not changed
|
||||||
- "{{ _multitest_2.results }}"
|
|
||||||
|
|
||||||
- name: Stat the insertafter file
|
- name: Stat the insertafter file
|
||||||
stat:
|
stat:
|
||||||
|
@ -516,9 +523,23 @@
|
||||||
- name: Assert that the insertafter file matches expected checksum
|
- name: Assert that the insertafter file matches expected checksum
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- result.stat.checksum == '282fedf460b3ed7357667a9c8b457ec67b53b6ea'
|
- result.stat.checksum == 'c6733b6c53ddd0e11e6ba39daa556ef8f4840761'
|
||||||
|
|
||||||
# Test insertbefore
|
# Test insertbefore
|
||||||
|
|
||||||
|
- name: Deploy the testmultiple file
|
||||||
|
copy:
|
||||||
|
src: testmultiple.txt
|
||||||
|
dest: "{{ output_dir }}/testmultiple.txt"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- name: Assert that the testmultiple file was deployed
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- result is changed
|
||||||
|
- result.checksum == '3e0090a34fb641f3c01e9011546ff586260ea0ea'
|
||||||
|
- result.state == 'file'
|
||||||
|
|
||||||
- name: Write the same line to a file inserted before different lines
|
- name: Write the same line to a file inserted before different lines
|
||||||
lineinfile:
|
lineinfile:
|
||||||
path: "{{ output_dir }}/testmultiple.txt"
|
path: "{{ output_dir }}/testmultiple.txt"
|
||||||
|
@ -527,6 +548,14 @@
|
||||||
register: _multitest_3
|
register: _multitest_3
|
||||||
with_items: "{{ test_regexp }}"
|
with_items: "{{ test_regexp }}"
|
||||||
|
|
||||||
|
- name: Assert that the line is added once only
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- _multitest_3.results.0 is changed
|
||||||
|
- _multitest_3.results.1 is not changed
|
||||||
|
- _multitest_3.results.2 is not changed
|
||||||
|
- _multitest_3.results.3 is not changed
|
||||||
|
|
||||||
- name: Do the same thing again to check for changes
|
- name: Do the same thing again to check for changes
|
||||||
lineinfile:
|
lineinfile:
|
||||||
path: "{{ output_dir }}/testmultiple.txt"
|
path: "{{ output_dir }}/testmultiple.txt"
|
||||||
|
@ -535,14 +564,13 @@
|
||||||
register: _multitest_4
|
register: _multitest_4
|
||||||
with_items: "{{ test_regexp }}"
|
with_items: "{{ test_regexp }}"
|
||||||
|
|
||||||
- name: Assert that the file was changed the first time but not the second time
|
- name: Assert that the line is not added anymore
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- item.0 is changed
|
- _multitest_4.results.0 is not changed
|
||||||
- item.1 is not changed
|
- _multitest_4.results.1 is not changed
|
||||||
with_together:
|
- _multitest_4.results.2 is not changed
|
||||||
- "{{ _multitest_3.results }}"
|
- _multitest_4.results.3 is not changed
|
||||||
- "{{ _multitest_4.results }}"
|
|
||||||
|
|
||||||
- name: Stat the insertbefore file
|
- name: Stat the insertbefore file
|
||||||
stat:
|
stat:
|
||||||
|
@ -552,7 +580,7 @@
|
||||||
- name: Assert that the insertbefore file matches expected checksum
|
- name: Assert that the insertbefore file matches expected checksum
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- result.stat.checksum == 'a8452bb3643be8d18ba3fc212632b1633bd9f885'
|
- result.stat.checksum == '5d298651fbc377b45257da10308a9dc2fe1f8be5'
|
||||||
|
|
||||||
###################################################################
|
###################################################################
|
||||||
# Issue 36156
|
# Issue 36156
|
||||||
|
@ -625,7 +653,7 @@
|
||||||
- name: Assert that the file was changed when no regexp was provided
|
- name: Assert that the file was changed when no regexp was provided
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- item is changed
|
- item is not changed
|
||||||
with_items: "{{ _multitest_7.results }}"
|
with_items: "{{ _multitest_7.results }}"
|
||||||
|
|
||||||
- name: Stat the file
|
- name: Stat the file
|
||||||
|
@ -636,7 +664,7 @@
|
||||||
- name: Assert that the file contents match what is expected
|
- name: Assert that the file contents match what is expected
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- result.stat.checksum == '5bf50f3d74afd20de4010ca5c04bc7037b062d30'
|
- result.stat.checksum == '06e2c456e5028dd7bcd0b117b5927a1139458c82'
|
||||||
|
|
||||||
# Test insertbefore
|
# Test insertbefore
|
||||||
- name: Deploy the test.conf file
|
- name: Deploy the test.conf file
|
||||||
|
@ -705,7 +733,7 @@
|
||||||
- name: Assert that the file was changed when no regexp was provided
|
- name: Assert that the file was changed when no regexp was provided
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- item is changed
|
- item is not changed
|
||||||
with_items: "{{ _multitest_10.results }}"
|
with_items: "{{ _multitest_10.results }}"
|
||||||
|
|
||||||
- name: Stat the file
|
- name: Stat the file
|
||||||
|
@ -716,7 +744,7 @@
|
||||||
- name: Assert that the file contents match what is expected
|
- name: Assert that the file contents match what is expected
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
- result.stat.checksum == 'eca8d8ea089d4ea57a3b87d4091599ca8b60dfd2'
|
- result.stat.checksum == 'c3be9438a07c44d4c256cebfcdbca15a15b1db91'
|
||||||
|
|
||||||
- name: Copy empty file to test with insertbefore
|
- name: Copy empty file to test with insertbefore
|
||||||
copy:
|
copy:
|
||||||
|
|
Loading…
Reference in a new issue