Ansible automate backup restoration

Hi. Im having trouble in my backup test playbook.

backed up files are is Amazon s3 bucket.
The problem is - we have many versions there. i need to limit rsync to latest version without providing it manually, or to prompt me for choice.
At the moment im stuck. I fail to find a module which could help me.

  • name: List s3 objects
    aws_s3:
    bucket: “bucket_name”
    prefix: “application_folder”
    max_keys: 3 *(application has 3 files in it so im trying to limit to the last 3 - unnsuccesfully though. )
    mode: list
    register: s3objects
    become: yes
    become_user: username

  • debug: var=s3objects

  • name: write to file
    copy:
    content: “{{ s3objects }}”
    dest: /home/username/application_folder_version
    mode: 0644
    force: yes
    with_items: s3objects
    become: yes
    become_user: username

outcome:

“application/2018-08-21/1.78/application.properties”,
“application/2018-08-21/1.78/run.sh”,
“application/2018-08-21/1.78/application.jar”,
“application/2018-08-22/1.79/application.properties”,
“application/2018-08-22/1.79/run.sh”,
“application/2018-08-22/1.79/application.jar”,
“application/2018-08-30/1.80/application.properties”,
“application/2018-08-30/1.80/run.sh”,
“application/2018-08-30/1.80/application.jar”,
“application/2018-09-06/1.81/application.properties”,
“application/2018-09-06/1.81/run.sh”,
“application/2018-09-06/1.81/application.jar”,
“application/2018-09-07/1.81/application.properties”,
“application/2018-09-07/1.81/run.sh”,
“application/2018-09-07/1.81/application.jar”,
“application/2018-09-17/1.82/application.properties”,
“application/2018-09-17/1.82/run.sh”,
“application/2018-09-17/1.82/application.jar”,
“application/2018-10-11/1.83/application.properties”,
“application/2018-10-11/1.83/run.sh”,
“application/2018-10-11/1.83/application.jar”,
“application/2018-10-16/1.84/application.properties”,
“application/2018-10-16/1.84/run.sh”,
“application/2018-10-16/1.84/application.jar”,
“application/2018-10-18/1.85/application.properties”,
“application/2018-10-18/1.85/run.sh”,
“application/2018-10-18/1.85/application.jar”,
“application/2018-10-25/1.86/application.properties”,
“application/2018-10-25/1.86/run.sh”,
“application/2018-10-25/1.86/application.jar”,
“application/2018-10-31/1.87/application.properties”,
“application/2018-10-31/1.87/run.sh”,
“application/2018-10-31/1.87/application.jar”,
“application/2018-11-07/1.88/application.properties”,
“application/2018-11-07/1.88/run.sh”,
“application/2018-11-07/1.88/application.jar”

I could read last 3 lines from a file and take them as variables, but i don´t see how to do it with ansible.

Thanks for reading.

Maybe restructure the tasks and take a single file into account, so
that you can simple sort and pick the last one.
Then iterate over each of the 3 files.

Dick

im still in trouble with even a single file, because of the directory structure. The folder with a date is generated automatically and i wont know it in advance.

meaning

“application/2018-10-31/1.87/application.properties”,
“application/2018-10-31/1.87/run.sh”,
“application/2018-10-31/1.87/application.jar”,
“application/2018-11-07/1.88/application.properties”,
“application/2018-11-07/1.88/run.sh”,
“application/2018-11-07/1.88/application.jar”
application/Foldernamechanges/versionnamechanges/static.jar

You have left out a lot of useful information, I have never user aws_s3 so I don't know it's structure.
But to get the 3 last elements in a list it's just <some list variable>[-3]