Skip to content

Commit

Permalink
ec2_eni: minor fix: check if private_ip is provided (#540)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mandar242 authored Oct 22, 2021
1 parent 9c4d31c commit b426b98
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
9 changes: 5 additions & 4 deletions plugins/modules/ec2_eni.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/ec2_eni/tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"

0 comments on commit b426b98

Please sign in to comment.