Is this what you are looking for? “Append to existing line” section ==> Bootstrap
Append to an existing line
Let's say foo.txt contains "Hello World". Here is how you would append "example" after Hello World.
- lineinfile:
path: /tmp/foo.txt
backrefs: true
regexp: '^(old line)$'
line: '\1 example'