Ansible replace Module: Find & Replace Text in Files (Complete Guide)
By Luca Berton · Published 2024-01-01 · Category: database-automation
How to use the Ansible replace module (ansible.builtin.replace) to find and replace text in files using regex. Update config files, change settings, multi-line replacements with examples.
Ansible replace Module: Find & Replace Text in Files (Complete Guide)
The Ansible replace module (ansible.builtin.replace) performs regex-based find-and-replace operations on files. It's the go-to module for updating configuration values, modifying settings, and performing bulk text changes across your fleet.
Basic Usage
Key Parameters
| Parameter | Description | Required | |-----------|-------------|----------| | path | File to modify | ✅ | | regexp | Regex pattern to find | ✅ | | replace | Replacement text | ❌ (empty = delete match) | | backup | Create backup before modifying | ❌ | | before | Only replace before this pattern | ❌ | | after | Only replace after this pattern | ❌ | | owner | File owner after modification | ❌ | | group | File group after modification | ❌ | | mode | File permissions | ❌ | | validate | Validation command before placing | ❌ | | encoding | File encoding | ❌ (utf-8) |
Common Examples
Change a Configuration Value
Comment/Uncomment Lines
Replace with Variables
Delete Matching Lines
Replace Within a Section (before/after)
Multi-Line Replacement
Backup Before Replacing
Validate After Replacement
replace vs lineinfile vs template
| Use Case | Module | |----------|--------| | Replace text matching a regex anywhere in file | replace | | Ensure a specific line exists | lineinfile | | Replace with Jinja2 logic/loops | template | | Replace all occurrences of a pattern | replace | | Add/modify one line | lineinfile | | Manage entire file content | template or copy |
Regex Tips for Ansible replace
Loop Over Multiple Files
FAQ
What is the Ansible replace module?
The ansible.builtin.replace module performs regex-based find-and-replace on files. It replaces ALL occurrences of a pattern in a file, unlike lineinfile which manages single lines. It's ideal for bulk text changes and configuration updates.
How do I find and replace text in Ansible?
Use ansible.builtin.replace with path (file to modify), regexp (pattern to find), and replace (replacement text). The module uses Python regular expressions and replaces all matching occurrences.
What is the difference between Ansible replace and lineinfile?
replace finds and replaces ALL occurrences of a regex pattern anywhere in the file. lineinfile ensures a specific line exists (adds it if missing, replaces if the regex matches). Use replace for bulk changes; lineinfile for managing individual lines.
Can Ansible replace use regex capture groups?
Yes. Use parentheses in regexp to capture groups and \1, \2 in replace to reference them. Example: regexp: '(port)\s+\d+' with replace: '\1 8080'.
How do I replace text only in a specific section of a file?
Use the after and before parameters to limit the replacement scope. For example, after: '\[server\]' and before: '\[database\]' only replaces within the [server] section.
Conclusion
The Ansible replace module is essential for regex-based text modifications across your infrastructure. Use it for configuration updates, bulk changes, and pattern-based modifications. Combine with backup for safety and validate for configuration verification.
Related Articles • Ansible lineinfile Module: Add & Replace Lines • Ansible template Module: Deploy Jinja2 Templates • Ansible blockinfile Module: Insert Text Blocks
Category: database-automation