Skip to Content

3 Ways to Modify Files with Ansible: lineinfile, replace, and blockinfile

Ansible is a powerful and flexible automation tool, capable of handling many complex tasks, including modifying files.

Three of the key modules that Ansible provides for file modification are lineinfile, replace, and blockinfile.

  • The lineinfile module is used when you need to manage lines in text files. It can ensure a particular line is present or absent in the file.
  • The replace module is similar to lineinfile, but it operates on complete file content or can replace all instances of a specified pattern within a file.
  • The blockinfile module is used when you want to insert/update/remove a block of lines in a file.

 

The lineinfile module

The lineinfile module is an important module when dealing with text files. It is mainly used when you want to add, remove, or replace a line in a text file.

Here are some examples of how to use the lineinfile module:

  • Example 1: Ensure a particular line is present in a file:
    - name: Ensure the line "AllowUsers ansible" is in the sshd_config file
      ansible.builtin.lineinfile:
        path: /etc/ssh/sshd_config
        line: AllowUsers ansible
    

     

  • Example 2: Remove a line from a file:
    - name: Ensure the line "AllowUsers ansible" is in the sshd_config file
      ansible.builtin.lineinfile:
        path: /etc/ssh/sshd_config
        line: AllowUsers ansible
    

     

  • Example 3: Replace a line in a file:
    - name: Ensure the line "AllowUsers ansible" is in the sshd_config file
      ansible.builtin.lineinfile:
        path: /etc/ssh/sshd_config
        line: AllowUsers ansible
    

     

  • Example 4: Insert a line after a match:
    - name: Ensure the line "AllowUsers ansible" is in the sshd_config file
      ansible.builtin.lineinfile:
        path: /etc/ssh/sshd_config
        line: AllowUsers ansible
    

     

The replace module

The replace module allows for replacing all instances of a pattern within a file. It’s useful when you want to replace all occurrences of a text pattern, not just a single line.

Here are some examples of how to use the replace module:

  • Example 1: Replace all instances of a particular string in a file:
    - name: Ensure the line "AllowUsers ansible" is in the sshd_config file
      ansible.builtin.lineinfile:
        path: /etc/ssh/sshd_config
        line: AllowUsers ansible
    

     

  • Example 2: Replace a pattern using a backreference:
    - name: Ensure the line "AllowUsers ansible" is in the sshd_config file
      ansible.builtin.lineinfile:
        path: /etc/ssh/sshd_config
        line: AllowUsers ansible
    

     

  • Example 3: Replace a pattern using a dictionary of replacements:
    - name: Ensure the line "AllowUsers ansible" is in the sshd_config file
      ansible.builtin.lineinfile:
        path: /etc/ssh/sshd_config
        line: AllowUsers ansible
    

     

  • Example 4: Replace a string in a file, only if a pattern exists in the file:
    - name: Ensure the line "AllowUsers ansible" is in the sshd_config file
      ansible.builtin.lineinfile:
        path: /etc/ssh/sshd_config
        line: AllowUsers ansible
    

     

The blockinfile module

The blockinfile module is very useful when you want to insert/update/remove a block of lines in a file.

Here are some examples of how to use the blockinfile module:

  • Example 1: Insert a block of text:
    - name: Ensure the line "AllowUsers ansible" is in the sshd_config file
      ansible.builtin.lineinfile:
        path: /etc/ssh/sshd_config
        line: AllowUsers ansible
    

     

  • Example 2: Insert a block of text after a marker:
    - name: Ensure the line "AllowUsers ansible" is in the sshd_config file
      ansible.builtin.lineinfile:
        path: /etc/ssh/sshd_config
        line: AllowUsers ansible
    

     

  • Example 3: Remove a block of text:
    - name: Remove block of text
      ansible.builtin.blockinfile:
        path: /path/to/your/file
        state: absent
        marker: "# {mark} ANSIBLE MANAGED BLOCK"
    

     

  • Example 4: Insert a block of text only if it does not already exist:
    - name: Remove block of text
      ansible.builtin.blockinfile:
        path: /path/to/your/file
        state: absent
        marker: "# {mark} ANSIBLE MANAGED BLOCK"
    

     

These are three powerful tools within Ansible that allow for efficient and precise file modification. By understanding and using these modules, you can automate and simplify many file-related tasks in your infrastructure.