# Making S3 get overwrite behavior match get\_url

**URL:** <https://forum.ansible.com/t/making-s3-get-overwrite-behavior-match-get-url/15434>\
**Category:** Ansible Developer\
**Created:** [June 10, 2014, 12:20am UTC](https://forum.ansible.com/t/making-s3-get-overwrite-behavior-match-get-url/15434 "2014-06-10T00:20:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [June 10, 2014, 12:20am UTC](https://forum.ansible.com/t/making-s3-get-overwrite-behavior-match-get-url/15434/1 "2014-06-10T00:20:42Z")

</div>

Hi,

I am using S3 to store my build artifacts and I would like to copy them to hosts using the s3 module using something like the following task:

- s3: mode=get s3 bucket=myartifacts object=/website.zip dest=/srv/www/website.zip

The tasks accepts an ‘overwrite’ parameter that determines whether to overwrite the local file. Unfortunately, the behavior is different than what I would expect. If overwrite=yes, the local file will always be overwritten – even if it is identical. (Note that this means that the tasks always returns changed status.) Of overwrite=no, then the task will fail if an existing file is not identical.

At minimum I would like the module not to overwrite a local file with an identical one. The change would entail exiting early when the md5sums match.

But really, I would like to see the module behavior match the ‘force’ parameter from ‘get\_url’: if ‘no’ leave any existing file even if different, if ‘yes’ overwrite if different. However, ‘force’ is already an alias for ‘overwrite’ so this means introducing backward incompatibility.

Am I correct in thinking current behavior is wrong? If so, what is the correct course of action: only fix overwriting identical files or change to ‘get\_url’ semantics?

Regards,  
Joost

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 10, 2014, 4:19pm UTC](https://forum.ansible.com/t/making-s3-get-overwrite-behavior-match-get-url/15434/2 "2014-06-10T16:19:08Z")

</div>

If overwrite is no, we should make sure the task does not fail if the file already exists, that’s for sure.

If overwrite is yes, I _DO_ agree it should overwrite the file, that is what was requested.

---

<div class="post-metadata">

**Author:** ![Michael\_Schultz](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Michael\_Schultz](https://forum.ansible.com/u/Michael_Schultz)\
**Post date:** [June 10, 2014, 4:45pm UTC](https://forum.ansible.com/t/making-s3-get-overwrite-behavior-match-get-url/15434/3 "2014-06-10T16:45:23Z")

</div>

Yeah, I have the same complaint in my notes concerning the s3 module, it doesn’t quite match the copy module’s force argument which is what I expected.

From the copy module:

> 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. (added in Ansible 1.1)

Specifically, force=yes only replaces the remote file when contents are different than source. This differs from s3\_module’s overwrite=yes which always replaces the file even if the contents are the same.

I agree that “overwrite” means the file should be overwritten no matter what, but there should be an option to only write the file when the contents are changed (and that isn’t overwrite=no because that implies never overwrite even if contents differ).

I guess there are three options being fit into a boolean:

- always replace the file
- replace the file only if contents have changed
- never replace the file

s3\_module is missing the middle option I believe.

–michael

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 10, 2014, 8:28pm UTC](https://forum.ansible.com/t/making-s3-get-overwrite-behavior-match-get-url/15434/4 "2014-06-10T20:28:59Z")

</div>

Sounds like the solution here might be to introduce a trinary as a possible value, attempt to booleanify it using the module\_utils code and if it doesn’t booleanify, but is set to “smart”, it would do that middle option.

This could keep everything working while adding the new bits.

---

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [June 10, 2014, 11:42pm UTC](https://forum.ansible.com/t/making-s3-get-overwrite-behavior-match-get-url/15434/5 "2014-06-10T23:42:15Z")

</div>

"smart" sounds like a good compromise. I noticed some possibilities  
for refactoring as well. Okay if I take a stab at it?

Regards,  
Joost

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 11, 2014, 10:10pm UTC](https://forum.ansible.com/t/making-s3-get-overwrite-behavior-match-get-url/15434/6 "2014-06-11T22:10:37Z")

</div>

Definitely, please do!

---

<div class="post-metadata">

**Author:** ![Michael\_Schultz](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Michael\_Schultz](https://forum.ansible.com/u/Michael_Schultz)\
**Post date:** [June 16, 2014, 6:09pm UTC](https://forum.ansible.com/t/making-s3-get-overwrite-behavior-match-get-url/15434/7 "2014-06-16T18:09:39Z")

</div>

Joost,

Let me know if you are working on this, otherwise I can probably squeeze some time in to fix it. I would also like to see an option to set the file permissions of a local file when it is downloaded (but that may be beyond scope).

–michael

---

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [June 16, 2014, 6:43pm UTC](https://forum.ansible.com/t/making-s3-get-overwrite-behavior-match-get-url/15434/8 "2014-06-16T18:43:03Z")

</div>

Hi Micheal,

Yeah, I'm working on it. Should have a PR this evening. Thanks for the  
offer, if something comes between I'll take you up on it!

Regards,  
Joost

---

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [June 16, 2014, 10:41pm UTC](https://forum.ansible.com/t/making-s3-get-overwrite-behavior-match-get-url/15434/9 "2014-06-16T22:41:10Z")

</div>

Hi,

I created a pull request for the changes I made:  
[https://github.com/ansible/ansible/pull/7797](https://github.com/ansible/ansible/pull/7797)

@MichaelS: if you want to add file permissions we should probably take  
it one step at a time. I hope the refactorings will make it easier to  
implement your changes.

Regards,  
Joost

---

<div class="post-metadata">

**Author:** ![Michael\_Schultz](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@Michael\_Schultz](https://forum.ansible.com/u/Michael_Schultz)\
**Post date:** [June 19, 2014, 9:31pm UTC](https://forum.ansible.com/t/making-s3-get-overwrite-behavior-match-get-url/15434/10 "2014-06-19T21:31:13Z")

</div>

Joost,

Yeah that functionality should be separately added, so I’ll work on it when I get the chance. I just used the code in your PR for a few deploys and the `smart` more worked as I was expecting. Thanks!

–michael
