AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible blockinfile Module: Insert & Manage Multi-Line Text Blocks (Guide) — Video Tutorial

How to insert and manage multi-line text blocks with Ansible blockinfile module (ansible.builtin.blockinfile). Add config sections, manage markers.

Watch Video

Watch "Ansible blockinfile Module: Insert & Manage Multi-Line Text Blocks (Guide)" on YouTube

What You'll Learn

Full Tutorial Content

Today we’re going to talk about how to edit a multi-line text in a file with Ansible and so much more. Ansible module blockinfile. I’m Luca Berton and welcome to today’s episode of Ansible Pilot Ansible module blockinfile Today we’re talking about Ansible module `blockinfile`. The full name is `ansible.builtin.blockinfile`, which means that is part of the collection of modules “builtin” with ansible and shipped with it. It’s a module pretty stable and out for years and it supports a large variety of operating systems. You are able to insert, update and remove a block of multi-line text in a file. This block is going to be surrounded by customizable marker lines, just to identify that this edit was performed by Ansible. Main Parameters - path _string_ - block _string_ - insertafter/insertbefore _string_ - validate _string_ - create _boolean_ - state _string_ - marker_begin/marker_end _string_ - mode/owner/group - setype/seuser/selevel This module has some parameters to perform any tasks. The only required is “path” parameter, where you specify the filesystem path of the file we’re going to edit. “block” parameter is the text we would like to insert in the file, easy! By default, the text is going to be inserted at the end of the file, but we could personalize it in a specific position with “insertafter”/”insertbefore” parameter. If there is any tool to validate the file we could specify in the validate parameter, very useful for configuration files. If the file does not exist we could create it! Usually, we would like to insert a text block but we could also remove using state in conjunction with parameter absent. Our text is going to be surrounded by some markers, some comments, that show up that we did this edit with Ansible. We could customize the text as well. Let me also highlight that we could also specify some permissions or SELinux property. Demo Are you ready to make your hands dirty? Let’s jump in a live Playbook of blockinfile module usage in the Ansible playbook. ```yaml --- - name: blockinfile module demo hosts: all become: true tasks: - name: Generate /etc/hosts file ansible.builtin.blockinfile: state: present dest: /etc/hosts content: | 192.168.0.200 Playbook demo.example.com ``` [code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/edit%20multi-line%20text) Conclusion Now you know better the Ansible module blockinfile and you could use it successfully in your playbook. Basic Usage Add a block to a file ```yaml - name: Add SSH banner ansible.builtin.blockinfile: path: /etc/ssh/banner.txt block: | ========================================== Authorized access only. All activity is monitored and logged. ========================================== create: true become: true ``` Insert after a specific line ```yaml - name: Add custom hosts ansible.builtin.blockinfile: path: /etc/hosts insertafter: '^127\.0\.

About This Tutorial

Read the full written article: Ansible blockinfile Module: Insert & Manage Multi-Line Text Blocks (Guide)

Topics Covered

Related Video Tutorials