diff --git a/plugins/modules/ec2_eni.py b/plugins/modules/ec2_eni.py index 87f3ae90aa0..c2364a2a268 100644 --- a/plugins/modules/ec2_eni.py +++ b/plugins/modules/ec2_eni.py @@ -445,10 +445,11 @@ def create_eni(connection, vpc_id, module): args["TagSpecifications"] = boto3_tag_specifications(tags, types='network-interface') # check if provided private_ip_address is within the subnet's address range - cidr_block = connection.describe_subnets(SubnetIds=[str(subnet_id)])['Subnets'][0]['CidrBlock'] - valid_private_ip = ip_address(private_ip_address) in ip_network(cidr_block) - if not valid_private_ip: - module.fail_json(changed=False, msg="Error: cannot create ENI - Address does not fall within the subnet's address range.") + if private_ip_address: + cidr_block = connection.describe_subnets(SubnetIds=[str(subnet_id)])['Subnets'][0]['CidrBlock'] + valid_private_ip = ip_address(private_ip_address) in ip_network(cidr_block) + if not valid_private_ip: + module.fail_json(changed=False, msg="Error: cannot create ENI - Address does not fall within the subnet's address range.") if module.check_mode: module.exit_json(changed=True, msg="Would have created ENI if not in check mode.") diff --git a/tests/integration/targets/ec2_eni/tasks/main.yaml b/tests/integration/targets/ec2_eni/tasks/main.yaml index c615365200e..7c2b5308f4c 100644 --- a/tests/integration/targets/ec2_eni/tasks/main.yaml +++ b/tests/integration/targets/ec2_eni/tasks/main.yaml @@ -113,6 +113,7 @@ loop: - "{{ eni_id_1 | default(omit) }}" - "{{ eni_id_2 | default(omit) }}" + - "{{ eni_id_3 | default(omit) }}" - name: terminate the instances ec2_instance: diff --git a/tests/integration/targets/ec2_eni/tasks/test_eni_basic_creation.yaml b/tests/integration/targets/ec2_eni/tasks/test_eni_basic_creation.yaml index 42735a9a831..774b97dcf9c 100644 --- a/tests/integration/targets/ec2_eni/tasks/test_eni_basic_creation.yaml +++ b/tests/integration/targets/ec2_eni/tasks/test_eni_basic_creation.yaml @@ -243,3 +243,21 @@ - eni_id_2 in ( eni_info.network_interfaces | selectattr('id') | map(attribute='id') | list ) - ec2_ips[0] in ( eni_info.network_interfaces | map(attribute='private_ip_addresses') | flatten | map(attribute='private_ip_address') | list ) - ec2_ips[1] in ( eni_info.network_interfaces | map(attribute='private_ip_addresses') | flatten | map(attribute='private_ip_address') | list ) + + +# ========================================================= + +- name: create another network interface without private_ip_address + ec2_eni: + device_index: 1 + subnet_id: "{{ vpc_subnet_id }}" + state: present + register: result_no_private_ip + +- assert: + that: + - result_no_private_ip.changed + +- name: save the third network interface ID for cleanup + set_fact: + eni_id_3: "{{ result_no_private_ip.interface.id }}"