Ansible Pilot

Edit multi-line text - Ansible module blockinfile

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

Deep dive into the Ansible module blockinfile of the collection ansible. builtin to edit multi-line text with Ansible. Live demo and Ansible playbook included.

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

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 demo of blockinfile module usage in the Ansible playbook.

---
- 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 demo demo.example.com

code with ❤️ in GitHub

Recap

Now you know better the Ansible module blockinfile and you could use it successfully in your playbook. Subscribe to the YouTube channel, Medium, Website and Twitter to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 100+ Automation Examples For Linux and Windows System Administrator and DevOps

Access the Complete Book and Learn Quick Ansible by 200+ Examples

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Trustpilot
Follow me

Subscribe not to miss any new releases

August 1, 2021

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress