diff options
author | Sam Doran <sdoran@redhat.com> | 2019-11-11 20:29:53 -0500 |
---|---|---|
committer | Matt Davis <nitzmahone@users.noreply.github.com> | 2019-11-11 17:29:52 -0800 |
commit | f861c284d0ac0d1ac5d01def39dc927dbc34bbfb (patch) | |
tree | 71e67cd7cdff596dd5b8aced2a3948ec3f1bddb1 | |
parent | cbe69adf37d02e45f6180f307b36c2b55bf0cef6 (diff) | |
download | ansible-f861c284d0ac0d1ac5d01def39dc927dbc34bbfb.tar.gz |
[stable-2.8] lineinfile - use correct index value when inserting at the end (#63696) (#63795)
(cherry picked from commit 92cd13a2cf)
Co-authored-by: Sam Doran <sdoran@redhat.com>
-rw-r--r-- | changelogs/fragments/lineinfile-use-correct-index-value.yaml | 2 | ||||
-rw-r--r-- | lib/ansible/modules/files/lineinfile.py | 10 | ||||
-rw-r--r-- | test/integration/targets/lineinfile/tasks/main.yml | 30 |
3 files changed, 40 insertions, 2 deletions
diff --git a/changelogs/fragments/lineinfile-use-correct-index-value.yaml b/changelogs/fragments/lineinfile-use-correct-index-value.yaml new file mode 100644 index 0000000000..ef5b412871 --- /dev/null +++ b/changelogs/fragments/lineinfile-use-correct-index-value.yaml @@ -0,0 +1,2 @@ +bugfixes: + - lineinfile - use correct index value when inserting a line at the end of a file (https://github.com/ansible/ansible/issues/63684) diff --git a/lib/ansible/modules/files/lineinfile.py b/lib/ansible/modules/files/lineinfile.py index dbe2b00084..531525eaee 100644 --- a/lib/ansible/modules/files/lineinfile.py +++ b/lib/ansible/modules/files/lineinfile.py @@ -409,8 +409,14 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create, elif insertafter and index[1] != -1: - # Don't insert the line if it already matches at the index - if b_line != b_lines[index[1]].rstrip(b'\n\r'): + # Don't insert the line if it already matches at the index. + # If the line to insert after is at the end of the file use the appropriate index value. + if len(b_lines) == index[1]: + if b_lines[index[1] - 1].rstrip(b'\r\n') != b_line: + b_lines.append(b_line + b_linesep) + msg = 'line added' + changed = True + elif b_line != b_lines[index[1]].rstrip(b'\n\r'): b_lines.insert(index[1], b_line + b_linesep) msg = 'line added' changed = True diff --git a/test/integration/targets/lineinfile/tasks/main.yml b/test/integration/targets/lineinfile/tasks/main.yml index ec6e59e264..819e82da9b 100644 --- a/test/integration/targets/lineinfile/tasks/main.yml +++ b/test/integration/targets/lineinfile/tasks/main.yml @@ -1107,3 +1107,33 @@ - insertbefore_test4_file.stat.checksum == '3c6630b9d44f561ea9ad999be56a7504cadc12f7' - insertbefore_test5 is not changed - insertbefore_test5_file.stat.checksum == '3c6630b9d44f561ea9ad999be56a7504cadc12f7' + + +# Test inserting a line at the end of the file using regexp with insertafter +# https://github.com/ansible/ansible/issues/63684 +- name: Create a file by inserting a line + lineinfile: + path: "{{ output_dir }}/testend.txt" + create: yes + line: testline + register: testend1 + +- name: Insert a line at the end of the file + lineinfile: + path: "{{ output_dir }}/testend.txt" + insertafter: testline + regexp: line at the end + line: line at the end + register: testend2 + +- name: Stat the file + stat: + path: "{{ output_dir }}/testend.txt" + register: testend_file + +- name: Assert inserting at the end gave the expected results. + assert: + that: + - testend1 is changed + - testend2 is changed + - testend_file.stat.checksum == 'ef36116966836ce04f6b249fd1837706acae4e19' |