# S3 list result to file or variable

**URL:** <https://forum.ansible.com/t/s3-list-result-to-file-or-variable/28964>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [November 7, 2018, 1:39pm UTC](https://forum.ansible.com/t/s3-list-result-to-file-or-variable/28964 "2018-11-07T13:39:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonitsh](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tonitsh](https://forum.ansible.com/u/tonitsh)\
**Post date:** [November 7, 2018, 1:39pm UTC](https://forum.ansible.com/t/s3-list-result-to-file-or-variable/28964/1 "2018-11-07T13:39:22Z")

</div>

Hi. Im trying to automate our restore machine testing.  
At one point i need to connect to amason S3 and download the latest data from a bucket.

s3://bucet.name/application\_name/DATE/version/files1-3

What i have tried thus far:

- name: List s3 objects  
aws\_s3:  
bucket: “bucket.name”  
prefix: “/application\_name/”  
dest: /home/user/tmp-file  
mode: list  
max\_keys: 5  
register: s3objects

become: yes  
become\_user: user

- name: show list?  
debug:  
msg: “heres the list: {{ s3objects.s3\_keys }}”

- name: write to file  
copy:  
content: “{{ s3objects }}”  
dest: /home/user/tmp-file  
with\_items: s3objects.s3\_keys  
become: yes  
become\_user: user

The tmp file has this info:  
“{“msg”: “LIST operation complete”, “failed”: false, “changed”: false, “s3\_keys”: }”

Im very new to ansible, what i would ultimately want is to get this:  
aws s3 ls s3://bucet.name/application\_name/ | tail -5

---

<div class="post-metadata">

**Author:** ![tonitsh](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tonitsh](https://forum.ansible.com/u/tonitsh)\
**Post date:** [November 7, 2018, 1:57pm UTC](https://forum.ansible.com/t/s3-list-result-to-file-or-variable/28964/2 "2018-11-07T13:57:20Z")

</div>

> hmm i see that i have one thing wrong.

prefix is not what i thought it was. If i remove it i get the list for all the applications.

---

<div class="post-metadata">

**Author:** ![tonitsh](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tonitsh](https://forum.ansible.com/u/tonitsh)\
**Post date:** [November 7, 2018, 2:27pm UTC](https://forum.ansible.com/t/s3-list-result-to-file-or-variable/28964/3 "2018-11-07T14:27:30Z")

</div>

ANd if i enter prefix without / \ then i get the correct data.  
max\_keys has no effect tho.

kolmapäev, 7. november 2018 15:57.20 UTC+2 kirjutas [ton...@gmail.com](mailto:ton...@gmail.com):

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [November 7, 2018, 9:05pm UTC](https://forum.ansible.com/t/s3-list-result-to-file-or-variable/28964/4 "2018-11-07T21:05:40Z")

</div>

That must be irritating, since every example has leading slashes on the prefix!

Regards, K.
