Skip to content

Commit b426b98

Browse files
authored
ec2_eni: minor fix: check if private_ip is provided (ansible-collections#540)
ec2_eni: minor fix: check if private_ip is provided SUMMARY Add condition to check if private_ip_address is provided before checking if the address is within the subnet's range to avoid failure on tasks without private_ip_address. ISSUE TYPE Bugfix Pull Request COMPONENT NAME ec2_eni ADDITIONAL INFORMATION Few integration tests in other modules such as ec2_instance have tasks that use ec2_eni without a private_ip_address parameter causing it to fail. This check will provide a fix for the bug. Reviewed-by: Mark Chappell <None> Reviewed-by: Alina Buzachis <None> Reviewed-by: None <None>
1 parent 9c4d31c commit b426b98

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

plugins/modules/ec2_eni.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,11 @@ def create_eni(connection, vpc_id, module):
445445
args["TagSpecifications"] = boto3_tag_specifications(tags, types='network-interface')
446446

447447
# check if provided private_ip_address is within the subnet's address range
448-
cidr_block = connection.describe_subnets(SubnetIds=[str(subnet_id)])['Subnets'][0]['CidrBlock']
449-
valid_private_ip = ip_address(private_ip_address) in ip_network(cidr_block)
450-
if not valid_private_ip:
451-
module.fail_json(changed=False, msg="Error: cannot create ENI - Address does not fall within the subnet's address range.")
448+
if private_ip_address:
449+
cidr_block = connection.describe_subnets(SubnetIds=[str(subnet_id)])['Subnets'][0]['CidrBlock']
450+
valid_private_ip = ip_address(private_ip_address) in ip_network(cidr_block)
451+
if not valid_private_ip:
452+
module.fail_json(changed=False, msg="Error: cannot create ENI - Address does not fall within the subnet's address range.")
452453
if module.check_mode:
453454
module.exit_json(changed=True, msg="Would have created ENI if not in check mode.")
454455

tests/integration/targets/ec2_eni/tasks/main.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
loop:
114114
- "{{ eni_id_1 | default(omit) }}"
115115
- "{{ eni_id_2 | default(omit) }}"
116+
- "{{ eni_id_3 | default(omit) }}"
116117

117118
- name: terminate the instances
118119
ec2_instance:

tests/integration/targets/ec2_eni/tasks/test_eni_basic_creation.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,21 @@
243243
- eni_id_2 in ( eni_info.network_interfaces | selectattr('id') | map(attribute='id') | list )
244244
- ec2_ips[0] in ( eni_info.network_interfaces | map(attribute='private_ip_addresses') | flatten | map(attribute='private_ip_address') | list )
245245
- ec2_ips[1] in ( eni_info.network_interfaces | map(attribute='private_ip_addresses') | flatten | map(attribute='private_ip_address') | list )
246+
247+
248+
# =========================================================
249+
250+
- name: create another network interface without private_ip_address
251+
ec2_eni:
252+
device_index: 1
253+
subnet_id: "{{ vpc_subnet_id }}"
254+
state: present
255+
register: result_no_private_ip
256+
257+
- assert:
258+
that:
259+
- result_no_private_ip.changed
260+
261+
- name: save the third network interface ID for cleanup
262+
set_fact:
263+
eni_id_3: "{{ result_no_private_ip.interface.id }}"

0 commit comments

Comments
 (0)