Follow

@mhamzahkhan

Pure Ansible solution would be as follows:

- name: ensure file exists
copy:
content: ""
dest: /etc/nologin
force: true

Ansible-lint will whine about the user, group and chmod not specified explicitly but it can be ignored with # noqa annotation since your objective is to specifically preserve the existing permissions.