Bootstrap FreeKB - Ansible - List files to Amazon Web Services (AWS) S3 Buckets using the s3_object module
Ansible - List files to Amazon Web Services (AWS) S3 Buckets using the s3_object module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

Prerequisites

s3_object can be used to upload files your Amazon Web Services (AWS) S3 Buckets. 

---
- hosts: localhost
  tasks:
  - name: list objects in S3 bucket
    amazon.aws.s3_object:
      bucket: my-bucket-abcdefg
      mode: list
    register: objects

  - debug:
      var: objects
...

 

Something like this should be returned.

ok: [localhost] => {
    "list": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python3.9"
        },
        "changed": false,
        "failed": false,
        "msg": "LIST operation complete",
        "s3_keys": [
            "/",
            "a_directory/",
            "a_directory/images/foo.jpg",
            "a_directory/images/bar.png",
            "b_directory/",
            "b_directory/images/foo.jpg",
            "b_directory/images/bar.png"
        ],
    }
}

 

prefix can be used to only list items in a certain directory, such as "a_directory".

---
- hosts: localhost
  tasks:
  - name: list objects in S3 bucket
    amazon.aws.s3_object:
      bucket: my-bucket-abcdefg
      mode: list
      prefix: a_directory/
    register: objects

  - debug:
      var: objects
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 301beb in the box below so that we can be sure you are a human.