summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Doran <sdoran@redhat.com>2019-10-22 10:39:58 -0400
committerGitHub <noreply@github.com>2019-10-22 10:39:58 -0400
commit92cd13a2cff295c7cd5f196c3e30b9db4c9f1c1a (patch)
tree1563ce6d70d48420078252bc2f86eb918fd3367b
parent05c8e33983802cf2f3dd6af6fb2da8a94bc6914d (diff)
downloadansible-92cd13a2cff295c7cd5f196c3e30b9db4c9f1c1a.tar.gz
lineinfile - use correct index value when inserting at the end (#63696)
-rw-r--r--changelogs/fragments/lineinfile-use-correct-index-value.yaml2
-rw-r--r--lib/ansible/modules/files/lineinfile.py10
-rw-r--r--test/integration/targets/lineinfile/tasks/main.yml30
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 14d53aee13..8187f71c5b 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'