Ansible - List Amazon Web Services (AWS) S3 Buckets using the aws_s3_bucket_info module
by
Jeremy Canfield |
Updated: July 31 2023
| Ansible articles
If you are not familiar with modules, check out Ansible - Getting Started with Modules.
Prerequisites
- Before you can use the Ansible Amazon Web Services (AWS) modules, you will need to install the AWS CLI tool on the hosts that will be using the Ansible Amazon Web Services (AWS) modules. Check out my article on Getting Started with the Ansible Amazon Web Services (AWS) modules.
- You will also need to set your Amazon Web Services (AWS) Profile Configurations. Check out my article Set Amazon Web Services (AWS) Profile Configurations.
- The aws_s3_bucket_info requires the following packages. Check out my article Resolve "boto3 required for this module".
- botocore version 1.25.0 or higher
- boto3 version 1.22.0 or higher
- Python 3.6 or higher must be used. The ansible --version command can be used to list the version of Python being used with Ansible. If your Ansible installation is used a version lower than Python 3.6, one solution would be to install Ansible in a Python virtual environment using Python 3.6 or higher.
- The community.aws collection will need to be installed. Check out my article on Install a collection using the ansible-galaxy collection install command.
aws_s3_bucket_info can be used to list your Amazon Web Services (AWS) S3 Buckets.
---
- name: main play
hosts: localhost
tasks:
- name: list S3 Buckets
community.aws.aws_s3_bucket_info:
register: out
- debug:
var: out
...
Something like this should be returned.
ok: [localhost] => (item={u'name': u'my-bucket-abcdefg', u'creation_date': u'2023-06-02T02:22:19+00:00'}) => {
"ansible_loop_var": "item",
"item": {
"creation_date": "2023-06-02T02:22:19+00:00",
"name": "my-bucket-abcdefg"
},
"item.name": "my-bucket-abcdefg"
}
Extra parameters such as the buckets Tags can be returned like this.
---
- name: main play
hosts: localhost
tasks:
- name: list S3 Buckets
community.aws.aws_s3_bucket_info:
bucket_facts:
bucket_tagging: true
register: out
- debug:
var: out
...
Which should return something like this.
ok: [staging_docker_2] => {
"s3_buckets": {
"buckets": [
{
"bucket_tagging": {
"environment": "staging"
},
"creation_date": "2023-07-21T23:17:02+00:00",
"name": "my-bucket-abcdefg"
}
],
"changed": false,
"failed": false,
"msg": "Retrieved s3 info."
}
}
Did you find this article helpful?
If so, consider buying me a coffee over at