Ansible Search String in File: lineinfile & regex Guide
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
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.
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
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
string present • remote host • Ansible execution
string different • remote host • Ansible execution
file not present • remote host • Ansible execution
Conclusion Now you know how to Search for a String in a File with Ansible and how you could use successfully in your Playbook.
Search and Replace Line
Check if String Exists
lineinfile Patterns
Regex Search with search Test
Using find Module
Replace Module (Multiple Matches)
lineinfile vs replace
| Feature | lineinfile | replace | |---------|-----------|---------| | Scope | Single line | All matches | | Insert | Yes (after/before) | No | | Ensure line | Yes | No | | Multi-line | No | Yes (with (?s)) | | Remove line | Yes (state: absent) | Replaces with empty |
Validate After Change
FAQ
lineinfile says "changed" but file looks the same?
Check for trailing whitespace differences. Use regexp to match existing line precisely.
How do I handle multi-line blocks?
Use blockinfile for multi-line insertions, or template for complex file management.
Can I search case-insensitively?
Search with lineinfile (check mode)
Search with grep
Search with slurp
Ensure Line Present
Ensure Line Absent
Replace with Regex
Insert After/Before
Search Multiple Patterns
replace Module
blockinfile (Multi-Line)
FAQ
lineinfile vs replace vs blockinfile? • lineinfile — single line operations (ensure present/absent) • replace — regex find/replace across entire file • blockinfile — manage multi-line blocks with markers
How to search without modifying?
Use check_mode: true with lineinfile, or use shell: grep with changed_when: false.
Can I search binary files?
No — use command: strings /path/file | grep pattern for binary files.
Related Articles • Ansible when Conditional Guide • Ansible Inventory Guide • Ansible Become Guide • Ansible for Windows Guide
Category: troubleshooting
Watch the video: Ansible Search String in File: lineinfile & regex Guide — Video Tutorial