Ansible can be used to deploy software on multiple servers without human intervention. Ansible is also capable of configuring servers and creating user accounts. Ansible is agent-less software, which means there is no need to install the software on the nodes, and you are required to connect the nodes via SSH to do the necessary server activities.
What is Ansible Lineinfile Module?
Ansible's Lineinfile module saves you time while working with files and modifying their content on the fly, such as adding a new line or updating, replacing a line in the file when a specified text is detected, and much more. Ansible Lineinfile has some parameters to make your task fast. You can also mention the condition to match the line that you want to replace and remove using regular expressions. You can use and modify the matched line by specifying the backreference option.
How to Manage Files with Ansible Lineinfile Module
Here is the step-by-step procedure to manage files with the Ansible Lineinfile Module:
Step 1: Install Ansible
First, you have to ensure Ansible is installed and configured, and also need a target system or systems to implement the changes.
pip install ansibleOutput:

Step 2: Check the Ansible version
Then you can check your Ansible version by typing the below command.
ansible --versionOutput:

Step 3: Create Ansible Playbook
A YAML file called an Ansible playbook is where you specify the tasks you wish to run.
touch manage_files.ymlOutput:

Step 4: Make the Playbook Structure
The playbook's basic structure is as follows.
---
- name: Manage files with lineinfile module
hosts: all
become: yes # Use become to gain root privileges if necessary
tasks:
Step 5: Create an Inventory File
Create a hosts.ini inventory file to list the hosts you want to target.
touch hosts.iniOutput:

Step 6: Create a Directory
Creating a new directory follows the easy process, the only difference is that in the state parameter, you enter the directory as the value.
---
- hosts: all
tasks:
- name: Creating a new directory
file:
path: "/your path"
state: directoryb
Step 7: Remove Files
Ansible playbooks can also remove existing files. To do this, change the status parameter to absent.
---
- hosts: all
tasks:
- name: Removing a file
file:
path: "/your path"
state: absent
Step 8: Test the Playbook
Lastly, you have to run the playbook to implement the modifications.
ansible-playbook -i hosts.ini manage_files.ymlOutput:

Best Practices of Ansible Lineinfile Module
- Backup Files: Always run the backup argument first to generate a backup before making any changes.
- Be Careful Matching Lines: As you are writing regular expressions, be careful to color the appropriate lines.
- Idempotency: You should make sure that the Makefile tasks are idempotent. That's just as easy as saying running them several times won't change the file after the first time it is applied unless the source changes.
- Changes: Run your playbooks through a staging environment before deploying them to production systems.
Conclusion
In this article, we have learned about Managing Files with the Ansible Lineinfile Module. Ansible playbooks are simple to write as they are similar to plain English, and Ansible is written in Python. No prior programming knowledge is required. A single Ansible control node can manage thousands of nodes.