Ansible is a powerful automation tool that can  manage files on remote hosts. The blockinfile module is can be used to insert, update, and remove blocks of text in files. It is a valuable addition to any Ansible playbook. 

About blockinfile Module

The generally commonly used modules for managing Linux files are included in the ansible.builtin collection and it is part of ansible-core. The blockinfile module is can of performing tasks like file creation, copying, editing, and modifying file permissions and other attributes. Most commonly operation of this module is to  add a block of text to an existing file. To use collections you can also install ansible navigator .

blockinfile module Options

The blockinfile module takes a number of arguments, including the following:

  • path: File path , in which you want to update the content.
  • block: Tex block that you want to insert, update, or remove.
  • mode: Permission of the file or directory .
  • owner: Ownership of the file
  • backup : Create a backup file including timestamp before make changes
  • markers: The marker lines that surround the block of text.
  • state: The state of the block. eg: presentabsent, and updated .

The best resource to read about any ansible module  is ansible documentation, where you list all modules through the ansible-doc -l command with option . blockinfile module also support lots of other option also. You can read about blockinfile through the below command.

# ansible-doc  ansible.builtin.blockinfile

For example, the following Ansible example syntax would insert a block of text into the remote file.

- name: Append the some lines to a file
  ansible.builtin.blockinfile:
     path: /file_path
     block: |
         Simple First line in the block of text
         Simple Second line in the block of text
     markers:
         begin: "### BEGIN BLOCK ###"
         end: "### END BLOCK ###"
     backup: yes
     state: present

Some Examples

(1) To insert a block of text in /etc/motd banner file , also used backup option to create a backup before making any changes in motd file. Playbook content is below :

---
- name: Insert Example Text 
  hosts: node1.example.com
  tasks:
   - name: 
     ansible.builtin.blockinfile:
        path: /etc/motd
        block: |
         This server is for authorized users only. 
         Unauthorized access is prohibited.
        backup: yes
        state: present
Blockinfile Insert Text block with BackUp

(2) To remove a block of text from a file, use the state option to specify absent with what block you want to remove.You can use above snippet code with state absent.

state: absent

(3) Use the markers option to specify unique marker lines that will surround the block of text. This will help prevent the block of text from being accidentally removed or overwritten.

---
- name: Marker Example Text 
  hosts: node1.example.com
  tasks:
   - name: 
     ansible.builtin.blockinfile:
        path: /etc/motd
        marker: " ### MARKER BLOCK ###"
        block: |
         This server is for authorized users only. 
         Unauthorized access is prohibited.
        backup: yes
        state: present
MARKER BLOCK

I hope this article was helpful!

By Sachin G

I am a professional freelance contributor and founder of tech transit. Love to write and lover of education, culture, and community. I have been using it, setting, supporting, and maintaining it since 2009.Linux rocks! Sachin G Follow me on LinkedIn and x formerly twitter