Depending on your requirements, you might need to add a line to a file in different positions.
- Start of the file.
- End of the file.
- After a line/pattern.
- Before a line/pattern.
- Append a string to a line (before/after).
Also, there might be other conditions you need to satisfy like,
- Add multiple lines.
- Add command line arguments.
- Edit an existing line.
- Add lines from another file.
- Comment a line.
- Uncomment a line.
Adding a line to the start of a file in Ansible
If you need to add a line and that particular line is NOT present anywhere in the file, then you can use the lineinfile module for this scenario.
In the following example, I am adding the line “Added Line 1” to the top of the file.
insertbefore – This should be set to BOF to make sure the line is added at the beginning. If the pattern already exists it won’t be added. So executing again won’t add more lines.
path – This should be set to the file which you want to change.
line – This is the line which you want to add.
Input.txt --------- Existing line Script ------ - hosts: all tasks: - name: line insert lineinfile: path: /Users/mdtutorials2/Documents/Ansible/Input.txt line: 'Added Line 1' insertbefore: BOF Input.txt after execution ------------------------- Added Line 1 Existing line
Adding a line to the end of a file in Ansible
Adding a line to the end of a file is pretty easy with lineinfile since it is the default behaviour.
For example, to add the line “hey this is easy” to a file as the last line, you can execute the following script.
As you can see I have only added two parameters path and the line. Since the given line/pattern is not present in the file, it is written at the last. But if you run again, the line WON’T be written again.
last.txt -------- first second script ------ - hosts: all tasks: - name: Insert a line at the end of a file. lineinfile: path: /Users/mdtutorials2/Documents/Ansible/line.txt line: last last.txt after execution ------------------------ first second last
Inserting a line after a pattern/line in Ansible
We can also add a line based on a pattern. Suppose you need to add a line to the .bashrc file of the remote server. But, you want to insert the line just after the last occurrence of a pattern.
I am trying to add a new alias to
ll="ls -lhA" on the remote servers’ .bashrc file. I want it to be added just after the last match for ‘alias’ so that every alias commands are organized.
- insertafter – You should add here the regex which you need to check for. In the below script ‘.*’ is not needed. It will work the same way with just ‘alias’.
The best part is you don’t have to worry about the line getting added again during executions since the task is idempotent.
Note: In case the pattern is not found, the line will be added to the last line of the file.
.bashrc ------- #user aliases for common commands alias c='clear' alias grep='grep --color=auto' JAVA_HOME=/usr/lib/jvm/jdk1.7.0 export JAVA_HOME Ansible script -------------- - hosts: all tasks: - name: Inserting a line after a pattern in Ansible example lineinfile: path: ~/.bashrc line: alias ll='ls -lhA' insertafter: alias.* .bashrc after execution ----------------------- #user aliases for common commands alias c='clear' alias grep='grep --color=auto' alias ll='ls -lhA' JAVA_HOME=/usr/lib/jvm/jdk1.7.0 export JAVA_HOME
Inserting a line Before a pattern/line
Adding a line before a pattern in a file in Ansible is similar to the earlier example. Instead of ‘insertafter’, you have to use ‘insertbefore’.
Suppose you want to add more hosts on the remote server. You might want to add the additional host details in the /etc/hosts/ file.
I am going to add the following line just before the host details for ‘domain.com’.
- insertbefore – the regex for finding the line which contains ‘domain.com’ is added.
/etc/hosts ---------- 127.0.0.1 localhost 127.0.1.1 ubuntu 220.127.116.11 domain.com Ansible script -------------- hosts: all tasks: - name: Ansible insert a line before example lineinfile: path: /etc/hosts line: '0.0.0.0 facebook.com' insertbefore: .*domain.com /etc/hosts after execution -------------------------- 127.0.0.1 localhost 127.0.1.1 ubuntu 0.0.0.0 facebook.com 18.104.22.168 domain.com
Adding multiple lines to a file
If you need to add a block of line to a file, then please refer the post on blockinfile.
If your intention is to add multiple lines at different parts of the file, or even different files, but only 1 task, then you can use lineinfile along with the with_items structure. Please refer this post for more details.