Explaining of Ansible lineinfile

Explaining of Ansible  lineinfile
Techiio-author
Written by Nilima PaulJanuary 31, 2022
14 min read
Ansible
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

In this blog post, We will discuss Ansible lineinfile.

Introduction to Ansible lineinfile

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.

How lineinfile Works in Ansible?

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:

1. Inserting a lineinfile

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 lineinfile module
hosts: test_group
tasks:
- name: "Insert a line in a file and if create the file if does not exist"
become: yes
lineinfile:
path: /root/testing/test_file2
line: "ansible_master"
state: present
backup: yes

ansible test_group -m shell -a “ls /root/testing”
blogpost

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 lineinfile module
hosts: test_group
tasks:
- name: "Insert a line in a file and if create the file if does not exist"
become: yes
lineinfile:
path: /root/testing/test_file2
line: "ansible_master"
state: present
create: yes
backup: yes

cat lineinfile_insert.yml

Output:

blogpost

Explanation: In the above example, we have added the ‘create’ option and re-ran the playbook and it ran without any error. 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 'lineinfile' module just adds the line toward the finish of the document of course, be that as it may, we can add line later and before a particular line utilizing the 'insertafter' and 'insertbefore' choices separately. Here is the preview of the record test document that we will alter.

cat test_file

Output:

blogpost

Below is the playbook to insert line after a specific line: –

Code:

---
- name: Testing lineinfile module
hosts: test_group
tasks:
- name: "Check existence of a String in a file and add the line after it"
become: yes
lineinfile:
path: /root/test_file
insertafter: "ServerName"
line: "ansible_client"
firstmatch: yes
state: present
backup: yes

ansible test_group -m shell -a "cat /root/testing"

Output:

blogpost

Explanation: In the above model, we have added 'ansible_client' line after the line that matches 'ServerName'. Similarly, we can involve the 'insertbefore' choice instead of the 'insertafter' choice to add the line before the particular line on the off chance that it tracks down the match. Model will be shown when we alter the line in the record.

2. Replacing the lineinfile

We can supplant a line utilizing the 'lineinfile' module anyway it will possibly supplant one line in a record in the event that there are numerous lines match the predetermined customary articulation. 'lineinfile' 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 'firstmatch' choice to supplant the principal line that matches the rules. Ansible has '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 assuming 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 lineinfile module
hosts: test_group
tasks:
- name: "Check existence of a String in a file and replace if it finds the match"
become: yes
lineinfile:
path: /root/test_file
regexp: "ansible_client"
line: "ansible_master"
state: present
backup: yes

ansible-playbook lineinfile_replace.yml

Output:

blogpost

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"

3. Removing all Lines from a File that Matches the Criteria

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 lineinfile module
hosts: test_group
tasks:
- name: "Check existence of a String in a file and remove it"
become: yes
lineinfile:
path: /root/test_file
regexp: "ansible_client"
state: absent
backup: yes

ansible-playbook lineinfile_remove.yml

Output:

blogpost

Explanation: In the above example, 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 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”

4. Modifying a lineinfile

We can look through a line utilizing 'regexp' choice and utilize 'backrefs' choice to hold that lines in a default placeholder '\1' and utilize this variable in '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 'insertbefore' 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 lineinfile 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 line before it"
become: yes
lineinfile:
path: /root/test_file
insertbefore: '# ansible_master'
line: "ansible_master.lab.com"
state: present
backup: yes

ansible test_group -m shell -a "cat /root/test_file"

Output:

blogpost

Explanation: In the above model, we have looked for the line that contains 'ansible_master'. We have utilized the 'backrefs' 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, task added another line 'ansible_master.lab.com' before the line, 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.

Conclusion

The 'lineinfile' module is exceptionally valuable assuming we need to make changes to a setup record on various hosts. It saves our 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.

Ansible lineinfile
Ansible
Linux
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance