Getting aws account id

Hi guys,

does anyone have a good option to get the my aws account id using Ansible ?

Thanks !

You can use a Ansible Filter Plugin.

I have a repo with all of my filter plugins. Line 8 has the function you need.

`

def get_account_id(region):
    client = boto3.client('iam', region_name=region)
    try:
        account_id = client.list_users()['Users'][0]['Arn'].split(':')[4]
        return account_id
    except Exception:
        raise errors.AnsibleFilterError(
            "Failed to retrieve account id"
        )

`

https://github.com/linuxdynasty/ld-ansible-filters/blob/master/filter_plugins/aws.py#L8

Thanks Allen, I’ll give it a try !