mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
[cloud] Add IPv6 support for ec2_vpc_subnet module(#30444)
* Add integration test suite for ec2_vpc_subnet * wrap boto3 connection in try/except update module documentation and add RETURN docs add IPv6 support to VPC subnet module rename ipv6cidr to ipv6_cidr, use required_if for parameter testing, update some failure messages to be more descriptive DryRun mode was removed from this function a while ago but exception handling was still checking for it, removed add wait and timeout for subnet creation process fixup the ipv6 cidr disassociation logic a bit per review update RETURN values per review added module parameter check removed DryRun parameter from boto3 call since it would always be false here fix subnet wait loop add a purge_tags parameter, fix the ensure_tags function, update to use compare_aws_tags func fix tags type error per review remove **kwargs use in create_subnet function per review * rebased on #31870, fixed merge conflicts, and updated error messages * fixes to pass tests * add test for failure on invalid ipv6 block and update tags test for purge_tags=true function * fix pylint issue * fix exception handling error when run with python3 * add ipv6 tests and fix module code * Add permissions to hacking/aws_config/testing_policies/ec2-policy.json for adding IPv6 cidr blocks to VPC and subnets * fix type in tests and update assert conditional to check entire returned value * add AWS_SESSION_TOKEN into environment for aws cli commands to work in CI * remove key and value options from call to boto3_tag_list_to_ansible_dict * remove wait loop and use boto3 EC2 waiter * remove unused register: result vars * revert az argument default value to original setting default=None
This commit is contained in:
parent
23b1dbacaf
commit
cfbe9c8aee
3 changed files with 499 additions and 87 deletions
|
@ -16,7 +16,7 @@
|
|||
name: "{{ resource_prefix }}-vpc"
|
||||
state: present
|
||||
cidr_block: "10.232.232.128/26"
|
||||
region: '{{ec2_region}}'
|
||||
region: '{{ ec2_region }}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
|
@ -33,7 +33,7 @@
|
|||
tags:
|
||||
Name: '{{ec2_vpc_subnet_name}}'
|
||||
Description: '{{ec2_vpc_subnet_description}}'
|
||||
region: '{{ec2_region}}'
|
||||
region: '{{ ec2_region }}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
|
@ -56,20 +56,41 @@
|
|||
tags:
|
||||
Name: '{{ec2_vpc_subnet_name}}'
|
||||
Description: '{{ec2_vpc_subnet_description}}'
|
||||
region: '{{ec2_region}}'
|
||||
region: '{{ ec2_region }}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
state: present
|
||||
register: vpc_subnet_recreate
|
||||
|
||||
- name: assert recreation changed nothing (expected changed=true)
|
||||
- name: assert recreation changed nothing (expected changed=false)
|
||||
assert:
|
||||
that:
|
||||
- 'not vpc_subnet_recreate.changed'
|
||||
- 'vpc_subnet_recreate.subnet.id.startswith("subnet-")'
|
||||
- '"Name" in vpc_subnet_recreate.subnet.tags and vpc_subnet_recreate.subnet.tags["Name"] == ec2_vpc_subnet_name'
|
||||
- '"Description" in vpc_subnet_recreate.subnet.tags and vpc_subnet_recreate.subnet.tags["Description"] == ec2_vpc_subnet_description'
|
||||
- 'vpc_subnet_recreate.subnet == vpc_subnet_create.subnet'
|
||||
|
||||
- name: add invalid ipv6 block to subnet (expected failed)
|
||||
ec2_vpc_subnet:
|
||||
cidr: "10.232.232.128/28"
|
||||
az: "{{ ec2_region }}a"
|
||||
vpc_id: "{{ vpc_result.vpc.id }}"
|
||||
ipv6_cidr: 2001:db8::/64
|
||||
tags:
|
||||
Name: '{{ec2_vpc_subnet_name}}'
|
||||
Description: '{{ec2_vpc_subnet_description}}'
|
||||
region: '{{ec2_region}}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
state: present
|
||||
register: vpc_subnet_ipv6_failed
|
||||
ignore_errors: yes
|
||||
|
||||
- name: assert failure happened (expected failed)
|
||||
assert:
|
||||
that:
|
||||
- 'vpc_subnet_ipv6_failed.failed'
|
||||
- "'Couldn\\'t associate ipv6 cidr' in vpc_subnet_ipv6_failed.msg"
|
||||
|
||||
- name: add a tag (expected changed=true)
|
||||
ec2_vpc_subnet:
|
||||
|
@ -80,7 +101,7 @@
|
|||
Name: '{{ec2_vpc_subnet_name}}'
|
||||
Description: '{{ec2_vpc_subnet_description}}'
|
||||
AnotherTag: SomeValue
|
||||
region: '{{ec2_region}}'
|
||||
region: '{{ ec2_region }}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
|
@ -95,16 +116,14 @@
|
|||
- '"Description" in vpc_subnet_add_a_tag.subnet.tags and vpc_subnet_add_a_tag.subnet.tags["Description"] == ec2_vpc_subnet_description'
|
||||
- '"AnotherTag" in vpc_subnet_add_a_tag.subnet.tags and vpc_subnet_add_a_tag.subnet.tags["AnotherTag"] == "SomeValue"'
|
||||
|
||||
# We may want to change this behaviour by adding purge_tags to the module
|
||||
# and setting it to false by default
|
||||
- name: remove tags (expected changed=true)
|
||||
- name: remove tags with default purge_tags=true (expected changed=true)
|
||||
ec2_vpc_subnet:
|
||||
cidr: "10.232.232.128/28"
|
||||
az: "{{ ec2_region }}a"
|
||||
vpc_id: "{{ vpc_result.vpc.id }}"
|
||||
tags:
|
||||
AnotherTag: SomeValue
|
||||
region: '{{ec2_region}}'
|
||||
region: '{{ ec2_region }}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
|
@ -119,12 +138,36 @@
|
|||
- '"Description" not in vpc_subnet_remove_tags.subnet.tags'
|
||||
- '"AnotherTag" in vpc_subnet_remove_tags.subnet.tags and vpc_subnet_remove_tags.subnet.tags["AnotherTag"] == "SomeValue"'
|
||||
|
||||
- name: change tags with purge_tags=false (expected changed=true)
|
||||
ec2_vpc_subnet:
|
||||
cidr: "10.232.232.128/28"
|
||||
az: "{{ ec2_region }}a"
|
||||
vpc_id: "{{ vpc_result.vpc.id }}"
|
||||
tags:
|
||||
Name: '{{ec2_vpc_subnet_name}}'
|
||||
Description: '{{ec2_vpc_subnet_description}}'
|
||||
region: '{{ec2_region}}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
state: present
|
||||
purge_tags: false
|
||||
register: vpc_subnet_change_tags
|
||||
|
||||
- name: assert tag addition happened (expected changed=true)
|
||||
assert:
|
||||
that:
|
||||
- 'vpc_subnet_change_tags.changed'
|
||||
- '"Name" in vpc_subnet_change_tags.subnet.tags and vpc_subnet_change_tags.subnet.tags["Name"] == ec2_vpc_subnet_name'
|
||||
- '"Description" in vpc_subnet_change_tags.subnet.tags and vpc_subnet_change_tags.subnet.tags["Description"] == ec2_vpc_subnet_description'
|
||||
- '"AnotherTag" in vpc_subnet_change_tags.subnet.tags and vpc_subnet_change_tags.subnet.tags["AnotherTag"] == "SomeValue"'
|
||||
|
||||
- name: test state=absent (expected changed=true)
|
||||
ec2_vpc_subnet:
|
||||
cidr: "10.232.232.128/28"
|
||||
vpc_id: "{{ vpc_result.vpc.id }}"
|
||||
state: absent
|
||||
region: '{{ec2_region}}'
|
||||
region: '{{ ec2_region }}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
|
@ -166,6 +209,138 @@
|
|||
assert:
|
||||
that:
|
||||
- 'result.changed'
|
||||
|
||||
# FIXME - Replace by creating IPv6 enabled VPC once ec2_vpc_net module supports it.
|
||||
- name: install aws cli - FIXME temporary this should go for a lighterweight solution
|
||||
command: pip install awscli
|
||||
|
||||
- name: Assign an Amazon provided IPv6 CIDR block to the VPC
|
||||
command: aws ec2 associate-vpc-cidr-block --amazon-provided-ipv6-cidr-block --vpc-id '{{ vpc_result.vpc.id }}'
|
||||
environment:
|
||||
AWS_ACCESS_KEY_ID: '{{aws_access_key}}'
|
||||
AWS_SECRET_ACCESS_KEY: '{{aws_secret_key}}'
|
||||
AWS_SESSION_TOKEN: '{{security_token}}'
|
||||
AWS_DEFAULT_REGION: '{{ec2_region}}'
|
||||
|
||||
- name: Get the assigned IPv6 CIDR
|
||||
command: aws ec2 describe-vpcs --vpc-ids '{{ vpc_result.vpc.id }}'
|
||||
environment:
|
||||
AWS_ACCESS_KEY_ID: '{{aws_access_key}}'
|
||||
AWS_SECRET_ACCESS_KEY: '{{aws_secret_key}}'
|
||||
AWS_SESSION_TOKEN: '{{security_token}}'
|
||||
AWS_DEFAULT_REGION: '{{ec2_region}}'
|
||||
register: vpc_ipv6
|
||||
|
||||
- set_fact:
|
||||
vpc_ipv6_cidr: "{{ vpc_ipv6.stdout | from_json | json_query('Vpcs[0].Ipv6CidrBlockAssociationSet[0].Ipv6CidrBlock') }}"
|
||||
|
||||
- name: create subnet with IPv6 (expected changed=true)
|
||||
ec2_vpc_subnet:
|
||||
cidr: "10.232.232.128/28"
|
||||
vpc_id: "{{ vpc_result.vpc.id }}"
|
||||
ipv6_cidr: "{{ vpc_ipv6_cidr | regex_replace('::/56', '::/64') }}"
|
||||
assign_instances_ipv6: true
|
||||
state: present
|
||||
region: '{{ec2_region}}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
tags:
|
||||
Name: '{{ec2_vpc_subnet_name}}'
|
||||
Description: '{{ec2_vpc_subnet_description}}'
|
||||
register: vpc_subnet_ipv6_create
|
||||
|
||||
- name: assert creation with IPv6 happened (expected changed=true)
|
||||
assert:
|
||||
that:
|
||||
- 'vpc_subnet_ipv6_create'
|
||||
- 'vpc_subnet_ipv6_create.subnet.id.startswith("subnet-")'
|
||||
- "vpc_subnet_ipv6_create.subnet.ipv6_cidr_block == '{{ vpc_ipv6_cidr | regex_replace('::/56', '::/64') }}'"
|
||||
- '"Name" in vpc_subnet_ipv6_create.subnet.tags and vpc_subnet_ipv6_create.subnet.tags["Name"] == ec2_vpc_subnet_name'
|
||||
- '"Description" in vpc_subnet_ipv6_create.subnet.tags and vpc_subnet_ipv6_create.subnet.tags["Description"] == ec2_vpc_subnet_description'
|
||||
- 'vpc_subnet_ipv6_create.subnet.assign_ipv6_address_on_creation'
|
||||
|
||||
- name: recreate subnet (expected changed=false)
|
||||
ec2_vpc_subnet:
|
||||
cidr: "10.232.232.128/28"
|
||||
vpc_id: "{{ vpc_result.vpc.id }}"
|
||||
ipv6_cidr: "{{ vpc_ipv6_cidr | regex_replace('::/56', '::/64') }}"
|
||||
assign_instances_ipv6: true
|
||||
region: '{{ec2_region}}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
state: present
|
||||
tags:
|
||||
Name: '{{ec2_vpc_subnet_name}}'
|
||||
Description: '{{ec2_vpc_subnet_description}}'
|
||||
register: vpc_subnet_ipv6_recreate
|
||||
|
||||
- name: assert recreation changed nothing (expected changed=false)
|
||||
assert:
|
||||
that:
|
||||
- 'not vpc_subnet_ipv6_recreate.changed'
|
||||
- 'vpc_subnet_ipv6_recreate.subnet == vpc_subnet_ipv6_create.subnet'
|
||||
|
||||
- name: change subnet ipv6 attribute (expected changed=true)
|
||||
ec2_vpc_subnet:
|
||||
cidr: "10.232.232.128/28"
|
||||
vpc_id: "{{ vpc_result.vpc.id }}"
|
||||
ipv6_cidr: "{{ vpc_ipv6_cidr | regex_replace('::/56', '::/64') }}"
|
||||
assign_instances_ipv6: false
|
||||
region: '{{ec2_region}}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
state: present
|
||||
purge_tags: false
|
||||
register: vpc_change_attribute
|
||||
|
||||
- name: assert assign_instances_ipv6 attribute changed (expected changed=true)
|
||||
assert:
|
||||
that:
|
||||
- 'vpc_change_attribute.changed'
|
||||
- 'not vpc_change_attribute.subnet.assign_ipv6_address_on_creation'
|
||||
|
||||
- name: add second subnet with duplicate ipv6 cidr (expected failure)
|
||||
ec2_vpc_subnet:
|
||||
cidr: "10.232.232.144/28"
|
||||
vpc_id: "{{ vpc_result.vpc.id }}"
|
||||
ipv6_cidr: "{{ vpc_ipv6_cidr | regex_replace('::/56', '::/64') }}"
|
||||
region: '{{ec2_region}}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
state: present
|
||||
purge_tags: false
|
||||
register: vpc_add_duplicate_ipv6
|
||||
ignore_errors: true
|
||||
|
||||
- name: assert graceful failure (expected failed)
|
||||
assert:
|
||||
that:
|
||||
- 'vpc_add_duplicate_ipv6.failed'
|
||||
- "'The IPv6 CIDR \\'{{ vpc_ipv6_cidr | regex_replace('::/56', '::/64') }}\\' conflicts with another subnet' in vpc_add_duplicate_ipv6.msg"
|
||||
|
||||
- name: remove subnet ipv6 cidr (expected changed=true)
|
||||
ec2_vpc_subnet:
|
||||
cidr: "10.232.232.128/28"
|
||||
vpc_id: "{{ vpc_result.vpc.id }}"
|
||||
region: '{{ec2_region}}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
state: present
|
||||
purge_tags: false
|
||||
register: vpc_remove_ipv6_cidr
|
||||
|
||||
- name: assert subnet ipv6 cidr removed (expected changed=true)
|
||||
assert:
|
||||
that:
|
||||
- 'vpc_remove_ipv6_cidr.changed'
|
||||
- "vpc_remove_ipv6_cidr.subnet.ipv6_cidr_block == ''"
|
||||
- 'not vpc_remove_ipv6_cidr.subnet.assign_ipv6_address_on_creation'
|
||||
|
||||
always:
|
||||
|
||||
################################################
|
||||
|
@ -177,7 +352,7 @@
|
|||
cidr: "10.232.232.128/28"
|
||||
vpc_id: "{{ vpc_result.vpc.id }}"
|
||||
state: absent
|
||||
region: '{{ec2_region}}'
|
||||
region: '{{ ec2_region }}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
|
@ -187,7 +362,7 @@
|
|||
name: "{{ resource_prefix }}-vpc"
|
||||
state: absent
|
||||
cidr_block: "10.232.232.128/26"
|
||||
region: '{{ec2_region}}'
|
||||
region: '{{ ec2_region }}'
|
||||
aws_access_key: '{{ aws_access_key }}'
|
||||
aws_secret_key: '{{ aws_secret_key }}'
|
||||
security_token: '{{ security_token }}'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue