59

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?

1 Answer 1

95

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.

6
  • Thanks for the quick response. I have this: ` copy: src="{{ templates_dir }}/nginx/index.php" dest=/var/www/index.php owner=vagrant` but it overwrites every time.
    – Dubby
    Commented Feb 8, 2014 at 13:06
  • But thank you. Your link helped as I found by setting force to false it is not overwritten. Something must be setting the default to force=true somewhere.
    – Dubby
    Commented Feb 8, 2014 at 13:10
  • I'm sorry, my answer was incorrect. You're right, "force" defaults to yes and you have to set it to "no". I've updated the answer and it should hopefully be correct now. Commented Feb 8, 2014 at 13:20
  • 3
    To create an empty file (eg for logs) you can do copy: content="" dest=/var/www/index.php force=no
    – dave1010
    Commented Dec 19, 2014 at 11:33
  • 2
    Creating empty files can also be done using file: path=/var/log/file.log state=touch mode="u=rw,g=r,o=r"
    – castis
    Commented Aug 20, 2016 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.