FreeKB - Ansible lineinfile module (edit file)
Ansible - lineinfile module (edit file)

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The lineinfile module can be used to perform a number of modifications to a file, such as:

  • Append to the beginning of the file (BOF)
  • Append to the end of the file (EOF)
  • Update a line


At the bare minimum, the following is all that is needed to invoke this module. By default, text is appended as a new line at the end of the file (EOF).

- name: "append 'Hello World' to /tmp/foo.txt"
    path: "/tmp/foo.txt"
    line: "Hello World"


When the target file does not exist, invoking this playbook will return an error. For this reason, you probably want to first use the stat module to determine if the target file exists, and then use the file module to create the target file, or use the when parameter to skip the lineinfile module if the target file does not exist. Or, you could use the ignore_errors parameter.

TASK [append 'World' to /tmp/foo.txt] 
fatal: []: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "msg": "Destination /tmp/foo.txt does not exist !", "rc": 257}


If the target file exists, and the file was successfully updated, the folllowing should be displayed.

TASK [append 'Hello World' to /tmp/foo.txt]
ok: []


Insert After

insertafter: EOF will append new lines to the bottom of the target file.

- name: "append 'World' to the end of /tmp/foo.txt"
    path: "/tmp/foo.txt"
    line: "World"
    insertafter: "EOF"


Or, you can use a regular expression. Let's say foo.txt contains the following:



The following will append "example" after the line beginning with Hello.

- name: "append 'example' after line beginning with 'Hello'"
    path: "/tmp/foo.txt"
    line: "example"
    insertafter: "^Hello"


Running this play should update foo.txt to have the following.



Beginning of file (BOF)

insertbefore: BOF is used to append to the beginning of the file (BOF).

- name: "append 'World' to the beginning of /tmp/foo.txt"
    path: "/tmp/foo.txt"
    line: "World"
    insertbefore: "BOF"


In this example, state: present is used so that "new line" is appended if the file does not currently contain any lines matching "new line". If there is already a line matching "new line", nothing will be appended.

- name: "append new line EOF if doesn't exist"
    path: "/tmp/foo.txt"
    line: "new line"
    state: "present"


In this example, line is used to replace the last line containing "old line" with "new line". If the file contains two or more lines containing "old line", only the last line containing "old line" will be updated to "new line". For this reason, the replace module is typically used to update a text in a file, as the replace module would replace every line containing "old line" with "new line".

- name: "replace old line with new line"
    path: "/tmp/foo.txt"
    regexp: "old line"
    line: "new line"


In this example, regexp and state: absent are used to replace "new line" with and empty line.

- name: "remove new line"
    path: "/tmp/foo.txt"
    regexp: "new line"
    state: "absent"


Optional Parameters

The lineinfile module accepts the following parameters.

  • owner (e.g. owner: "root")
  • group (e.g. group: "wheel")
  • mode (e.g. mode: "2775")
  • seuser - SELinux user (e.g. seuser: "unconfined_u")
  • serole - SELinux role (e.g. serole: "object_r")
  • setype - SELinux type (e.g. setype: "httpd_sys_content_t")
  • selevel - SELinux level (e.g. selevel: "s0")


Here is an example of how these parameters would be used.

- name: appended new line EOF
    path: "/tmp/foo.txt"
    line: "new line"
    insertafter: "EOF"
    owner: "foo"
    group: "foo"
    mode: "0644"
    seuser: "unconfined_u"
    serole: "object_r"
    setype: "httpd_sys_content_r"
    selevel: "s0"


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter db9d8 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |