Besides src and dest, what can the template module set on the deployed file, and what does validate protect against?
The same attributes as file (owner, group, mode, SELinux context); validate runs a check command on the rendered file before it replaces the real one, so a broken config is never installed.
- name: Deploy sudoers rules
ansible.builtin.template:
src: sudoers.j2
dest: /etc/sudoers.d/admins
owner: root
group: root
mode: "0440"
validate: /usr/sbin/visudo -cf %s
validate works like this: Ansible writes the rendered content to a temporary file, runs the command with %s replaced by that temporary path, and only if the command exits 0 moves the file into place. If it fails, the task fails and the old file stays untouched.
This matters most for files whose breakage locks you out: a sudoers file with a syntax error disables sudo (and therefore Ansible's become); a broken sshd_config can stop SSH from starting. Typical checks are visudo -cf %s, sshd -t -f %s, nginx -t -c %s or apachectl -t -f %s. %s must appear in the command.
Go deeper:
ansible.builtin.template: validate (official docs) โ the temporary-file mechanism and examples.