Ansible: copy file if not exists

Ansible

Ansible Problem Overview


I've seen the question asked in a round about sort of way but not conclusively answered. What I want to do is straight forward. I want to copy a file index.php to the remote host at /var/www/index.php but only if it doesn't already exist.

I've tried using creates and only_if but I don't think these are intended for the purpose I want here. Can anyone supply some examples of how I would go about this?

Ansible Solutions


Solution 1 - Ansible

Assuming index.php exists in the role's files subdirectory:

- copy: src=index.php dest=/var/www/index.php force=no

The decisive property is force. As the documentation explains, the default is yes, which will replace the remote file when contents are different than the source. If no, the file will only be transferred if the destination does not exist.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDubbyView Question on Stackoverflow
Solution 1 - AnsibleFernando CorreiaView Answer on Stackoverflow