Ansible

How to Use Ansible Lineinfile

Ansible is a free, open-source automation tool capable of managing one or hundreds of remote hosts. Its main use is managing configurations, deploying remote servers, managing applications and services, and many more. It also works on both Unix and Unix-like systems, as well as windows.

Out of the box, Ansible has many tools and features. One of them is the lineinfile module. This module allows you to manage a single line within a file located on a remote host.

It supports functionalities such as replacing content on a file, updating content within a line, adding content on a line, and more.

This guide will illustrate how to use the Ansible lineinfile module to manage files on remote hosts.

Requirements

To follow along with the concept of this tutorial, ensure you have the following basic requirements:

  1. An ansible control node
  2. A remote host to manage
  3. SSH access to the remote host

Confirm if a specific entry exists

To ensure that a specific entry is present in a file, we can use the lineinfile module and set the stage to present.

Consider the example playbook shown below:

---
- hosts: all
  gather_facts: yes
  become: yes

  tasks:
  - name: check if /etc/hosts contains 127.0.0.1"
    lineinfile:
      path: "
/etc/hosts"
      state: present
      line: "
127.0.0.1"
    check_mode: yes
    register: out

The above example will check if the entry exists in the specified file and add it if it does not exist.

Create a file and add a new line

We can use the lineinfile module to create a file and add a new line to the created file.

Consider the example playbook shown below:

---
- hosts: all
  gather_facts: no
  tasks:
  - name: create file and add line
    lineinfile:
      dest: /home/ubuntu/example.conf
      line: This is a new entry into the file
      state: present
      create: true

In the example playbook above, we use the dest parameter to specify the path of the file.

Next, we use the line parameter to set the line to add to the file. We use this in conjunction with the state: present parameter.

Finally, we set the create parameter to true, which tells Ansible to create the file if it exists.

If you run the playbook twice, it will not perform any action as both the file and the line specified exist.

Add a line before/after an entry

To add a line before or after a particular entry, you can use the insertafter or insertbefore parameters.

Take a look at the example shown below:

---
- hosts: all
  gather_facts: no
  tasks:
  - name: add line before/after
    lineinfile:
      path: /etc/apache2/apache2.conf
      regex: '^ServerRoot '
      insertafter: '^#ServerRoot '
      line: ServerRoot "/etc/apache2"

After the commented-out line, the lineinfile module will add the ServerRoot “/etc/apache2” entry.

Deleting a line

To remove an entry from a file, set the state to absent as shown in the example playbook below:

---
- hosts: all
  gather_facts: no
  tasks:
  - name: delete a line
    lineinfile:
      path: /etc/apache2/apache2.conf
      regex: '^#ServerRoot '
      state: absent

The example above uses a simple regular expression to match the line starting with #ServerRoot.

Commenting out a line

To comment out a line, use the Ansible lineinfile backrefs parameter. Take a look at the example playbook shown below:

---
- hosts: all
  gather_facts: no
  tasks:
  - name: delete a line
    lineinfile:
      path: /etc/apache2/apache2.conf
      line: '#\1'
      regex: '^#Listen 8080 '
      backrefs: yes

In the example above, we use a regular expression to match the line we wish to comment out.

We then use the contents of the matching line and add a commenting character.

Backup file before changing

It is good to ensure you backup a copy of your files before editing them to facilitate file restore in case of errors.

To back up a file using the lineinfile module, we can set the backup option to true.

Consider the example playbook below:

---
- hosts: all
  gather_facts: no
  tasks:
  - name: delete a line
    lineinfile:
      path: /etc/apache2/apache2.conf
      regex: '^#ServerRoot '
      state: absent
      backup: yes

Conclusion

The Ansible lineinfile module is beneficial when modifying configuration files on remote hosts using Ansible playbooks.

Thank you for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list