Let’s explore more about Lineinfile in Ansible. At Bobcares, with our Server Management Services, we can handle your Ansible issues.
Lineinfile in Ansible
The lineinfile module in Ansible changes a specific line in a file. After finding a specific text, it is useful to add a new line, change or replace an existing one, and remove a line from a file. When working with this module, we can use a regular expression to apply search criteria.
In this article, we will look into three things, namely:
- Inserting a line into a file
- Removing a line from a file
- Changing a line in a file
Inserting a line into a file
Using the path/dest parameter, we can specify the path of the file that we need to change. And use the line parameter to specify the line that we need to insert.
In the example below, we will add “Inserting a line in a file” to the file “server.txt.” The EOF will now be with the new line. The line will not combine if it already exists. We can also set the create parameter, which asks the program to create a new file if the asked one is missing. The state’s default value is Present.
E.g
- hosts: loc tasks: - name: Ansible insert lineinfile lineinfile: dest: /home/javaTpoint/remote_server.txt line: Inserting a line in a file. state: present create: yes
Removing a line from a file
We have to set the state parameter to Absent or remove that specific line. The line will not appear anywhere else.
E.g
- hosts: loc tasks: - name: Ansible lineinfile remove the line lineinfile: dest: /home/javaTpoint/remote_server.txt line: Removed lines. state: absent
Changing a line in a file
We need to use the Ansible backrefs parameter and the regexp parameter along with the state as Present. The file will not change if the regexp does not match any lines. If the regexp matches one or more lines, it will replace the last matching line. Regexp’s grouped elements are populated and have the ability to be changed.
E.g
In the below example, we are commenting on a line. The full line is captured line by placing them inside the parenthesis to ‘\1’. The ‘#\1’ replaces the line with ‘#’ followed by what was captured. We can have multiple captures and call them by using ‘\1’, ‘\2’, ‘\3’ etc.
Commenting a line with Ansible lineinfile backrefs
- name: Ansible lineinfile regexp replace the example lineinfile: dest: /etc/ansible/ansible.cfg regexp: '(inventory = /home/fedora/inventory.ini.*)' line: '#\1' backrefs: yes
Uncommenting the line with lineinfile regexp
- name: Ansible lineinfile backrefs example lineinfile: dest: /etc/ansible/ansible.cfg regexp: '#(inventory = /home/fedora/inventory.ini.*)' line: '\1' backrefs: yes
[Looking for a solution to another query? We’re available 24/7.]
Conclusion
In this article, we provide a brief description of the Lineinfile module in Ansible. We also included the steps to insert, remove, and modify a text using lineinfile.
PREVENT YOUR SERVER FROM CRASHING!
Never again lose customers to poor server speed! Let us help you.
Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.
var google_conversion_label = "owonCMyG5nEQ0aD71QM";
0 Comments