file module - state=touch always finishes as changed

Hello Ansible Community,

I'd like to create a task which ensures a file exists(if a file
doesn't exist, it should create an empty file). Now I use file
module's state=touch, but it always finishes as changed. Is there a
way to prevent it from touching when file exists?

Best regards,
Koji

RESOLVED. I found that adding "changed_when: False" does the trick, as
suggested on this issue.
https://github.com/ansible/ansible-modules-core/issues/170

Thank you,
Koji

I actually tend to use the copy module, like so:

  • copy: content=“” dest=/path/to/file force=no

force=no keeps it from replacing the file when it exists and the content is different.

http://docs.ansible.com/ansible/copy_module.html