How to skip the task by checking the file exists or not using when

I have to check whether a file exists or not in /etc/. If the file exists then I have to skip the task otherwise the task should be run. Below is the code

  • name: check whether the file exists
    command: touch /etc/file.txt
    when: $(! -s /etc/file.txt)

If the file exists then task should be skipped other wise run the task

use stat module and make a condition on the registered result, when won’t execute a shell, a lookup could be used but they run on ‘master’ not on target.