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 lineinfile Module: Edit Single Lines in Config Files — Video Tutorial

How to edit single lines in files with Ansible lineinfile module. Add, modify, remove lines using regex, manage config files idempotently with examples.

Watch Video

Watch "Ansible lineinfile Module: Edit Single Lines in Config Files" on YouTube

What You'll Learn

Full Tutorial Content

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 - [ansible.builtin.lineinfile](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html) Playbook Are you ready to make your hands dirty? Let's jump in a live Playbook of lineinfile module usage in the Ansible playbook. - lineinfile.yml ```yaml --- - 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' ``` [code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/edit%20single-line%20text) Conclusion Now you know better the Ansible module lineinfile and you could use it successfully in your playbook. Common lineinfile Patterns Add or update a line ```yaml - name: Set timezone in config ansible.builtin.lineinfile: path: /etc/myapp/config.ini regexp: '^timezone=' line: 'timezone=UTC' become: true ``` Add line if not present ```yaml - name: Add DNS server ansible.builtin.lineinfile: path: /etc/resolv.conf line: 'nameserver 8.8.8.8' state: present become: true ``` Remove a line ```yaml - name: Remove old DNS entry ansible.builtin.lineinfile:

About This Tutorial

Read the full written article: Ansible lineinfile Module: Edit Single Lines in Config Files

Topics Covered

Related Video Tutorials