generated from ansible-collections/collection_template
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
297 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
cloud/aws | ||
role/ec2_instance_create |
17 changes: 17 additions & 0 deletions
17
tests/integration/targets/test_ec2_instance_create/defaults/main.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
aws_security_token: "{{ security_token | default(omit) }}" | ||
resource_prefix: mandkulkt1 | ||
|
||
# VPC and Subnet Configuration | ||
vpc_name: "{{ resource_prefix }}-vpc" | ||
test_vpc_name: 'vpc-{{ resource_prefix }}' | ||
test_vpc_cidr: '101.{{ 255 | random(seed=resource_prefix) }}.0.0/16' | ||
test_subnet_cidr: '101.{{ 255 | random(seed=resource_prefix) }}.0.0/24' | ||
|
||
# EC2 Instance Configuration | ||
ec2_instance_type: t2.micro | ||
ec2_key_name: "{{ resource_prefix }}-ec2-key" # SSH key name for EC2 instances | ||
|
||
# External Security Group Configuration | ||
external_sg_name: "{{ resource_prefix }}-external-sg" | ||
external_sg_description: "External Security Group for EC2" |
21 changes: 21 additions & 0 deletions
21
tests/integration/targets/test_ec2_instance_create/tasks/main.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
- name: Integration tests for ec2_networking_resources role | ||
module_defaults: | ||
group/aws: | ||
aws_access_key: "{{ aws_access_key }}" | ||
aws_secret_key: "{{ aws_secret_key }}" | ||
security_token: "{{ security_token | default(omit) }}" | ||
region: "{{ aws_region }}" | ||
block: | ||
- name: Create resources required for test | ||
include_tasks: setup.yml | ||
|
||
- name: Run tests for case 1 - EC2 with no external sg, igw, eip | ||
include_tasks: tasks/test_ec2_only.yml | ||
|
||
- name: Run tests for case 2 - EC2 with external sg, igw, eip | ||
include_tasks: tasks/test_ec2_with_igw_sg_eip.yml | ||
|
||
always: | ||
- name: Delete resources required for test | ||
ansible.builtin.include_tasks: teardown.yml |
49 changes: 49 additions & 0 deletions
49
tests/integration/targets/test_ec2_instance_create/tasks/setup.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
- name: Setup | ||
block: | ||
- name: Get AMI image ID using filters | ||
amazon.aws.ec2_ami_info: | ||
region: "{{ aws_region }}" | ||
filters: | ||
architecture: x86_64 | ||
# CentOS Community Platform Engineering (CPE) | ||
owner-id: "125523088429" | ||
virtualization-type: hvm | ||
root-device-type: ebs | ||
name: Fedora-Cloud-Base-* | ||
register: images | ||
# very spammy | ||
no_log: true | ||
|
||
- name: Create vpc to work in | ||
amazon.aws.ec2_vpc_net: | ||
cidr_block: "{{ test_vpc_cidr }}" | ||
name: "{{ test_vpc_name }}" | ||
state: present | ||
region: "{{ aws_region }}" | ||
register: vpc | ||
|
||
- name: Define VPC id | ||
ansible.builtin.set_fact: | ||
test_vpc_id: "{{ vpc.vpc.id }}" | ||
|
||
- name: Create EC2 subnet | ||
amazon.aws.ec2_vpc_subnet: | ||
vpc_id: "{{ test_vpc_id }}" | ||
cidr: "{{ test_subnet_cidr }}" | ||
az: "{{ aws_region }}a" | ||
region: "{{ aws_region }}" | ||
register: subnet | ||
|
||
- name: Create a key | ||
amazon.aws.ec2_key: | ||
name: "{{ ec2_key_name }}" | ||
state: present | ||
region: "{{ aws_region }}" | ||
register: ec2_key_result | ||
|
||
- name: Set facts for test resources | ||
ansible.builtin.set_fact: | ||
image_id: "ami-0bcda2433f3dabc41" #"{{ images.images.0.image_id }}" | ||
subnet_id: "{{ subnet.subnet.id }}" | ||
vpc_id: "{{ vpc.vpc.id }}" |
25 changes: 25 additions & 0 deletions
25
tests/integration/targets/test_ec2_instance_create/tasks/teardown.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
- name: Teardown | ||
block: | ||
- name: Delete Subnets | ||
amazon.aws.ec2_vpc_subnet: | ||
vpc_id: "{{ test_vpc_id }}" | ||
cidr: "{{ test_subnet_cidr }}" | ||
region: "{{ aws_region }}" | ||
state: absent | ||
ignore_errors: true | ||
|
||
- name: Delete a VPC | ||
amazon.aws.ec2_vpc_net: | ||
cidr_block: "{{ test_vpc_cidr }}" | ||
vpc_id: "{{ test_vpc_id }}" | ||
region: "{{ aws_region }}" | ||
state: absent | ||
ignore_errors: true | ||
|
||
- name: Delete a key | ||
amazon.aws.ec2_key: | ||
name: "{{ resource_prefix }}-ec2-key" | ||
region: "{{ aws_region }}" | ||
state: absent | ||
ignore_errors: true |
44 changes: 44 additions & 0 deletions
44
tests/integration/targets/test_ec2_instance_create/tasks/test_ec2_only.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
- block: | ||
- name: Create EC2 instance with no external SG, no IGW, no EIP | ||
ansible.builtin.include_role: | ||
name: cloud.aws_ops.ec2_instance_create | ||
vars: | ||
ec2_instance_create_aws_region: "{{ aws_region }}" | ||
ec2_instance_create_instance_name: "only-ec2-{{ resource_prefix }}" | ||
ec2_instance_create_instance_type: "{{ ec2_instance_type }}" | ||
ec2_instance_create_ami_id: "{{ image_id }}" | ||
ec2_instance_create_vpc_subnet_id: "{{ subnet_id }}" | ||
ec2_instance_create_key_name: "{{ ec2_key_name }}" | ||
ec2_instance_create_associate_external_sg: false | ||
ec2_instance_create_associate_eip: false | ||
ec2_instance_create_associate_igw: false | ||
ec2_instance_create_tags: | ||
Environment: Testing | ||
Name: "{{ resource_prefix }}-instance" | ||
|
||
- name: Get EC2 instance info | ||
amazon.aws.ec2_instance_info: | ||
filters: | ||
"tag:Name": "only-ec2-{{ resource_prefix }}" | ||
register: _ec2_instance | ||
until: _ec2_instance.instances[0].state.name == 'running' | ||
retries: 12 | ||
delay: 5 | ||
|
||
- name: Validate EC2 creation (no SG, no IGW, no EIP) | ||
ansible.builtin.assert: | ||
that: | ||
- _ec2_instance.instances | length == 1 | ||
- _ec2_instance.instances[0].state.name == 'running' | ||
- _ec2_instance.instances[0].tags.Name == "only-ec2-{{ resource_prefix }}" | ||
- _ec2_instance.instances[0].network_interfaces[0].groups[0].group_name == "default" | ||
- _ec2_instance.instances[0].key_name == ec2_key_name | ||
|
||
always: | ||
- name: Terminate EC2 instance | ||
amazon.aws.ec2_instance: | ||
state: absent | ||
instance_ids: "{{ _ec2_instance.instances[0].instance_id }}" | ||
wait: true | ||
ignore_errors: true |
107 changes: 107 additions & 0 deletions
107
tests/integration/targets/test_ec2_instance_create/tasks/test_ec2_with_igw_sg_eip.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
--- | ||
- block: | ||
- name: Create EC2 instance with no external SG, no IGW, no EIP | ||
ansible.builtin.include_role: | ||
name: cloud.aws_ops.ec2_instance_create | ||
vars: | ||
ec2_instance_create_aws_region: "{{ aws_region }}" | ||
ec2_instance_create_instance_name: "ec2-all-enabled-{{ resource_prefix }}" | ||
ec2_instance_create_instance_type: "{{ ec2_instance_type }}" | ||
ec2_instance_create_ami_id: "{{ image_id }}" | ||
ec2_instance_create_vpc_subnet_id: "{{ subnet_id }}" | ||
ec2_instance_create_key_name: "{{ ec2_key_name }}" | ||
ec2_instance_create_vpc_id: "{{ vpc_id }}" | ||
ec2_instance_create_tags: | ||
Environment: Testing | ||
Name: "{{ resource_prefix }}-instance" | ||
|
||
# Optional: external security group | ||
ec2_instance_create_associate_external_sg: true | ||
ec2_instance_create_external_sg_name: "{{ external_sg_name }}" | ||
ec2_instance_create_external_sg_description: "{{ external_sg_description }}" | ||
ec2_instance_create_external_sg_rules: | ||
- proto: tcp | ||
ports: 22 | ||
cidr_ip: 10.0.1.0/16 | ||
- proto: tcp | ||
ports: 8000 | ||
cidr_ip: 10.0.1.0/16 | ||
ec2_instance_create_sg_tags: | ||
Environment: Testing | ||
Name: "{{ resource_prefix }}-sg" | ||
|
||
# Optional: EIP | ||
ec2_instance_create_associate_eip: true | ||
ec2_instance_create_eip_tags: | ||
Environment: Testing | ||
Name: "{{ resource_prefix }}-eip" | ||
|
||
# Optional: Internet Gateway | ||
ec2_instance_create_associate_igw: true | ||
ec2_instance_create_igw_tags: | ||
Environment: Testing | ||
Name: "{{ resource_prefix }}-igw" | ||
|
||
- name: Get EC2 instance info | ||
amazon.aws.ec2_instance_info: | ||
filters: | ||
"tag:Name": "ec2-all-enabled-{{ resource_prefix }}" | ||
register: _ec2_instance | ||
until: _ec2_instance.instances[0].state.name == 'running' | ||
retries: 12 | ||
delay: 5 | ||
|
||
- name: Gather information about Internet Gateway | ||
amazon.aws.ec2_vpc_igw_info: | ||
filters: | ||
"tag:Name": "{{ resource_prefix }}-igw" | ||
register: igw_info | ||
|
||
- name: Gather information about security group | ||
amazon.aws.ec2_security_group_info: | ||
filters: | ||
"tag:Name": "{{ resource_prefix }}-sg" | ||
register: sg_info | ||
|
||
- name: Gather information about route table | ||
amazon.aws.ec2_vpc_route_table_info: | ||
filters: | ||
vpc-id: "{{ vpc_id }}" | ||
register: rtb_info | ||
|
||
- name: Validate EC2 creation (SG, IGW, EIP) | ||
ansible.builtin.assert: | ||
that: | ||
- _ec2_instance.instances | length == 1 | ||
- _ec2_instance.instances[0].state.name == 'running' | ||
- _ec2_instance.instances[0].tags.Name == "ec2-all-enabled-{{ resource_prefix }}" | ||
- _ec2_instance.instances[0].network_interfaces[0].groups[0].group_name == "{{ external_sg_name }}" | ||
- _ec2_instance.instances[0].key_name == ec2_key_name | ||
|
||
always: | ||
- name: Terminate EC2 instance | ||
amazon.aws.ec2_instance: | ||
state: absent | ||
instance_ids: "{{ _ec2_instance.instances[0].instance_id }}" | ||
wait: true | ||
ignore_errors: true | ||
|
||
- name: Delete Internet gateway ensuring attached VPC is correct | ||
amazon.aws.ec2_vpc_igw: | ||
state: absent | ||
internet_gateway_id: "{{ igw_info.internet_gateways[0].internet_gateway_id }}" | ||
vpc_id: "{{ vpc_id }}" | ||
ignore_errors: true | ||
|
||
- name: Delete security group | ||
amazon.aws.ec2_security_group: | ||
group_id: "{{ sg_info.security_groups[0].group_id }}" | ||
state: absent | ||
ignore_errors: true | ||
|
||
- name: Delete route table | ||
amazon.aws.ec2_vpc_route_table: | ||
vpc_id: "{{ vpc_id }}" | ||
route_table_id: "{{ rtb_info.route_tables[0].id }}" | ||
lookup: id | ||
state: absent |