From b376bde4d77aaf5f5080da5fa634c1b4aebd656e Mon Sep 17 00:00:00 2001 From: Sloane Hertel Date: Fri, 9 Jun 2017 11:31:42 -0400 Subject: [PATCH] [cloud] ec2_eni module - allow attaching a new instance - fixes #19452 (#22377) * fix bug attaching instances * pep8 --- lib/ansible/modules/cloud/amazon/ec2_eni.py | 14 +++++++++++--- test/sanity/pep8/legacy-files.txt | 1 - 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2_eni.py b/lib/ansible/modules/cloud/amazon/ec2_eni.py index e7eed690fa8..3a8690cfafa 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_eni.py +++ b/lib/ansible/modules/cloud/amazon/ec2_eni.py @@ -426,6 +426,9 @@ def modify_eni(connection, vpc_id, module, eni): if attached is True: if eni.attachment and eni.attachment.instance_id != instance_id: detach_eni(eni, module) + eni.attach(instance_id, device_index) + wait_for_eni(eni, "attached") + changed = True if eni.attachment is None: eni.attach(instance_id, device_index) wait_for_eni(eni, "attached") @@ -472,10 +475,14 @@ def delete_eni(connection, module): def detach_eni(eni, module): + attached = module.params.get("attached") + force_detach = module.params.get("force_detach") if eni.attachment is not None: eni.detach(force_detach) wait_for_eni(eni, "detached") + if attached: + return eni.update() module.exit_json(changed=True, interface=get_eni_info(eni)) else: @@ -489,6 +496,7 @@ def uniquely_find_eni(connection, module): subnet_id = module.params.get('subnet_id') instance_id = module.params.get('instance_id') device_index = module.params.get('device_index') + attached = module.params.get('attached') try: filters = {} @@ -501,7 +509,7 @@ def uniquely_find_eni(connection, module): filters['private-ip-address'] = private_ip_address filters['subnet-id'] = subnet_id - if instance_id and device_index: + if not attached and instance_id and device_index: filters['attachment.instance-id'] = instance_id filters['attachment.device-index'] = device_index @@ -562,11 +570,11 @@ def main(): module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=[ ['secondary_private_ip_addresses', 'secondary_private_ip_address_count'] - ], + ], required_if=([ ('state', 'absent', ['eni_id']), ('attached', True, ['instance_id']) - ]) + ]) ) if not HAS_BOTO: diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index ce83d1af9d3..63f84371dca 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -17,7 +17,6 @@ lib/ansible/modules/cloud/amazon/ec2_eip.py lib/ansible/modules/cloud/amazon/ec2_elb.py lib/ansible/modules/cloud/amazon/ec2_elb_facts.py lib/ansible/modules/cloud/amazon/ec2_elb_lb.py -lib/ansible/modules/cloud/amazon/ec2_eni.py lib/ansible/modules/cloud/amazon/ec2_eni_facts.py lib/ansible/modules/cloud/amazon/ec2_key.py lib/ansible/modules/cloud/amazon/ec2_lc.py