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