Edit single-line text -Ansible module lineinfile
How to edit a single-line text in a file with Ansible module lineinfile.
Today we’re going to talk about how to edit a single-line text in a file with Ansible. Ansible module lineinfile. I’m Luca Berton and welcome to today’s episode of Ansible Pilot
Ansible module lineinfile
Today we’re talking about the Ansible module lineinfile.
The full name is ansible.builtin.lineinfile
, 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 single line of text in a file.
Main Parameters
- path string - file path
- line string - text
- insertafter/insertbefore string - EOF/regular expression
- validate string - validation command
- create boolean - create if not exist
- state string - present/absent
- owner/group/mode - permission
- setype/seuser/selevel - SELinux
This module has some parameters to perform any tasks. The only required is “path”, where you specify the filesystem path of the file you’re going to edit. “line” is the line of 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” or “insertbefore”. If there is any tool to validate the file we could specify it in the validate parameter, very useful for configuration files. If the file does not exist we could also “create” it! Usually, we would like to insert a text line but we could also remove using state in conjunction with parameter absent. Let me also highlight that we could also specify some permissions or SELinux property.
Links
The Best Resources For Ansible
Certifications
- Coursera Pro - Unlimited access to 7,000+ world-class courses, hands-on projects, and job-ready certificate programs—all included in your subscription
Video Course
Printed Book
-
Ansible For VMware by Examples
-
Ansible for Kubernetes by Example
-
Hands-on Ansible Automation
-
Red Hat Ansible Automation Platform
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
- Terraform By Example: A Practical Approach for Beginners to Learn Cloud Infrastructure with Terraform
- Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
- Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
- Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
- Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
- Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
- Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
- Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
- Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
- Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
Are you ready to make your hands dirty? Let’s jump in a live demo of lineinfile module usage in the Ansible playbook.
- lineinfile.yml
---
- name: lineinfile module demo
hosts: all
become: true
tasks:
- name: allow password authentication
ansible.builtin.lineinfile:
state: present
dest: /etc/ssh/sshd_config
regexp: "^PasswordAuthentication"
line: "PasswordAuthentication yes"
validate: 'sshd -t -f %s'
Recap
Now you know better the Ansible module lineinfile and you could use it successfully in your playbook. Subscribe to the YouTube channel, Medium, and Website, X (formerly 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: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate