I am looking for a way in Ansible (2.9) to move a line in a text file to the end of the file.
line 1
line 2
line at end of file
line 3
line 4
should become
line 1
line 2
line 3
line 4
line at end of file
The lineinfile module can add a new line to the end, but if the line already exists, it will not move it.
# This does not move an existing line
- name: Move line to end of file
lineinfile:
path: /etc/myfile
line: 'line at end of file'
insertafter: EOF
My current solution is to first remove the line, and then re-add it, but this is of course not idempotent, and causes two changes on each run.