Vinicius Peres
Vinicius Peres

Reputation: 101

“How to ‘Count AWS S3 Files in subfolder’ in Ansible”

Initially I want to be able to count the number of files I have in each subfolder. I want to be able to run the manual in the Ansible program, where I get a count of the files in each subfolder.

Ansible 2.8

  tasks:
    - name: AwsS3
      aws_s3:
        bucket: teste-acme
        mode: list
        prefix: folder/
      register: s3_bucket_items

    - name: CountFilesS3Folder
      debug: 
        msg: "{{s3_bucket_items.s3_keys |length }}"
     with_items: s3_bucket_items

I expect the output of "[CountFilesS3Folder]" to be

bambu       1
bonsai      1
sakura      4
shibazakura 3
ume         2 

but the actual output is:

TASK [AwsS3] 
**********************************
"s3_keys": [
        "folder/bambu/bambu-338400da-1.tgz", 
        "folder/bonsai/bonsai-0de3166b-1.tgz", 
        "folder/sakura/sakura-1236s38f-1.tgz",
        "folder/sakura/sakura-342bd38f-2.tgz", 
        "folder/sakura/sakura-4567888f-3.tgz",
        "folder/sakura/sakura-678338sf-4.tgz", 
        "folder/shibazakura/shibazakura-1f5273e8-1.tgz",
        "folder/shibazakura/shibazakura-123asde8-2.tgz", 
        "folder/shibazakura/shibazakura-asdqwee8-3.tgz", 
        "folder/ume/ume-3164f62c-1.tgz",
        "folder/ume/ume-4c12312c-2.tgz",

TASK [CountFilesS3Folder] 
**********************************
ok: [localhost] => {
    "msg": "11"

Upvotes: 0

Views: 321

Answers (1)

Pacifist
Pacifist

Reputation: 3203

Redirect this variable output to a file and use ansible command/shell module to run the below command which will give you the desired result.

cat file.txt | awk -F'/' '{print $2}'|sort|uniq -c

Output will be :

1 bambu
1 bonsai
4 sakura
3 shibazakura
2 ume

Upvotes: 1

Related Questions