Sep-18-2020, 02:02 PM
You have a very valid point and apologies for not providing the correct information. Please find below:
def get_instance_info(env): ec2_details = [] get_ec2 = ec2.describe_instances( Filters=[ { 'Name': 'tag:environment', 'Values': [env] }, ] ) for reservation in get_ec2['Reservations']: for instance in reservation['Instances']: ec2_info = {} ec2_info.update({'InstanceID':instance['InstanceId']}) ec2_info.update({'State':instance['State']['Name']}) ec2_info.update({'PrivateIpAddress':instance['PrivateIpAddress']}) ec2_info.update({'InstanceType':instance['InstanceType']}) for tag in instance['Tags']: if tag['Key'] in 'Name': ec2_info.update({'Name':tag['Value']}) for tag in instance['Tags']: if tag['Key'] in 'environment': ec2_info.update({'Environment':tag['Value']}) else: ec2_info.setdefault('Environment','MISSING') for tag in instance['Tags']: if tag['Key'] in 'role': ec2_info.update({'Role':tag['Value']}) else: ec2_info.setdefault('Role','MISSING') ec2_details.append(ec2_info) return ec2_details def get_ip_info(ipaddr): ip_details = [] get_ip = ec2.describe_instances( Filters=[ { 'Name': 'private-ip-address', 'Values': [ipaddr] },]) for reservation in get_ip['Reservations']: for instance in reservation['Instances']: ip_info = {} ip_info.update({'InstanceID':instance['InstanceId']}) ip_info.update({'State':instance['State']['Name']}) ip_info.update({'PrivateIpAddress':instance['PrivateIpAddress']}) ip_info.update({'InstanceType':instance['InstanceType']}) for tag in instance['Tags']: if tag['Key'] in 'Name': ip_info.update({'Name':tag['Value']}) for tag in instance['Tags']: if tag['Key'] in 'environment': ip_info.update({'Environment':tag['Value']}) else: ip_info.setdefault('Environment','MISSING') for tag in instance['Tags']: if tag['Key'] in 'role': ip_info.update({'Role':tag['Value']}) else: ip_info.setdefault('Role','MISSING') ip_details.append(ip_info) return ip_details def get_role_info(role): role_details = [] get_role = ec2.describe_instances( Filters=[ { 'Name': 'tag:role', 'Values': [role] },]) for reservation in get_role['Reservations']: for instance in reservation['Instances']: role_info = {} role_info.update({'InstanceID':instance['InstanceId']}) role_info.update({'State':instance['State']['Name']}) role_info.update({'PrivateIpAddress':instance['PrivateIpAddress']}) role_info.update({'InstanceType':instance['InstanceType']}) for tag in instance['Tags']: if tag['Key'] in 'Name': role_info.update({'Name':tag['Value']}) for tag in instance['Tags']: if tag['Key'] in 'environment': role_info.update({'Environment':tag['Value']}) else: role_info.setdefault('Environment','MISSING') for tag in instance['Tags']: if tag['Key'] in 'role': role_info.update({'Role':tag['Value']}) else: role_info.setdefault('Role','MISSING') role_details.append(role_info) return role_details