# copy module: fail if remote file exists and is different from source?

**URL:** <https://forum.ansible.com/t/copy-module-fail-if-remote-file-exists-and-is-different-from-source/19878>\
**Category:** Ansible Project\
**Created:** [November 27, 2015, 6:24pm UTC](https://forum.ansible.com/t/copy-module-fail-if-remote-file-exists-and-is-different-from-source/19878 "2015-11-27T18:24:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [November 27, 2015, 6:24pm UTC](https://forum.ansible.com/t/copy-module-fail-if-remote-file-exists-and-is-different-from-source/19878/1 "2015-11-27T18:24:50Z")

</div>

Hi,

is there an (easy) way to make the copy module [1] fail if the remote  
file exists and is different from the source file?  
The goal is to  
- not override the destination (force=no) and  
- abort if the file exists and is different from the source.

The fetch module [2] has a 'fail\_on\_missing' parameter. I would need a  
fail\_on\_remotefileexists\_but\_different 😉  
Currently one is left with  
- backup=yes  
or  
- force=no  
but both parameter won't help for the specific use-case.

Anyone else had this use-case yet and made a workaround?

thanks,  
nusenu

[1] [https://docs.ansible.com/ansible/copy\_module.html](https://docs.ansible.com/ansible/copy_module.html)  
[2] [https://docs.ansible.com/ansible/fetch\_module.html](https://docs.ansible.com/ansible/fetch_module.html)

---

<div class="post-metadata">

**Author:** ![Nusenu1](https://avatars.discourse-cdn.com/v4/letter/n/db5fbb/32.png) [@Nusenu1](https://forum.ansible.com/u/Nusenu1)\
**Post date:** [November 27, 2015, 9:48pm UTC](https://forum.ansible.com/t/copy-module-fail-if-remote-file-exists-and-is-different-from-source/19878/2 "2015-11-27T21:48:41Z")

</div>

> is there an (easy) way to make the copy module [1] fail if the remote  
> file exists and is different from the source file?  
> The goal is to  
> - not override the destination (force=no) and  
> - abort if the file exists and is different from the source.
> 
> The fetch module [2] has a 'fail\_on\_missing' parameter. I would need a  
> fail\_on\_remotefileexists\_but\_different 😉  
> Currently one is left with  
> - backup=yes  
> or  
> - force=no  
> but both parameter won't help for the specific use-case.
> 
> Anyone else had this use-case yet and made a workaround?

My workaround procedure is [1]:

1) copy with force=no  
2) fetch  
3) compare files locally via shell (sha1sum)  
4) fail if fingerprints do not match

If there is a less dirty solution let me know 😉

[1]  
[https://github.com/nusenu/ansible-relayor/commit/b2462b6b8b4ed3b1351ff167c6dd5f7ed3263e7a](https://github.com/nusenu/ansible-relayor/commit/b2462b6b8b4ed3b1351ff167c6dd5f7ed3263e7a)

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 3, 2015, 7:21pm UTC](https://forum.ansible.com/t/copy-module-fail-if-remote-file-exists-and-is-different-from-source/19878/3 "2015-12-03T19:21:31Z")

</div>

you could just use stat module to check the file remotely (you would  
also get hash to compare to local file)
