LineInFile

Manipulate lines in a file (add, delete). The file needs to exist before calling this module.

Parameters

ParameterTypeValueDescription
filepathString<path-to-file>Path of the file which will be edited
lineString<Content-of-line>Content of the line to consider
stateStringpresent|absentIs the line expected to be found or not
positionStringtop|bottom|anywhere|<line-number>Where is the line expected (or not) to be found

Examples

---
- name: Edit some files
  steps:
    - name: Add a line at the top
      lineinfile:
        filepath: /path/to/my/file
        line: the first line
        state: present
        position: top

    - name: Add a line at the 2nd place
      lineinfile:
        filepath: /path/to/my/file
        line: 2nd line
        state: present
        position: 2

    - name: Add a line at the bottom
      lineinfile:
        filepath: /path/to/my/file
        line: the last line
        state: present
        position: bottom

    - name: Remove all occurences of a line based on its content
      lineinfile:
        filepath: /path/to/my/file
        line: the content expected not to be present at all
        state: absent