Technology Security Analyst
Ansible Lineinfile is a module of Ansible that is utilized to adjust the specific line in a document. It is helpful to add another line, adjust a line, supplant a line, and eliminate a current line in a record if it tracks down a particular text. We can utilize a customary articulation to apply search conditions while working with the Lineinfile module. It has cool choices that make our occupation more straightforward. For instance, assuming we need to embed a line in a record on various hosts and we are don't know that document is available on the hosts or not, we can utilize 'make' choices and make it 'yes' to stay away from mistakes that we will experience on the off chance that the document doesn't exist on the objective.
Fundamentally, the Lineinfile module takes the document name utilizing the 'way' boundary as it is the necessary choice for this module and rolls out the improvements in a record according to other given boundaries like what activity we need to perform on that document. The module can take a reinforcement before rolling out any improvements assuming we have indicated the 'reinforcement' choice to yes as it is a discretionary boundary, nonetheless, it is prescribed to take a reinforcement of the record utilizing 'reinforcement' so we can get the first document back on the off chance that the progressions made to the record are wrong.
We should find out with regards to different tasks we can perform utilizing the 'Lineinfile' module and how:
We can insert a line in a file using the ‘line in file’ module. By default, It adds the line at the end of the file if does not exist. Here is a playbook for the same:
Code:
---
- name: Testing line in file module
hosts: test_group
tasks:
- name: "Insert a line in a file and if create the file if does not exist"
become: yes
line in the file:
path: /root/testing/test_file2
line: "ansible_master"
state: present
backup: yes
ansible test_group -m shell -a “ls /root/testing”
Explanation: In the above example, we got an error because the file does not exist at the destination and we have not specified the ‘create’ option. Let’s add the ‘create’ option and make it to yes.
Code:
---
- name: Testing line in file module
hosts: test_group
tasks:
- name: "Insert a line in a file and if create the file if does not exist"
become: yes
line in the file:
path: /root/testing/test_file2
line: "ansible_master"
state: present
create: yes
backup: yes
cat lineinfile_insert.yml
Explanation: In the above example, we have added the ‘create’ option and re-ran the playbook and it ran without any error. The below command is used to verify that there was no file present named ‘test_file2’ before running the playbook:
ansible test_group -m shell -a "ls /root/testing"
As referenced over, the 'line in file' module just adds the line toward the finish of the document, of course, be that as it may, we can add a line later and before a particular line utilizing the 'insert after' and 'insert before' choices separately. Here is the preview of the record test document that we will alter.
cat test_file
Below is the playbook to insert line after a specific line: –
---
- name: Testing line in file module
hosts: test_group
tasks:
- name: "Check existence of a String in a file and add the line after it"
become: yes
line in the file:
path: /root/test_file
insertafter: "ServerName"
line: "ansible_client"
first match: yes
state: present
backup: yes
ansible test_group -m shell -a "cat /root/testing"
Explanation: In the above model, we have added the 'ansible_client' line after the line that matches 'ServerName'. Similarly, we can involve the 'insert before' choice instead of the 'insert after' choice to add the line before the particular line on the off chance that it tracks down the match. The model will be shown when we alter the line in the record.
We can supplant a line utilizing the 'line in file' module anyway it will possibly supplant one line in a record if there are numerous lines that match the predetermined customary articulation. 'line in file' module utilizes the 'regexp' choice to match the rules. It replaces the last line that matches the rules of course. We can handle it utilizing the 'first match' choice to supplant the principal line that matches the rules. Ansible has a 'supplant' module to supplant every one of the events in the record. We will utilize a similar test record that is referenced above to see it in real life and assume you have recalled that we have added 'ansible_client' after the line that has 'ServerName' text in it.
Here is the playbook to supplant the line in a record:
Code:
---
- name: Testing line in file module
hosts: test_group
tasks:
- name: "Check existence of a String in a file and replace if it finds the match"
become: yes
line in the file:
path: /root/test_file
regexp: "ansible_client"
line: "ansible_master"
state: present
backup: yes
ansible-playbook lineinfile_replace.yml
Explanation: In the above example, we have replaced the line that matches ‘ansible_client’ text in it with ‘ansible_master’. We can see the difference if we compare the above snapshot with the last snapshot. In the last snapshot, the third line was ‘ansible_client’ that is replaced with ‘ansible_master’ as we see in the above snapshot.
Below command is used to search for ‘ansible_master’ in the ‘test_file’:
ansibletest_group -m shell -a "grep -in ansible_master /root/test_file"
We can search for the lines that match specific criteria using the ‘regexp’ option and remove those lines by changing the ‘state’ option to absent. Here is the playbook for the same:
Code:
---
- name: Testing line in file module
hosts: test_group
tasks:
- name: "Check existence of a String in a file and remove it"
become: yes
line in the file:
path: /root/test_file
regexp: "ansible_client"
state: absent
backup: yes
ansible-playbook lineinfile_remove.yml
Output:
Explanation: In the above example, the playbook searched for ‘ansible_client’ and removed those lines so when we ran the below command to check the ‘ansible_client’ text in the file, we got a non-zero return code as it does not find any text ‘ansible_client’ in the file.
ansible test_group -m shell -a “grep -in asnible_client /root/test_file”
We can look through a line utilizing 'regexp' choice and utilize 'backrests' choice to hold that lines in a default placeholder '\1' and utilize this variable in the 'line' choice of the module to alter the current line. One use case is to remark a line in a design record. Here is the playbook for the equivalent, we are utilizing the 'insert before' choice to embed a line before the adjusted line. The Scenario is we need to add '#' before the line that contains 'ansible_master' in it and adds 'ansible_master.lab.com' before this line. Here is the playbook for the equivalent:
Code:
---
- name: Testing line in file module
hosts: test_group
tasks:
- name: "Check existence of a String in a file and modify it"
become: yes
lineinfile:
path: /root/test_file
regexp: '(ansible_master)'
line: '# \1'
backrefs: yes
firstmatch: yes
state: present
backup: yes
- name: "Check existence of a string in a file and add a line before it"
become: yes
line in the file:
path: /root/test_file
insert before: '# ansible_master'
line: "ansible_master.lab.com"
state: present
backup: yes
ansible test_group -m shell -a "cat /root/test_file"
Output:
Explanation: In the above model, we have looked for the line that contains 'ansible_master'. We have utilized the 'backrests' choice to hold that line in the '\1' placeholder and utilized the 'line' choice to add '#' toward the front of that line. In the second errand of the playbook, the task added another line 'ansible_master.lab.com' before the line, which we recently adjusted. In the above depiction, we can see the distinction before the alteration of the document and after the adjustment of the record.
The 'line in file' module is exceptionally valuable assuming we need to make changes to a setup record on various hosts. It saves us time when we want to embed, adjust, supplant, or eliminate a line in a document on various machines. It has a limit as it just chips away at a solitary line.