File tree Expand file tree Collapse file tree 1 file changed +5
-7
lines changed Expand file tree Collapse file tree 1 file changed +5
-7
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import configparser
33import os
44import sys
55
6- # script.py - List AWS profile names with account IDs extracted from role_arn in credentials file
6+ # script.py - List AWS profile names with account IDs from aws_account_id or extracted from role_arn
77
88# Get patterns from command-line arguments
99patterns = sys .argv [1 :]
@@ -31,12 +31,10 @@ def get_account_id_from_arn(arn):
3131# Iterate through the sections and print those that match the patterns along with the account ID
3232for section in config .sections ():
3333 if not patterns or any (pattern in section for pattern in patterns ):
34- try :
35- # Attempt to fetch the role_arn from the credentials file
34+ account_id = config .get (section , 'aws_account_id' ) if config .has_option (section , 'aws_account_id' ) else 'Unknown'
35+ # If aws_account_id is not found, try to extract it from role_arn
36+ if account_id == 'Unknown' and config .has_option (section , 'role_arn' ):
3637 role_arn = config .get (section , 'role_arn' )
3738 account_id = get_account_id_from_arn (role_arn )
38- except (configparser .NoSectionError , configparser .NoOptionError ):
39- # If the role_arn is not found, set account_id to 'Unknown'
40- account_id = 'Unknown'
41-
39+
4240 print (f"{ section } \t { account_id } " )
You can’t perform that action at this time.
0 commit comments