How To Use The Ansible Lineinfile Module

February 3, 2024

Silver Imac Displaying Collage Photos
Photo by Designecologist on Pexels.

Introduction

The Ansible Lineinfile module is a powerful tool that allows you to make targeted changes to specific lines in a file without replacing the entire file. This can be incredibly useful for configuration management and automation tasks.

Understanding the Lineinfile Module

Before diving into how to use the Lineinfile module, it's important to understand its purpose and capabilities. The Lineinfile module is designed to modify existing files, unlike other file-related Ansible modules that create files from scratch or delete existing ones. It uses regex patterns to identify the lines to modify and allows you to perform different actions based on the match.

Installing Ansible

Before you can start using the Lineinfile module, you need to have Ansible installed on your system. Ansible is an open-source automation tool that allows you to automate various IT infrastructure tasks.

To install Ansible, you can follow the official documentation's installation guide, which provides detailed instructions for different operating systems.

Syntax

The Lineinfile module follows a specific syntax that allows you to specify the target file, the line to search for, and the changes to apply. Here's an example of the syntax:

- name: Modify a line in a file
  lineinfile:
    path: /path/to/file
    regexp: '^(line_to_match)'
    line: '\1 line_to_append'
    state: present

Let's break down each parameter:

Usage Examples

Appending a Line

One common use case for the Lineinfile module is appending a line to a specific file. Let's say you want to add a new line to the end of the /etc/hosts file on a group of servers. Here's how you can achieve that:

- name: Append a line to hosts file
  lineinfile:
    path: /etc/hosts
    line: '192.168.1.1  server1'
    state: present

This example appends the line 192.168.1.1 server1 to the /etc/hosts file. If the line already exists in the file, it won't be added again.

Replacing a Line

Another use case for the Lineinfile module is replacing a specific line in a file. Let's say you want to replace the line old_line with new_line in the file /path/to/file. Here's how you can do it:

- name: Replace a line in a file
  lineinfile:
    path: /path/to/file
    regexp: '^old_line'
    line: 'new_line'

This example replaces the line that starts with old_line with new_line in the specified file.

Deleting a Line

If you want to remove a specific line from a file, you can use the absent state. Here's an example:

- name: Delete a line from a file
  lineinfile:
    path: /path/to/file
    regexp: '^line_to_delete'
    state: absent

This example removes the line that matches the line_to_delete regular expression pattern from the file.

Conclusion

The Ansible Lineinfile module is a valuable asset in your configuration management and automation toolkit. It allows you to make targeted changes to specific lines in files, giving you fine-grained control over your infrastructure. By following the syntax and examples provided in this guide, you can easily leverage the Lineinfile module to streamline your workflows and increase efficiency.

Remember to always test your changes in a test environment before applying them to production systems.