Hi@akhtar,
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
lineinfile:
path: /etc/sudoers
state: present
regexp: '^%ADMIN ALL='
line: '%ADMIN ALL=(ALL) NOPASSWD: ALL'
validate: /usr/sbin/visudo -cf %s