Debian Codegraph @

ansible_collections/community/aws/tests/integration/targets/elb_target_info/tasks/main.yml

raw
1 ---
2 - name: set up elb_target_info test prerequisites
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 collections:
10 - amazon.aws
11
12 block:
13
14 # ============================================================
15
16 - name:
17 debug: msg="********** Setting up elb_target_info test dependencies **********"
18
19 - name: set up testing VPC
20 ec2_vpc_net:
21 name: "{{ resource_prefix }}-vpc"
22 state: present
23 cidr_block: 20.0.0.0/16
24 tags:
25 Name: "{{ resource_prefix }}-vpc"
26 Description: "Created by ansible-test"
27 register: vpc
28
29 - name: set up testing internet gateway
30 ec2_vpc_igw:
31 vpc_id: "{{ vpc.vpc.id }}"
32 state: present
33 register: igw
34
35 - name: set up testing subnet
36 ec2_vpc_subnet:
37 state: present
38 vpc_id: "{{ vpc.vpc.id }}"
39 cidr: 20.0.0.0/18
40 az: "{{ aws_region }}a"
41 resource_tags:
42 Name: "{{ resource_prefix }}-subnet"
43 register: subnet_1
44
45 - name: set up testing subnet
46 ec2_vpc_subnet:
47 state: present
48 vpc_id: "{{ vpc.vpc.id }}"
49 cidr: 20.0.64.0/18
50 az: "{{ aws_region }}b"
51 resource_tags:
52 Name: "{{ resource_prefix }}-subnet"
53 register: subnet_2
54
55 - name: create routing rules
56 ec2_vpc_route_table:
57 vpc_id: "{{ vpc.vpc.id }}"
58 tags:
59 created: "{{ resource_prefix }}-route"
60 routes:
61 - dest: 0.0.0.0/0
62 gateway_id: "{{ igw.gateway_id }}"
63 subnets:
64 - "{{ subnet_1.subnet.id }}"
65 - "{{ subnet_2.subnet.id }}"
66 register: route_table
67
68 - name: create testing security group
69 ec2_security_group:
70 name: "{{ resource_prefix }}-sg"
71 description: a security group for ansible tests
72 vpc_id: "{{ vpc.vpc.id }}"
73 rules:
74 - proto: tcp
75 from_port: 80
76 to_port: 80
77 cidr_ip: 0.0.0.0/0
78 - proto: tcp
79 from_port: 22
80 to_port: 22
81 cidr_ip: 0.0.0.0/0
82 register: sg
83
84 - name: set up testing target group (type=instance)
85 register: alb_target_group
86 elb_target_group:
87 name: "{{ tg_name }}-inst"
88 health_check_port: 80
89 protocol: http
90 port: 80
91 vpc_id: '{{ vpc.vpc.id }}'
92 state: present
93 target_type: instance
94 # set this to 30 to test polling for changes, instead of having everything go out immediately
95 deregistration_delay_timeout: 30
96 tags:
97 Description: "Created by {{ resource_prefix }}"
98
99 - name: set up testing target group (type=ip)
100 register: nlb_target_group
101 elb_target_group:
102 name: "{{ tg_name }}-ip"
103 health_check_port: 80
104 protocol: tcp
105 port: 80
106 vpc_id: '{{ vpc.vpc.id }}'
107 state: present
108 # set this to 30 to test polling for changes, instead of having everything go out immediately
109 deregistration_delay_timeout: 30
110 target_type: ip
111 tags:
112 Description: "Created by {{ resource_prefix }}"
113
114 - name: set up testing target group which will not be associated with any load balancers
115 register: idle_target_group
116 elb_target_group:
117 name: "{{ tg_name }}-idle"
118 health_check_port: 80
119 protocol: tcp
120 port: 80
121 vpc_id: '{{ vpc.vpc.id }}'
122 state: present
123 target_type: instance
124 tags:
125 Description: "Created by {{ resource_prefix }}"
126
127 - name: set up ec2 instance to use as a target
128 ec2_instance:
129 name: "{{ resource_prefix }}-inst"
130 security_group: "{{ sg.group_id }}"
131 instance_type: t3.micro
132 image_id: "{{ ec2_ami_id }}"
133 vpc_subnet_id: "{{ subnet_2.subnet.id }}"
134 network:
135 assign_public_ip: true
136 volumes: []
137 wait: true
138 ebs_optimized: false
139 user_data: |
140 #cloud-config
141 package_upgrade: true
142 package_update: true
143 packages:
144 - httpd
145 runcmd:
146 - "service httpd start"
147 - echo "HELLO ANSIBLE" > /var/www/html/index.html
148 register: ec2
149
150 - set_fact:
151 instance_id: "{{ ec2.instances[0].instance_id }}"
152
153 - name: create an application load balancer
154 elb_application_lb:
155 name: "{{ lb_name }}-alb"
156 security_groups:
157 - "{{ sg.group_id }}"
158 subnets:
159 - "{{ subnet_1.subnet.id }}"
160 - "{{ subnet_2.subnet.id }}"
161 listeners:
162 - Protocol: HTTP
163 Port: 80
164 DefaultActions:
165 - Type: forward
166 TargetGroupName: "{{ tg_name }}-inst"
167 state: present
168
169
170 - name: create a network load balancer
171 elb_network_lb:
172 name: "{{ lb_name }}-nlb"
173 subnets:
174 - "{{ subnet_1.subnet.id }}"
175 - "{{ subnet_2.subnet.id }}"
176 listeners:
177 - Protocol: TCP
178 Port: 80
179 DefaultActions:
180 - Type: forward
181 TargetGroupName: "{{ tg_name }}-ip"
182 state: present
183
184 - name: register with the ALB
185 elb_target:
186 target_group_name: "{{ tg_name }}-inst"
187 target_id: "{{ instance_id }}"
188 state: present
189 target_status: "initial"
190
191 - name: register with the NLB IP target group
192 elb_target:
193 target_group_name: "{{ tg_name }}-ip"
194 target_id: "{{ ec2.instances[0].private_ip_address }}"
195 state: present
196 target_status: "initial"
197
198 # ============================================================
199
200 - debug: msg="********** Running elb_target_info integration tests **********"
201
202 # ============================================================
203 - name: gather facts
204 elb_target_info:
205 instance_id: "{{ instance_id }}"
206 register: target_facts
207
208 - assert:
209 that:
210 - "alb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))"
211 - "nlb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))"
212 - "idle_target_group.target_group_arn not in (target_facts.instance_target_groups | map(attribute='target_group_arn'))"
213 - (target_facts.instance_target_groups | length) == 2
214 msg: "target facts showed the target in the right target groups"
215
216
217 - name: register with unused target group
218 elb_target:
219 target_group_name: "{{ tg_name }}-idle"
220 target_id: "{{ instance_id }}"
221 state: present
222 target_status: "unused"
223
224 - name: gather facts again, including the idle group
225 elb_target_info:
226 instance_id: "{{ instance_id }}"
227 register: target_facts
228
229 - assert:
230 that:
231 - "alb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))"
232 - "nlb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))"
233 - "idle_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))"
234 - (target_facts.instance_target_groups | length) == 3
235 msg: "target facts reflected the addition of the target to the idle group"
236
237 - name: gather facts again, this time excluding the idle group
238 elb_target_info:
239 instance_id: "{{ instance_id }}"
240 get_unused_target_groups: false
241 register: target_facts
242
243 - assert:
244 that:
245 - "alb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))"
246 - "nlb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))"
247 - "idle_target_group.target_group_arn not in (target_facts.instance_target_groups | map(attribute='target_group_arn'))"
248 - (target_facts.instance_target_groups | length) == 2
249 msg: "target_facts.instance_target_groups did not gather unused target groups when variable was set"
250
251 - name: register twice in the same target group
252 elb_target:
253 target_group_name: "{{ tg_name }}-ip"
254 target_port: 22
255 target_id: "{{ ec2.instances[0].private_ip_address }}"
256 state: present
257 target_status: "healthy"
258 target_status_timeout: 400
259
260 - name: gather facts
261 elb_target_info:
262 instance_id: "{{ instance_id }}"
263 get_unused_target_groups: false
264 register: target_facts
265
266 - assert:
267 that:
268 - alb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))
269 - nlb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))
270 - (target_facts.instance_target_groups | length) == 2
271 - (target_facts.instance_target_groups |
272 selectattr('target_group_arn', 'equalto', nlb_target_group.target_group_arn) |
273 map(attribute='targets') |
274 flatten |
275 list |
276 length) == 2
277 msg: "registering a target twice didn't affect the overall target group count, increased target count"
278
279 - set_fact:
280 original_target_groups: "{{ target_facts.instance_target_groups }}"
281
282 - name: Deregister instance from all target groups
283 elb_target:
284 target_group_arn: "{{ item.0.target_group_arn }}"
285 target_port: "{{ item.1.target_port }}"
286 target_az: "{{ item.1.target_az }}"
287 target_id: "{{ item.1.target_id }}"
288 state: absent
289 target_status: "draining"
290 with_subelements:
291 - "{{ original_target_groups }}"
292 - "targets"
293
294 - name: wait for all targets to deregister simultaneously
295 elb_target_info:
296 get_unused_target_groups: false
297 instance_id: "{{ instance_id }}"
298 register: target_facts
299 until: (target_facts.instance_target_groups | length) == 0
300 retries: 60
301 delay: 10
302
303 - name: reregister in elbv2s
304 elb_target:
305 target_group_arn: "{{ item.0.target_group_arn }}"
306 target_port: "{{ item.1.target_port }}"
307 target_az: "{{ item.1.target_az }}"
308 target_id: "{{ item.1.target_id }}"
309 state: present
310 target_status: "initial"
311 with_subelements:
312 - "{{ original_target_groups }}"
313 - "targets"
314
315 # wait until all groups associated with this instance are 'healthy' or
316 # 'unused'
317 - name: wait for registration
318 elb_target_info:
319 get_unused_target_groups: false
320 instance_id: "{{ instance_id }}"
321 register: target_facts
322 until: >
323 (target_facts.instance_target_groups |
324 map(attribute='targets') |
325 flatten |
326 map(attribute='target_health') |
327 rejectattr('state', 'equalto', 'healthy') |
328 rejectattr('state', 'equalto', 'unused') |
329 list |
330 length) == 0
331 retries: 61
332 delay: 10
333
334 - assert:
335 that:
336 - alb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))
337 - nlb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))
338 - (target_facts.instance_target_groups | length) == 2
339 - (target_facts.instance_target_groups |
340 selectattr('target_group_arn', 'equalto', nlb_target_group.target_group_arn) |
341 map(attribute='targets') |
342 flatten |
343 list |
344 length) == 2
345 msg: "reregistration completed successfully"
346
347 always:
348
349 - name:
350 debug: msg="********** Tearing down elb_target_info test dependencies **********"
351
352 - name: remove ec2 instance
353 ec2_instance:
354 name: "{{ resource_prefix }}-inst"
355 state: absent
356 wait: True
357 ignore_errors: true
358
359 - name: remove application load balancer
360 elb_application_lb:
361 name: "{{ lb_name }}-alb"
362 security_groups:
363 - "{{ sg.group_id }}"
364 subnets:
365 - "{{ subnet_1.subnet.id }}"
366 - "{{ subnet_2.subnet.id }}"
367 listeners:
368 - Protocol: HTTP
369 Port: 80
370 DefaultActions:
371 - Type: forward
372 TargetGroupName: "{{ tg_name }}-inst"
373 state: absent
374 wait: true
375 wait_timeout: 200
376 register: removed
377 retries: 10
378 until: removed is not failed
379 ignore_errors: true
380
381 - name: remove NLB
382 ignore_errors: true
383 elb_network_lb:
384 name: "{{ lb_name }}-nlb"
385 state: absent
386
387 - name: remove testing target groups
388 elb_target_group:
389 name: "{{ item }}"
390 health_check_port: 80
391 protocol: http
392 port: 80
393 vpc_id: '{{ vpc.vpc.id }}'
394 state: absent
395 target_type: instance
396 tags:
397 Description: "Created by {{ resource_prefix }}"
398 wait: true
399 wait_timeout: 200
400 register: removed
401 retries: 10
402 until: removed is not failed
403 with_items:
404 - "{{ tg_name }}-idle"
405 - "{{ tg_name }}-ip"
406 - "{{ tg_name }}-inst"
407 ignore_errors: true
408
409 - name: remove testing security group
410 ec2_security_group:
411 state: absent
412 name: "{{ resource_prefix }}-sg"
413 description: a security group for ansible tests
414 vpc_id: "{{ vpc.vpc.id }}"
415 rules:
416 - proto: tcp
417 from_port: 80
418 to_port: 80
419 cidr_ip: 0.0.0.0/0
420 - proto: tcp
421 from_port: 22
422 to_port: 22
423 cidr_ip: 0.0.0.0/0
424 register: removed
425 retries: 10
426 until: removed is not failed
427 ignore_errors: true
428
429 - name: remove routing rules
430 ec2_vpc_route_table:
431 state: absent
432 lookup: id
433 route_table_id: "{{ route_table.route_table.id }}"
434 register: removed
435 retries: 10
436 until: removed is not failed
437 ignore_errors: true
438
439 - name: remove testing subnet
440 ec2_vpc_subnet:
441 state: absent
442 vpc_id: "{{ vpc.vpc.id }}"
443 cidr: 20.0.0.0/18
444 az: "{{ aws_region }}a"
445 resource_tags:
446 Name: "{{ resource_prefix }}-subnet"
447 register: removed
448 retries: 10
449 until: removed is not failed
450 ignore_errors: true
451
452 - name: remove testing subnet
453 ec2_vpc_subnet:
454 state: absent
455 vpc_id: "{{ vpc.vpc.id }}"
456 cidr: 20.0.64.0/18
457 az: "{{ aws_region }}b"
458 resource_tags:
459 Name: "{{ resource_prefix }}-subnet"
460 register: removed
461 retries: 10
462 until: removed is not failed
463 ignore_errors: true
464
465 - name: remove testing internet gateway
466 ec2_vpc_igw:
467 vpc_id: "{{ vpc.vpc.id }}"
468 state: absent
469 register: removed
470 retries: 10
471 until: removed is not failed
472 ignore_errors: true
473
474 - name: remove testing VPC
475 ec2_vpc_net:
476 name: "{{ resource_prefix }}-vpc"
477 state: absent
478 cidr_block: 20.0.0.0/16
479 tags:
480 Name: "{{ resource_prefix }}-vpc"
481 Description: "Created by ansible-test"
482 register: removed
483 retries: 10
484 until: removed is not failed
485 ignore_errors: True
486
487 # ============================================================