Junior — Middle
Which module in Ansible should be used to edit file contents to replace a line?
sobes.tech AI
Answer from AI
To replace content in a file using Ansible, the replace module is typically used.
It allows searching for lines with a regex and replacing them with the desired text.
Example usage:
- name: Replace a line in a file
replace:
path: /path/to/file.txt
regexp: 'old_text'
replace: 'new_text'
If you just need to add or change a line, you can also consider the lineinfile module, but for replacement based on a pattern, replace is more suitable.