Skip to content

Commit 38a8a72

Browse files
Fix: ec2_instance idempotency issue when attaching ENI to isntance (#1576) (#1579)
[PR #1576/b7533c55 backport][stable-6] ec2_instance - fix idempotency issue when attaching ENI to isntance This is a backport of PR #1576 as merged into main (b7533c5). SUMMARY closes #1403 ISSUE TYPE Bugfix Pull Request COMPONENT NAME ec2_instance Reviewed-by: Alina Buzachis
1 parent 5ff9190 commit 38a8a72

File tree

3 files changed

+66
-11
lines changed

3 files changed

+66
-11
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
bugfixes:
3+
- ec2_instance - fix check_mode issue when adding network interfaces (https://github.com/ansible-collections/amazon.aws/issues/1403).

plugins/modules/ec2_instance.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,16 +1510,19 @@ def change_network_attachments(instance, params):
15101510
# network.interfaces can create the need to attach new interfaces
15111511
old_ids = [inty["NetworkInterfaceId"] for inty in instance["NetworkInterfaces"]]
15121512
to_attach = set(new_ids) - set(old_ids)
1513-
for eni_id in to_attach:
1514-
try:
1515-
client.attach_network_interface(
1516-
aws_retry=True,
1517-
DeviceIndex=new_ids.index(eni_id),
1518-
InstanceId=instance["InstanceId"],
1519-
NetworkInterfaceId=eni_id,
1520-
)
1521-
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
1522-
module.fail_json_aws(e, msg=f"Could not attach interface {eni_id} to instance {instance['InstanceId']}")
1513+
if not module.check_mode:
1514+
for eni_id in to_attach:
1515+
try:
1516+
client.attach_network_interface(
1517+
aws_retry=True,
1518+
DeviceIndex=new_ids.index(eni_id),
1519+
InstanceId=instance["InstanceId"],
1520+
NetworkInterfaceId=eni_id,
1521+
)
1522+
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
1523+
module.fail_json_aws(
1524+
e, msg=f"Could not attach interface {eni_id} to instance {instance['InstanceId']}"
1525+
)
15231526
return bool(len(to_attach))
15241527
return False
15251528

tests/integration/targets/ec2_instance_external_resource_attach/tasks/main.yml

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,39 @@
6060
- 'in_test_vpc_instance.instances.0.key_name == "{{ resource_prefix }}_test_key"'
6161
- '(in_test_vpc_instance.instances.0.network_interfaces | length) == 1'
6262

63+
- name: "Add a second interface (check_mode=true)"
64+
ec2_instance:
65+
state: present
66+
name: "{{ resource_prefix }}-test-eni-vpc"
67+
network:
68+
interfaces:
69+
- id: "{{ eni_a.interface.id }}"
70+
- id: "{{ eni_b.interface.id }}"
71+
image_id: "{{ ec2_ami_id }}"
72+
tags:
73+
TestId: "{{ ec2_instance_tag_TestId }}"
74+
instance_type: "{{ ec2_instance_type }}"
75+
wait: false
76+
register: add_interface_check_mode
77+
check_mode: true
78+
79+
- name: Validate task reported changed
80+
assert:
81+
that:
82+
- add_interface_check_mode is changed
83+
84+
- name: "Gather {{ resource_prefix }}-test-eni-vpc info"
85+
ec2_instance_info:
86+
filters:
87+
"tag:Name": '{{ resource_prefix }}-test-eni-vpc'
88+
register: in_test_vpc_instance
89+
90+
- name: Validate that only 1 ENI is attached to instance as we run using check_mode=true
91+
assert:
92+
that:
93+
- 'in_test_vpc_instance.instances.0.key_name == "{{ resource_prefix }}_test_key"'
94+
- '(in_test_vpc_instance.instances.0.network_interfaces | length) == 1'
95+
6396
- name: "Add a second interface"
6497
ec2_instance:
6598
state: present
@@ -75,9 +108,25 @@
75108
wait: false
76109
register: add_interface
77110
until: add_interface is not failed
78-
ignore_errors: yes
111+
ignore_errors: true
79112
retries: 10
80113

114+
- name: Validate that the instance has now 2 interfaces attached
115+
block:
116+
- name: "Gather {{ resource_prefix }}-test-eni-vpc info"
117+
ec2_instance_info:
118+
filters:
119+
"tag:Name": '{{ resource_prefix }}-test-eni-vpc'
120+
register: in_test_vpc_instance
121+
122+
- name: Validate that only 1 ENI is attached to instance as we run using check_mode=true
123+
assert:
124+
that:
125+
- 'in_test_vpc_instance.instances.0.key_name == "{{ resource_prefix }}_test_key"'
126+
- '(in_test_vpc_instance.instances.0.network_interfaces | length) == 2'
127+
128+
when: add_interface is successful
129+
81130
- name: "Make instance in the testing subnet created in the test VPC(check mode)"
82131
ec2_instance:
83132
state: present

0 commit comments

Comments
 (0)