Debian Codegraph @

ansible_collections/community/aws/tests/integration/targets/elb_target/tasks/ec2_target.yml

raw
1 ---
2 - name: set up ec2 based test prerequisites
3 block:
4 # ============================================================
5
6 - name:
7 debug: msg="********** Setting up elb_target EC2 test dependencies **********"
8
9 # ============================================================
10
11 - name: set up testing VPC
12 ec2_vpc_net:
13 name: "{{ resource_prefix }}-vpc"
14 state: present
15 cidr_block: 20.0.0.0/16
16 tags:
17 Name: "{{ resource_prefix }}-vpc"
18 Description: "Created by ansible-test"
19 register: vpc
20
21 - name: set up testing internet gateway
22 ec2_vpc_igw:
23 vpc_id: "{{ vpc.vpc.id }}"
24 state: present
25 register: igw
26
27 - name: set up testing subnet
28 ec2_vpc_subnet:
29 state: present
30 vpc_id: "{{ vpc.vpc.id }}"
31 cidr: 20.0.0.0/18
32 az: "{{ aws_region }}a"
33 resource_tags:
34 Name: "{{ resource_prefix }}-subnet"
35 register: subnet_1
36
37 - name: set up testing subnet
38 ec2_vpc_subnet:
39 state: present
40 vpc_id: "{{ vpc.vpc.id }}"
41 cidr: 20.0.64.0/18
42 az: "{{ aws_region }}b"
43 resource_tags:
44 Name: "{{ resource_prefix }}-subnet"
45 register: subnet_2
46
47 - name: create routing rules
48 ec2_vpc_route_table:
49 vpc_id: "{{ vpc.vpc.id }}"
50 tags:
51 created: "{{ resource_prefix }}-route"
52 routes:
53 - dest: 0.0.0.0/0
54 gateway_id: "{{ igw.gateway_id }}"
55 subnets:
56 - "{{ subnet_1.subnet.id }}"
57 - "{{ subnet_2.subnet.id }}"
58 register: route_table
59
60 - name: create testing security group
61 ec2_security_group:
62 name: "{{ resource_prefix }}-sg"
63 description: a security group for ansible tests
64 vpc_id: "{{ vpc.vpc.id }}"
65 rules:
66 - proto: tcp
67 from_port: 80
68 to_port: 80
69 cidr_ip: 0.0.0.0/0
70 - proto: tcp
71 from_port: 22
72 to_port: 22
73 cidr_ip: 0.0.0.0/0
74 register: sg
75
76 - name: set up testing target group (type=instance)
77 elb_target_group:
78 name: "{{ tg_name }}"
79 health_check_port: 80
80 protocol: http
81 port: 80
82 vpc_id: "{{ vpc.vpc.id }}"
83 state: present
84 target_type: instance
85 tags:
86 Description: "Created by {{ resource_prefix }}"
87
88 - name: set up testing target group (type=instance) with UDP protocol
89 elb_target_group:
90 name: "{{ tg_tcpudp_name }}"
91 protocol: udp
92 port: 53
93 vpc_id: "{{ vpc.vpc.id }}"
94 state: present
95 target_type: instance
96 tags:
97 Protocol: "UDP"
98 Description: "Created by {{ resource_prefix }}"
99
100 - name: set up testing target group for ALB (type=instance)
101 elb_target_group:
102 name: "{{ tg_used_name }}"
103 health_check_port: 80
104 protocol: http
105 port: 80
106 vpc_id: "{{ vpc.vpc.id }}"
107 state: present
108 target_type: instance
109 tags:
110 Description: "Created by {{ resource_prefix }}"
111 register: result
112
113 - name: set up testing target group for ALB (type=instance)
114 assert:
115 that:
116 - result.changed
117 - result.target_group_name == tg_used_name
118 - result.target_type == 'instance'
119 - result.vpc_id == vpc.vpc.id
120 - result.port == 80
121 - result.load_balancing_algorithm_type == 'round_robin' # default
122 - '"health_check_port" in result'
123 - '"tags" in result'
124
125 - name: set up testing target group for NLB (type=instance)
126 elb_target_group:
127 name: "{{ tg_name }}-nlb"
128 health_check_port: 80
129 protocol: tcp
130 port: 80
131 vpc_id: "{{ vpc.vpc.id }}"
132 state: present
133 target_type: instance
134 deregistration_delay_timeout: 60
135 deregistration_connection_termination: yes
136 tags:
137 Description: "Created by {{ resource_prefix }}"
138 register: result
139
140 - name: set up testing target group for NLB (type=instance)
141 assert:
142 that:
143 - result.changed
144 - '"health_check_port" in result'
145 - result.port == 80
146 - '"health_check_protocol" in result'
147 - result.health_check_protocol == 'TCP'
148 - '"tags" in result'
149 - '"target_group_arn" in result'
150 - result.target_group_name == tg_name ~ '-nlb'
151 - result.target_type == 'instance'
152 - result.deregistration_delay_timeout_seconds == '60'
153 - result.deregistration_delay_connection_termination_enabled == "true"
154 - result.vpc_id == vpc.vpc.id
155
156 - name: set up ec2 instance to use as a target
157 amazon.aws.ec2_instance:
158 name: "{{ resource_prefix }}-inst"
159 security_group: "{{ sg.group_id }}"
160 instance_type: t3.micro
161 image_id: "{{ ec2_ami_id }}"
162 vpc_subnet_id: "{{ subnet_2.subnet.id }}"
163 network:
164 assign_public_ip: true
165 volumes: []
166 wait: true
167 ebs_optimized: false
168 tags:
169 Name: "{{ resource_prefix }}-inst"
170 user_data: |
171 #!/bin/bash
172 sudo nohup python3 -m http.server 80 &
173 register: ec2
174
175 - set_fact:
176 instance_id: "{{ ec2.instances[0].instance_id }}"
177
178 - name: create an application load balancer
179 elb_application_lb:
180 name: "{{ lb_name }}"
181 security_groups:
182 - "{{ sg.group_id }}"
183 subnets:
184 - "{{ subnet_1.subnet.id }}"
185 - "{{ subnet_2.subnet.id }}"
186 listeners:
187 - Protocol: HTTP
188 Port: 80
189 DefaultActions:
190 - Type: forward
191 TargetGroupName: "{{ tg_used_name }}"
192 state: present
193
194 - name: create a network load balancer
195 elb_network_lb:
196 name: "{{ lb_name }}-nlb"
197 subnets:
198 - "{{ subnet_1.subnet.id }}"
199 - "{{ subnet_2.subnet.id }}"
200 listeners:
201 - Protocol: TCP
202 Port: 80
203 DefaultActions:
204 - Type: forward
205 TargetGroupName: "{{ tg_name }}-nlb"
206 state: present
207 register: result
208
209 - name: create a netwok load balancer
210 assert:
211 that:
212 - result.changed
213 - '"created_time" in result'
214 - '"load_balancer_arn" in result'
215 - '"tags" in result'
216 - result.type == 'network'
217 - result.vpc_id == vpc.vpc.id
218
219 - name: modify up testing target group for NLB (preserve_client_ip_enabled=false)
220 elb_target_group:
221 name: "{{ tg_name }}-nlb"
222 health_check_port: 80
223 protocol: tcp
224 port: 80
225 vpc_id: "{{ vpc.vpc.id }}"
226 state: present
227 target_type: instance
228 modify_targets: true
229 preserve_client_ip_enabled: false
230 tags:
231 Description: "Created by {{ resource_prefix }}"
232 register: result
233
234 - name: modify up testing target group for NLB (preserve_client_ip_enabled=false)
235 assert:
236 that:
237 - result.changed
238 - result.preserve_client_ip_enabled == 'false'
239 - result.proxy_protocol_v2_enabled == 'false'
240
241 - name: modify up testing target group for NLB (proxy_protocol_v2_enabled=true)
242 elb_target_group:
243 name: "{{ tg_name }}-nlb"
244 health_check_port: 80
245 protocol: tcp
246 port: 80
247 vpc_id: "{{ vpc.vpc.id }}"
248 state: present
249 target_type: instance
250 modify_targets: true
251 proxy_protocol_v2_enabled: true
252 tags:
253 Description: "Created by {{ resource_prefix }}"
254 register: result
255
256 - name: modify up testing target group for NLB (proxy_protocol_v2_enabled=true)
257 assert:
258 that:
259 - result.changed
260 - result.proxy_protocol_v2_enabled == 'true'
261 - result.preserve_client_ip_enabled == 'false'
262
263 - name: (idempotence) modify up testing target group for NLB (preserve_client_ip_enabled=false and proxy_protocol_v2_enabled=true)
264 elb_target_group:
265 name: "{{ tg_name }}-nlb"
266 health_check_port: 80
267 protocol: tcp
268 port: 80
269 vpc_id: "{{ vpc.vpc.id }}"
270 state: present
271 target_type: instance
272 modify_targets: true
273 preserve_client_ip_enabled: false
274 proxy_protocol_v2_enabled: true
275 tags:
276 Description: "Created by {{ resource_prefix }}"
277 register: result
278
279 - name: (idempotence) modify up testing target group for NLB (preserve_client_ip_enabled=false and proxy_protocol_v2_enabled=true)
280 assert:
281 that:
282 - not result.changed
283 - result.proxy_protocol_v2_enabled == 'true'
284 - result.preserve_client_ip_enabled == 'false'
285
286 # Modify target group atrributes =============================
287 # TODO: Add check_mode support
288
289 - name: update target group atrributes
290 elb_target_group:
291 name: "{{ tg_used_name }}"
292 health_check_port: 80
293 protocol: http
294 port: 80
295 vpc_id: "{{ vpc.vpc.id }}"
296 load_balancing_algorithm_type: least_outstanding_requests
297 state: present
298 target_type: instance
299 tags:
300 Description: "Created by {{ resource_prefix }}"
301 register: result
302
303 - name: update target group atrributes
304 assert:
305 that:
306 - result.changed
307 - result.target_group_name == tg_used_name
308 - result.load_balancing_algorithm_type == 'least_outstanding_requests'
309
310 - name: update target group atrributes - idempotence
311 elb_target_group:
312 name: "{{ tg_used_name }}"
313 health_check_port: 80
314 protocol: http
315 port: 80
316 vpc_id: "{{ vpc.vpc.id }}"
317 load_balancing_algorithm_type: least_outstanding_requests
318 state: present
319 target_type: instance
320 tags:
321 Description: "Created by {{ resource_prefix }}"
322 register: result
323
324 - name: update target group atrributes - idempotence
325 assert:
326 that:
327 - not result.changed
328 - result.load_balancing_algorithm_type == 'least_outstanding_requests'
329
330 # ============================================================
331
332 - name:
333 debug: msg="********** Running elb_target integration tests **********"
334
335 # ============================================================
336
337 - name: register an instance to unused target group
338 elb_target:
339 target_group_name: "{{ tg_name }}"
340 target_id: "{{ instance_id }}"
341 state: present
342 register: result
343
344 - name: target is registered
345 assert:
346 that:
347 - result.changed
348 - result.target_group_arn is defined
349 - result.target_group_arn != ""
350 - result.target_health_descriptions.target.id == instance_id
351
352 # ============================================================
353
354 - name: test idempotence
355 elb_target:
356 target_group_name: "{{ tg_name }}"
357 target_id: "{{ instance_id }}"
358 state: present
359 register: result
360
361 - name: target was already registered
362 assert:
363 that:
364 - not result.changed
365
366 # ============================================================
367
368 - name: remove an unused target
369 elb_target:
370 target_group_name: "{{ tg_name }}"
371 target_id: "{{ instance_id }}"
372 state: absent
373 deregister_unused: true
374 register: result
375
376 - name: target group was deleted
377 assert:
378 that:
379 - result.changed
380 - not result.target_health_descriptions
381
382 # ============================================================
383
384 - name: register an instance to used target group and wait until healthy
385 elb_target:
386 target_group_name: "{{ tg_used_name }}"
387 target_id: "{{ instance_id }}"
388 state: present
389 target_status: healthy
390 target_status_timeout: 400
391 register: result
392
393 - name: target is registered
394 assert:
395 that:
396 - result.changed
397 - result.target_group_arn is defined
398 - result.target_group_arn != ""
399 - result.target_health_descriptions.target.id == instance_id
400 - result.target_health_descriptions.target_health == healthy_state
401
402 # ============================================================
403
404 - name: remove a target from used target group
405 elb_target:
406 target_group_name: "{{ tg_used_name }}"
407 target_id: "{{ instance_id }}"
408 state: absent
409 target_status: unused
410 target_status_timeout: 400
411 register: result
412
413 - name: target was deregistered
414 assert:
415 that:
416 - result.changed
417
418 # ============================================================
419
420 - name: test idempotence
421 elb_target:
422 target_group_name: "{{ tg_used_name }}"
423 target_id: "{{ instance_id }}"
424 state: absent
425 register: result
426
427 - name: target was already deregistered
428 assert:
429 that:
430 - not result.changed
431
432 # ============================================================
433
434 - name: register an instance to used target group and wait until healthy again to test deregistering differently
435 elb_target:
436 target_group_name: "{{ tg_used_name }}"
437 target_id: "{{ instance_id }}"
438 state: present
439 target_status: healthy
440 target_status_timeout: 400
441 register: result
442
443 - name: target is registered
444 assert:
445 that:
446 - result.changed
447 - result.target_group_arn is defined
448 - result.target_group_arn != ""
449 - result.target_health_descriptions.target.id == instance_id
450 - result.target_health_descriptions.target_health == healthy_state
451
452 - name: start deregisteration but don't wait
453 elb_target:
454 target_group_name: "{{ tg_used_name }}"
455 target_id: "{{ instance_id }}"
456 state: absent
457 register: result
458
459 - name: target is starting to deregister
460 assert:
461 that:
462 - result.changed
463 - result.target_health_descriptions.target_health.reason == "Target.DeregistrationInProgress"
464
465 - name: now wait for target to finish deregistering
466 elb_target:
467 target_group_name: "{{ tg_used_name }}"
468 target_id: "{{ instance_id }}"
469 state: absent
470 target_status: unused
471 target_status_timeout: 400
472 register: result
473
474 - name: target was deregistered already and now has finished
475 assert:
476 that:
477 - not result.changed
478 - not result.target_health_descriptions
479
480 - name: create ip target group
481 elb_target_group:
482 name: "{{ tg_name }}-ip"
483 health_check_port: 443
484 protocol: tcp
485 port: 443
486 vpc_id: "{{ vpc.vpc.id }}"
487 state: present
488 target_type: ip
489 register: result
490
491 - name: ip target group must be created
492 assert:
493 that:
494 - result.changed
495 - result.target_type == 'ip'
496
497 - name: "mobify ip target group with AvailabilityZone: all"
498 elb_target_group:
499 name: "{{ tg_name }}-ip"
500 health_check_port: 443
501 protocol: tcp
502 port: 443
503 vpc_id: "{{ vpc.vpc.id }}"
504 state: present
505 target_type: ip
506 wait: false
507 modify_targets: true
508 targets:
509 - Id: 192.168.178.32
510 Port: 443
511 AvailabilityZone: all
512 register: result
513
514 - name: ip target group must be modified
515 assert:
516 that:
517 - result.changed
518 - result.load_balancing_cross_zone_enabled == 'use_load_balancer_configuration'
519
520 # ============================================================
521
522 always:
523 - name:
524 debug: msg="********** Tearing down elb_target test dependencies **********"
525
526 - name: remove ec2 instance
527 amazon.aws.ec2_instance:
528 instance_ids:
529 - "{{ instance_id }}"
530 state: absent
531 wait: True
532 register: removed
533 retries: 10
534 until: removed is not failed
535 ignore_errors: true
536
537 - name: remove application load balancer
538 elb_application_lb:
539 name: "{{ lb_name }}"
540 state: absent
541 wait: true
542 wait_timeout: 600
543 register: removed
544 retries: 10
545 until: removed is not failed
546 ignore_errors: true
547
548 - name: remove network load balancer
549 elb_network_lb:
550 name: "{{ lb_name }}-nlb"
551 state: absent
552 wait: true
553 wait_timeout: 600
554 register: removed
555 retries: 10
556 until: removed is not failed
557 ignore_errors: true
558
559 - name: remove testing target groups
560 elb_target_group:
561 name: "{{ item }}"
562 state: absent
563 wait: true
564 wait_timeout: 600
565 register: removed
566 retries: 10
567 until: removed is not failed
568 with_items:
569 - "{{ tg_name }}"
570 - "{{ tg_used_name }}"
571 - "{{ tg_tcpudp_name }}"
572 - "{{ tg_name }}-nlb"
573 - "{{ tg_name }}-ip"
574 ignore_errors: true
575
576 - name: remove routing rules
577 ec2_vpc_route_table:
578 state: absent
579 lookup: id
580 route_table_id: "{{ route_table.route_table.id }}"
581 register: removed
582 retries: 10
583 until: removed is not failed
584 ignore_errors: true
585
586 - name: remove testing subnet
587 ec2_vpc_subnet:
588 state: absent
589 vpc_id: "{{ vpc.vpc.id }}"
590 cidr: 20.0.0.0/18
591 az: "{{ aws_region }}a"
592 register: removed
593 retries: 15
594 until: removed is not failed
595 ignore_errors: true
596
597 - name: remove testing subnet
598 ec2_vpc_subnet:
599 state: absent
600 vpc_id: "{{ vpc.vpc.id }}"
601 cidr: 20.0.64.0/18
602 az: "{{ aws_region }}b"
603 register: removed
604 retries: 15
605 until: removed is not failed
606 ignore_errors: true
607
608 - name: remove testing security group
609 ec2_security_group:
610 state: absent
611 name: "{{ resource_prefix }}-sg"
612 register: removed
613 retries: 15
614 until: removed is not failed
615 ignore_errors: true
616
617 - name: remove testing internet gateway
618 ec2_vpc_igw:
619 vpc_id: "{{ vpc.vpc.id }}"
620 state: absent
621 register: removed
622 retries: 10
623 until: removed is not failed
624 ignore_errors: true
625
626 - name: remove testing VPC
627 ec2_vpc_net:
628 name: "{{ resource_prefix }}-vpc"
629 cidr_block: 20.0.0.0/16
630 state: absent
631 register: removed
632 retries: 10
633 until: removed is not failed
634 ignore_errors: true
635
636 # ============================================================