You can use the lineinfile module for this purpose. This module will search a file for a line, and ensure that it is present or absent. This is useful when you want to change a single line in a file only.
Something like this should work:
- name: test lineinfile
lineinfile: backup=yes state=present dest=/etc/foo.txt