The replace module can be used to replace markup in a file on the managed node (e.g. the target system). Or, the connection, delegate_to, or local_action modules/parameters can be used to run the task on the control node (that's your Ansible server).
The ansible-doc replace command can be used to show the Ansible documention on the replace module.
Let's say /tmp/foo.txt contains the following text.
In this example, every line beginning with Hello in /tmp/foo.txt will be updated to contain just the text World.
- name: "replace lines beginning with 'Hello' followed by anything with 'World' in foo.txt" replace: path: "/tmp/foo.txt: regexp: "^Hello.*" replace: "World"
If the file was successfully updated, the play should indicate changed.
TASK [replace lines beginning with 'Hello' followed by anything with 'World' in foo.txt] changed: [server1.example.com]
If the file was not updated, the play should indicate ok. This is the expected behavior if there are no lines to replace in the file.
TASK [replace lines beginning with 'Hello' followed by anything with 'World' in foo.txt] ok: [server1.example.com]