How to Use the Ansible lineinfile Module to Manage Text Files

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

Ansible is an IT automation and orchestration tool that uses SSH to configure machines. This module allows you to manage text files using Ansible, including patterns such as adding, deleting lines or merging two files.

Ansible is a software platform that allows for the automation of tasks. The “ansible lineinfile multiple lines” module allows users to manage text files with ease.

Ansible is a popular automation tool for managing hundreds of nodes at once. The Ansible lineinfile module allows you to manage a single line inside a file on distant nodes, which is one of the many amazing advantages of Ansible.

The Ansible lineinfile module performs different operations on a single line in a file, such as replacing, updating, or adding a line.

You’ll discover what the Ansible lineinfile module is, how it works, and how to use it to handle text files in this article.

Prerequisites

This guide will walk you through the Ansible lineinfile module step by step. If you want to join in, make sure you have the following items:

Related: Ansible Setup Guide (Ubuntu, RHEL, CentOS, macOS)

  • To execute commands, you’ll need a remote computer. An inventory file must be created, and one or more hosts must be configured to perform Ansible commands and playbooks. The example will utilize an inventory group named web and a remote Linux workstation called myserver.
  • If you want to follow along with the guide precisely, Apache should already be installed on the remote machine.

How to Configure an Apache Docker Container

Using the Ansible lineinfile module to change a text file.

Let’s start by utilizing the ad hoc commands to launch the Ansible lineinfile module. Ad hoc commands allow you to quickly test or execute a single command on a remote system.

Run the following command from your Ansible controller. This command connects to the web machine using the lineinfile module (-m) and passes an argument (-a) that is the command to run.

In this case, the lineinfile module modifies the localhost entry in the /etc/hosts file by mapping IP-address 127.0.0.1 to myapache. You may browse to the apache test page locally on HTTP://myapache:80 by mapping 127.0.0.1 with myapache.

  • The file’s location is indicated by path.
  • If there is a regular expression in the file, regexp detects it and replaces it with 127.0.0.1 myapache, which is supplied in the line argument.
  • The –become option tells the command to execute as a privileged user.
  • The web is a collection of all servers in the inventory group.
  • The module name is ansible.builtin.lineinfile or simply lineinfile.

web -m ansible path=/etc/hosts regexp=’127.0.0.1′ line=’127.0.0.1 myapache’ state=present ansible.builtin.lineinfile -a

You should receive a CHANGED message after running the command, indicating that the line has been successfully modified on the remote computer.

Using the ansible lineinfile module to run the ad hoc command Using the ansible lineinfile module to run the ad hoc command

Using an SSH client, connect to the remote node and use the cat command to see whether the /etc/hosts file has been changed with the new value.

The localhost entry on the remote system has been successfully changed with 127.0.0.1 myapache, as seen below.

On a distant workstation, verifying the host file On a distant workstation, verifying the host file

Within a Playbook, Modifying Multiple Text Files

Working with a single ad hoc command to manage lines on a distant system may be possible, however managing lines in several files or multiple lines in a file will be tough. Instead of utilizing ad-hoc commands, use the ansible-playbook command to use the Ansible lineinfile module inside the playbook.

Ansible Windows Playbooks: Creating and Running

Now we’ll see how to alter a few lines in the playbook using the Ansible lineinfile module.

Assuming you’ve already signed in to the Ansible controller host, follow these steps:

1. In your home directory, create an ansible lineinfile module demo directory. The playbook you’ll use to call the lineinfile module will be in this directory.

/ansible lineinfile module demo/mkdir cd /ansible lineinfile module demo/ansible lineinfile module demo

2. In the /ansible lineinfile module demo directory, create a new file named my playbook.yml and paste the contents of the following YAML playbook into it. The Ansible lineinefile module is used in numerous tasks in this playbook to handle lines of various config files for Apache on the remote system.

The following tasks are included in the playbook:

1. Verifies that ADMIN is listed in the /etc/sudoers file; if it isn’t, the job installs it.

2. Checks that the Apache default port is 8080 in the /etc/apache2/ports.conf file; if any other port is found, the lineinfile module changes it to 8080. It also changes the MaxKeepAliveRequests to 1000 and the KeepAliveTimeout to 100 in the /etc/apache2/apache2.conf file.

3. Adds a line to the end of the index.html page on the remote server’s /var/www/html directory with the words “Hello This is my Apache Page.”

YAML is used to write Ansible playbooks. Click here to learn more about YAML.

—- – example of the Ansible lineinfile module # Specifying the remote server on which the Ansible lineinfile module will run hosts: web remote user: ubuntu # Using Ubuntu as a remote host: genuine tasks: # (Task-1) Checking the Sudoers file to see whether Admins have full permissions. – name: Before saving ansible, validate the sudoers file. path: /etc/sudoers/builtin.lineinfile current state regexp: ‘ line: ‘% ADMIN ALL=’ (ALL) ALL’ # NOPASSWD: (Task-2) Set the Apache Default Port to 8080 – name: Set the Apache Default Port to 8080 ansible. /etc/apache2/ports.conf builtin.lineinfile regexp: ‘ ‘ Insert after this: ‘ Line: #Listen Listen 8080 # Adding the Line at the End of the HTML Page (Task-3) Hello, This is my Apache Page – name: If the file does not exist, add a line to it ansible.builtin.lineinfile: path: /var/www/html/index.html create: yes # Hello, This is my Apache Page (Task-4) Checking the Sudoers file to see whether Admins have access to all operations – name: Make sure that MaxKeepAliveRequests is bigger than 100. /etc/apache2/apache2.conf regexp: ‘builtin.lineinfile’ Line for MaxKeepAliveRequests: MaxKeepAliveRequests=1000 (Task-5) Checking the Sudoers file to see whether Admins are permitted to do anything – name: Make sure KeepAliveTimeout is larger than 50 ansible. ‘KeepAliveTimeout’ line: KeepAliveTimeout=100 builtin.lineinfile: path: /etc/apache2/apache2.conf regexp:

3. Now, use the ansible-playbook command to run the playbook and add or update all of the lines described in the playbook on the remote host.

my playbook.yml ansible-playbook

The use of the ansible playbook The use of the ansible playbook

The TASK has a changed status, which means the remote host wasn’t in the right condition and has to be adjusted to perform the command. That TASK has an OK status, indicating that no adjustments are required.

4. Next, use your chosen SSH client to connect to the remote computer.

5. Finally, use the cat command to see whether all of the lines described in my playbook.yml have been modified or added on the remote host.

# To see whether admin has full rights and, if not, to add it. /etc/sudoers/cat # Check that the MaxKeepAliveRequests and KeepAliveTimeout values are 1000 and 100, respectively. grep Alive | cat /etc/apache2/apache.config # Check that Apache is listening on port 8080. /etc/apache2/ports.config (cat)

The image below shows that the admin has been added to the sudoers file.

The sudoers file is being checked. The sudoers file is being checked.

The image below verifies that Apache is by default listening on Port 8080.

Checking the ports in the configuration file Checking the ports in the configuration file

Finally, double-check that the MaxKeepAliveRequests and KeepAliveTimeout parameters have been set to 1000 and 100, respectively.

In the config file, check the MaxKeepAliveRequests and KeepAliveTimeout values. In the config file, check the MaxKeepAliveRequests and KeepAliveTimeout values.

Conclusion

The lineinfile module in Ansible is a fantastic method to edit text files on remote systems. Within your playbooks, the module offers an excellent method to add, delete, and alter lines in text files.

What additional scenarios do you think the Ansible lineinfile module may help with?

The “ansible remove line from file” is a module that can be used to remove text from files. To use the module, type in the following command: ansible –lineinfile path/to/file

Related Tags

  • ansible replace line in file
  • ansible replace string in file
  • ansible remove multiple lines from file
  • ansible append line to file
  • ansible replace module