You can do this using lineinfile module in Ansible. After changing the data, this module has the capability to validate the file as shown below.
- name: Validate the sudoers file before saving
regexp: '^%ADMIN ALL='
line: '%ADMIN ALL=(ALL) NOPASSWD: ALL'
validate: /usr/sbin/visudo -cf %s