| 1 |
--- |
| 2 |
- name: Setup credentials and region |
| 3 |
module_defaults: |
| 4 |
group/aws: |
| 5 |
access_key: "{{ aws_access_key }}" |
| 6 |
secret_key: "{{ aws_secret_key }}" |
| 7 |
session_token: "{{ security_token | default(omit) }}" |
| 8 |
region: "{{ aws_region }}" |
| 9 |
|
| 10 |
block: |
| 11 |
# Set up the testing dependencies: VPC, subnet, security group, and two launch configurations |
| 12 |
- name: Create VPC for use in testing |
| 13 |
amazon.aws.ec2_vpc_net: |
| 14 |
name: "{{ vpc_name }}" |
| 15 |
cidr_block: "{{ subnet_a_cidr }}" |
| 16 |
tenancy: default |
| 17 |
register: testing_vpc |
| 18 |
|
| 19 |
- name: Create internet gateway for use in testing |
| 20 |
amazon.aws.ec2_vpc_igw: |
| 21 |
vpc_id: "{{ testing_vpc.vpc.id }}" |
| 22 |
state: present |
| 23 |
register: igw |
| 24 |
|
| 25 |
- name: Create subnet for use in testing |
| 26 |
amazon.aws.ec2_vpc_subnet: |
| 27 |
state: present |
| 28 |
vpc_id: "{{ testing_vpc.vpc.id }}" |
| 29 |
cidr: "{{ subnet_a_cidr }}" |
| 30 |
az: "{{ aws_region }}a" |
| 31 |
resource_tags: |
| 32 |
Name: "{{ subnet_name }}" |
| 33 |
register: testing_subnet |
| 34 |
|
| 35 |
- name: Create routing rules |
| 36 |
amazon.aws.ec2_vpc_route_table: |
| 37 |
vpc_id: "{{ testing_vpc.vpc.id }}" |
| 38 |
tags: |
| 39 |
created: "{{ route_name }}" |
| 40 |
routes: |
| 41 |
- dest: 0.0.0.0/0 |
| 42 |
gateway_id: "{{ igw.gateway_id }}" |
| 43 |
subnets: |
| 44 |
- "{{ testing_subnet.subnet.id }}" |
| 45 |
|
| 46 |
- name: Create a security group with the vpc created in the ec2_setup |
| 47 |
amazon.aws.ec2_security_group: |
| 48 |
name: "{{ sg_name }}" |
| 49 |
description: a security group for ansible tests |
| 50 |
vpc_id: "{{ testing_vpc.vpc.id }}" |
| 51 |
rules: |
| 52 |
- proto: tcp |
| 53 |
from_port: 22 |
| 54 |
to_port: 22 |
| 55 |
cidr_ip: 0.0.0.0/0 |
| 56 |
- proto: tcp |
| 57 |
from_port: 80 |
| 58 |
to_port: 80 |
| 59 |
cidr_ip: 0.0.0.0/0 |
| 60 |
register: sg |
| 61 |
|
| 62 |
- name: Ensure launch configs exist |
| 63 |
community.aws.autoscaling_launch_config: |
| 64 |
name: "{{ item }}" |
| 65 |
assign_public_ip: true |
| 66 |
image_id: "{{ ec2_ami_id }}" |
| 67 |
user_data: | |
| 68 |
package_upgrade: true |
| 69 |
package_update: true |
| 70 |
packages: |
| 71 |
- httpd |
| 72 |
runcmd: |
| 73 |
- "service httpd start" |
| 74 |
security_groups: "{{ sg.group_id }}" |
| 75 |
instance_type: t3.micro |
| 76 |
loop: |
| 77 |
- "{{ lc_name_1 }}" |
| 78 |
- "{{ lc_name_2 }}" |
| 79 |
|
| 80 |
- name: Launch asg and do not wait for instances to be deemed healthy (no ELB) |
| 81 |
amazon.aws.autoscaling_group: |
| 82 |
name: "{{ asg_name }}" |
| 83 |
launch_config_name: "{{ lc_name_1 }}" |
| 84 |
desired_capacity: 1 |
| 85 |
min_size: 1 |
| 86 |
max_size: 1 |
| 87 |
vpc_zone_identifier: "{{ testing_subnet.subnet.id }}" |
| 88 |
wait_for_instances: false |
| 89 |
state: present |
| 90 |
register: output |
| 91 |
|
| 92 |
- name: Assert that there is no viable instance |
| 93 |
ansible.builtin.assert: |
| 94 |
that: |
| 95 |
- "output.viable_instances == 0" |
| 96 |
|
| 97 |
# ============================================================ |
| 98 |
- name: Run test with start_cancel_instance_refresh.yml |
| 99 |
ansible.builtin.include_tasks: start_cancel_instance_refresh.yml |
| 100 |
|
| 101 |
# ============================================================ |
| 102 |
|
| 103 |
- name: Run test with refresh_and_cancel_three_times.yml |
| 104 |
ansible.builtin.include_tasks: refresh_and_cancel_three_times.yml |
| 105 |
loop: "{{ query('sequence', 'start=1 end=3') }}" |
| 106 |
|
| 107 |
- name: Run test with instance_refresh_info.yml |
| 108 |
ansible.builtin.include_tasks: instance_refresh_info.yml |
| 109 |
|
| 110 |
always: |
| 111 |
- name: Kill asg |
| 112 |
amazon.aws.autoscaling_group: |
| 113 |
name: "{{ asg_name }}" |
| 114 |
state: absent |
| 115 |
register: removed |
| 116 |
until: removed is not failed |
| 117 |
ignore_errors: true |
| 118 |
retries: 10 |
| 119 |
# Remove the testing dependencies |
| 120 |
|
| 121 |
- name: Remove the load balancer |
| 122 |
amazon.aws.elb_classic_lb: |
| 123 |
name: "{{ load_balancer_name }}" |
| 124 |
state: absent |
| 125 |
security_group_ids: |
| 126 |
- "{{ sg.group_id }}" |
| 127 |
subnets: "{{ testing_subnet.subnet.id }}" |
| 128 |
wait: true |
| 129 |
connection_draining_timeout: 60 |
| 130 |
listeners: |
| 131 |
- protocol: http |
| 132 |
load_balancer_port: 80 |
| 133 |
instance_port: 80 |
| 134 |
health_check: |
| 135 |
ping_protocol: tcp |
| 136 |
ping_port: 80 |
| 137 |
ping_path: "/" |
| 138 |
response_timeout: 5 |
| 139 |
interval: 10 |
| 140 |
unhealthy_threshold: 4 |
| 141 |
healthy_threshold: 2 |
| 142 |
register: removed |
| 143 |
until: removed is not failed |
| 144 |
ignore_errors: true |
| 145 |
retries: 10 |
| 146 |
|
| 147 |
- name: Remove launch configs |
| 148 |
community.aws.autoscaling_launch_config: |
| 149 |
name: "{{ item }}" |
| 150 |
state: absent |
| 151 |
register: removed |
| 152 |
until: removed is not failed |
| 153 |
ignore_errors: true |
| 154 |
retries: 10 |
| 155 |
loop: |
| 156 |
- "{{ lc_name_1 }}" |
| 157 |
- "{{ lc_name_2 }}" |
| 158 |
|
| 159 |
- name: Remove the security group |
| 160 |
amazon.aws.ec2_security_group: |
| 161 |
name: "{{ sg_name }}" |
| 162 |
description: a security group for ansible tests |
| 163 |
vpc_id: "{{ testing_vpc.vpc.id }}" |
| 164 |
state: absent |
| 165 |
register: removed |
| 166 |
until: removed is not failed |
| 167 |
ignore_errors: true |
| 168 |
retries: 10 |
| 169 |
|
| 170 |
- name: Remove routing rules |
| 171 |
amazon.aws.ec2_vpc_route_table: |
| 172 |
state: absent |
| 173 |
vpc_id: "{{ testing_vpc.vpc.id }}" |
| 174 |
tags: |
| 175 |
created: "{{ route_name }}" |
| 176 |
routes: |
| 177 |
- dest: 0.0.0.0/0 |
| 178 |
gateway_id: "{{ igw.gateway_id }}" |
| 179 |
subnets: |
| 180 |
- "{{ testing_subnet.subnet.id }}" |
| 181 |
register: removed |
| 182 |
until: removed is not failed |
| 183 |
ignore_errors: true |
| 184 |
retries: 10 |
| 185 |
|
| 186 |
- name: Remove internet gateway |
| 187 |
amazon.aws.ec2_vpc_igw: |
| 188 |
vpc_id: "{{ testing_vpc.vpc.id }}" |
| 189 |
state: absent |
| 190 |
register: removed |
| 191 |
until: removed is not failed |
| 192 |
ignore_errors: true |
| 193 |
retries: 10 |
| 194 |
|
| 195 |
- name: Remove the subnet |
| 196 |
amazon.aws.ec2_vpc_subnet: |
| 197 |
state: absent |
| 198 |
vpc_id: "{{ testing_vpc.vpc.id }}" |
| 199 |
cidr: "{{ subnet_a_cidr }}" |
| 200 |
register: removed |
| 201 |
until: removed is not failed |
| 202 |
ignore_errors: true |
| 203 |
retries: 10 |
| 204 |
|
| 205 |
- name: Remove the VPC |
| 206 |
amazon.aws.ec2_vpc_net: |
| 207 |
name: "{{ vpc_name }}" |
| 208 |
cidr_block: "{{ subnet_a_cidr }}" |
| 209 |
state: absent |
| 210 |
register: removed |
| 211 |
until: removed is not failed |
| 212 |
ignore_errors: true |
| 213 |
retries: 10 |