diff --git a/cloud/amazon/ec2_eni.py b/cloud/amazon/ec2_eni.py index b87e7c47304..ac05ba43a39 100644 --- a/cloud/amazon/ec2_eni.py +++ b/cloud/amazon/ec2_eni.py @@ -241,7 +241,6 @@ try: except ImportError: HAS_BOTO = False - def get_eni_info(interface): # Private addresses @@ -390,7 +389,10 @@ def modify_eni(connection, vpc_id, module, eni): secondary_addresses_to_remove_count = current_secondary_address_count - secondary_private_ip_address_count connection.unassign_private_ip_addresses(network_interface_id=eni.id, private_ip_addresses=current_secondary_addresses[:secondary_addresses_to_remove_count], dry_run=False) - if attached == True and instance_id is not None: + if attached == True: + if eni.attachment and eni.attachment.instance_id != instance_id: + detach_eni(eni, module) + if eni.attachment is None: eni.attach(instance_id, device_index) wait_for_eni(eni, "attached") changed = True @@ -451,13 +453,20 @@ def find_eni(connection, module): eni_id = module.params.get("eni_id") subnet_id = module.params.get('subnet_id') private_ip_address = module.params.get('private_ip_address') + instance_id = module.params.get('instance_id') + device_index = module.params.get('device_index') try: filters = {} - if private_ip_address: - filters['private-ip-address'] = private_ip_address if subnet_id: filters['subnet-id'] = subnet_id + if private_ip_address: + filters['private-ip-address'] = private_ip_address + else: + if instance_id: + filters['attachment.instance-id'] = instance_id + if device_index: + filters['attachment.device-index'] = device_index eni_result = connection.get_all_network_interfaces(eni_id, filters=filters) if len(eni_result) > 0: