AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 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 "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, 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 Search String in File: lineinfile & regex Guide — Video Tutorial
How to search for strings in files with Ansible. Use lineinfile regex, check mode, shell grep, and slurp module to find and manage text patterns.
What You'll Learn
- How to Search for a String in a File with Ansible?
- Ansible module lineinfile
- Parameters
- Links
- Playbook
- code
- string present
- string different
- file not present
- Conclusion
Full Tutorial Content
How to Search for a String in a File with Ansible?
I'm going to show you some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible module lineinfile
> `ansible.builtin.lineinfile`: insert, update and remove a single line of text in a file
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.
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
- `mode`/`owner`/`group` - 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/insertbefore.
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 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 properties.
Links
- [`ansible.builtin.lineinfile`](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html)
Playbook
How to Search for a String in a File.
How to search for a pattern in a file and return the result using only the Ansible built-in lineinfile module.
code
```yaml
---
- name: search Playbook
hosts: all
vars:
myfile: "/etc/ssh/sshd_config"
myline: 'PasswordAuthentication no'
become: true
tasks:
- name: string found
ansible.builtin.lineinfile:
name: "{{ myfile }}"
line: "{{ myline }}"
state: present
check_mode: true
register: conf
failed_when: (conf is changed) or (conf is failed)
```
string present
- remote host
```bash
$ ssh devops@demo.example.com
[devops@demo ~]$ sudo su
[root@demo devops]# grep 'PasswordAuthentication no' /etc/ssh/sshd_config
PasswordAuthentication no
[root@demo devops]#
```
- Ansible execution
```bash
$ ansible-playbook -i virtualmachines/demo/inventory file_management/file_search.yml
PLAY [search Playbook] ********************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.ex
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 7 min
- Category: troubleshooting
Read the full written article: Ansible Search String in File: lineinfile & regex Guide
Related Video Tutorials
- Ansible find Module: Search Files by Extension, Age, Size & Pattern — How to find files with Ansible find module. Search by extension, age, size, pattern, and content. Use results for cleanup, processing, and automation.
- Ansible Delete Files & Directories: file Module state=absent — How to delete files and directories with Ansible file module. Remove files, directories, symlinks, and manage cleanup tasks with examples.
- Add Secondary Groups to Linux Users with Ansible Playbook — Learn how to add secondary groups to Linux users with an Ansible playbook. This step-by-step guide includes YAML configuration and execution details.
- Ansible code in RHSB-2021-009 Log4Shell - Remote Code Execution - log4j (CVE-2021-44228) — Learn how my Ansible Playbook was featured in Red Hat Security Bulletin RHSB-2021-009 to address the Log4Shell vulnerability (CVE-2021-44228). Discover the playbook and its role in mitigating security risks.
- Automate CIS Benchmark Hardening for RHEL 9 with Ansible — Discover how to automate CIS Benchmark hardening for RHEL 9 systems using Ansible. Simplify security compliance with the "ansible-lockdown" project for efficient, effective protection.
- Strengthening Security: Automating CIS Benchmark Hardening for RHEL 9 with Ansible — Automate CIS Benchmark hardening for RHEL 9 using Ansible with the Ansible Lockdown roles, ensuring robust security configurations across systems.