If you are not familiar with modules, check out Ansible - Getting Started with Modules.
There are a few different modules that can be used to modify a file.
AVOID TROUBLE
If the target file does not exist, invoking this playbook will return an error like the sample below.
For this reason, you will almost always want to use the create: true parameter, so that the file is created if it does not exist. Or use the stat module to determine if the file exists, and then used the when parameter to only execute the task when the file exists.
fatal: [server1.example.com]: FAILED! => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "Destination /tmp/foo.txt does not exist !",
"rc": 257
}
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
lineinfile:
path: /tmp/foo.txt
line: "Hello World"
create: true
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: [server1.example.com]
If the file was not changed, the "changed" key should contain a value of false.
{
"out": {
"changed": false
}
Insert After
insertafter: EOF will append new lines to the bottom of the file.
- name: append 'World' to the end of /tmp/foo.txt
lineinfile:
path: /tmp/foo.txt
line: World
insertafter: EOF
create: true
Or, you can use a regular expression. Let's say foo.txt contains the following:
Hello
World
The following will append "example" after the line beginning with Hello.
- name: append 'example' after line beginning with 'Hello'
lineinfile:
path: /tmp/foo.txt
line: example
insertafter: ^Hello
create: true
Running this play should update foo.txt to have the following.
Hello
example
World
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
lineinfile:
path: /tmp/foo.txt
line: World
insertbefore: BOF
create: true
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
lineinfile:
path: /tmp/foo.txt
line: "new line"
state: present
create: true
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
lineinfile:
path: /tmp/foo.txt
regexp: "old line"
line: "new line"
create: true
Append to an existing line
Let's say foo.txt contains "Hello World". Here is how you would append "example" after Hello World.
- lineinfile:
path: /tmp/foo.txt
backrefs: true
regexp: '^(old line)$'
line: '\1 example'
create: true
Remove a line
In this example, regexp and state: absent are used to remove the last line containing "Hello World". If the file contains two or more lines containing "Hello World", only the last line containing "Hello World" will be removed.
- name: remove new line
lineinfile:
path: /tmp/foo.txt
regexp: "Hello World"
state: absent
create: true