Last Updated on 3 weeks by Sachin G

This article focuses on the Ansible blockinfile module ( ansible.builtin.blockinfile ), which allows users to insert, update, or remove blocks of text within files on remote hosts. Whether you’re adding an SSH configuration, updating NTP servers, or cleaning up deprecated settings, blockinfile ensure your changes are idempotent — meaning they happen only when needed.

It details the module’s parameters, such as path, block, markers, and state, and provides examples demonstrating how to manage file content dynamically. This guide walks you through:

  • How to insert text blocks
  • How to update existing blocks
  • How to remove blocks safely

All with real-world examples and best practices learned from production environments.

Understanding the Ansible Blockinfile Module

The commonly used modules for managing Linux files are included in the ansible.builtin collection and it is part of ansible-core. The blockinfile module can perform 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 .

Common Parameters for blockinfile module

The blockinfile The 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 a timestamp before making 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 the Ansible documentation, where you list all modules through the ansible-doc -l command with the option. The blockinfile module also supports lots of other options. You can read about blockinfile through the command below.

# 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

Ansible blockinfile: some examples

Inserting a Text Block

Scenario: Adding an SSH banner to /etc/ssh/sshd_config.

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 Back-up

Removing a Text Block

Scenario: Removing an old Apache config section. 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 the above snippet code with the state absent.

state: absent

Updating a Text Block

Scenario: Changing NTP servers without duplicating entries.

- name: Update NTP servers
  ansible.builtin.blockinfile:
    path: /etc/ntp.conf
    marker: "# {mark} NTP CONFIG"
    block: |
      server ntp1.example.com
      server ntp2.example.com

How it works: If the markers are found, only the block content changes — no duplicate entries.

Use Unique Markers in Ansible Blockinfile to Prevent Overwrites

Use the markers An 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

Real-World Lessons Learned

From a DevOps engineer’s perspective:

  • Markers are essential — forgetting {mark} can cause repeated insertions.
  • Idempotence matters — test with multiple runs before deploying to production.
  • Version control integration — commit file changes so you can track configuration drift.

FAQ

Q: How do I avoid block duplication in blockinfile?

A: Always set a custom marker with {mark} and unique wording.

Q: Can blockinfile work on Windows hosts?

A: Yes, but paths and line endings differ; test carefully.

Q: Which is better, blockinfile or template?

A: Use template When managing full files; blockinfile It is better for small sections.

Q: Can I insert multiple blocks into the same file?

A: Yes — just use different unique markers for each block.

Q: Does blockinfile preserve file permissions?

A: Yes, it modifies content without altering file ownership or mode.

Q: How do I ensure blockinfile works across multiple OS types?

A: Use when: conditions or separate tasks per OS family.

Want to learn more about Ansible?
Explore our complete Ansible Tutorials for Beginners to master automation step-by-step.
You can also check out our Recommended Ansible Courses for expert-led training and practical projects to boost your skills.