summaryrefslogtreecommitdiff
path: root/test/integration/targets/elb_target_info/playbooks/roles/elb_target_info/tasks/main.yml
blob: 6faa2abb003fbf1903e985ccb2c9f5cabac40277 (plain)
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
---
  - name: set up elb_target_info test prerequisites
    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:
      debug: msg="********** Setting up elb_target_info test dependencies **********"

    - name: Find AMI to use
      ec2_ami_info:
        owners: 'amazon'
        filters:
          name: '{{ ec2_ami_name }}'
      register: ec2_amis
    - set_fact:
        ec2_ami_image: '{{ ec2_amis.images[0].image_id }}'

    # ============================================================

    - 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_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)
      register: alb_target_group
      elb_target_group:
        name: "{{ tg_name }}-inst"
        health_check_port: 80
        protocol: http
        port: 80
        vpc_id: '{{ vpc.vpc.id }}'
        state: present
        target_type: instance
        # set this to 30 to test polling for changes, instead of having everything go out immediately
        deregistration_delay_timeout: 30
        tags:
          Description: "Created by {{ resource_prefix }}"

    - name: set up testing target group (type=ip)
      register: nlb_target_group
      elb_target_group:
        name: "{{ tg_name }}-ip"
        health_check_port: 80
        protocol: tcp
        port: 80
        vpc_id: '{{ vpc.vpc.id }}'
        state: present
        # set this to 30 to test polling for changes, instead of having everything go out immediately
        deregistration_delay_timeout: 30
        target_type: ip
        tags:
          Description: "Created by {{ resource_prefix }}"

    - name: set up testing target group which will not be associated with any load balancers
      register: idle_target_group
      elb_target_group:
        name: "{{ tg_name }}-idle"
        health_check_port: 80
        protocol: tcp
        port: 80
        vpc_id: '{{ vpc.vpc.id }}'
        state: present
        target_type: instance
        tags:
          Description: "Created by {{ resource_prefix }}"

    - name: set up ec2 instance to use as a target
      ec2:
        group_id: "{{ sg.group_id }}"
        instance_type: t2.micro
        image: "{{ ec2_ami_image }}"
        vpc_subnet_id: "{{ subnet_2.subnet.id }}"
        instance_tags:
          Name: "{{ resource_prefix }}-inst"
        exact_count: 1
        count_tag:
          Name: "{{ resource_prefix }}-inst"
        assign_public_ip: true
        volumes: []
        wait: true
        ebs_optimized: false
        user_data: |
          #cloud-config
          package_upgrade: true
          package_update: true
          packages:
            - httpd
          runcmd:
            - "service httpd start"
            - echo "HELLO ANSIBLE" > /var/www/html/index.html
      register: ec2

    - name: create an application load balancer
      elb_application_lb:
        name: "{{ lb_name }}-alb"
        security_groups:
          - "{{ sg.group_id }}"
        subnets:
          - "{{ subnet_1.subnet.id }}"
          - "{{ subnet_2.subnet.id }}"
        listeners:
          - Protocol: HTTP
            Port: 80
            DefaultActions:
              - Type: forward
                TargetGroupName: "{{ tg_name }}-inst"
        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 }}-ip"
        state: present

    - name: register with the ALB
      elb_target:
        target_group_name: "{{ tg_name }}-inst"
        target_id: "{{ ec2.instance_ids[0] }}"
        state: present
        target_status: "initial"

    - name: register with the NLB IP target group
      elb_target:
        target_group_name: "{{ tg_name }}-ip"
        target_id: "{{ ec2.instances[0].private_ip }}"
        state: present
        target_status: "initial"

    # ============================================================

    - debug: msg="********** Running elb_target_info integration tests **********"

    # ============================================================
    - name: gather facts
      elb_target_info:
        instance_id: "{{ ec2.instance_ids[0]}}"
      register: target_facts

    - assert:
        that:
          - "{{ alb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn')) }}"
          - "{{ nlb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn')) }}"
          - "{{ idle_target_group.target_group_arn not in (target_facts.instance_target_groups | map(attribute='target_group_arn')) }}"
          - (target_facts.instance_target_groups | length) == 2
        msg: "target facts showed the target in the right target groups"


    - name: register with unused target group
      elb_target:
        target_group_name: "{{ tg_name }}-idle"
        target_id: "{{ ec2.instance_ids[0]}}"
        state: present
        target_status: "unused"

    - name: gather facts again, including the idle group
      elb_target_info:
        instance_id: "{{ ec2.instance_ids[0]}}"
      register: target_facts

    - assert:
        that:
          - "{{ alb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn')) }}"
          - "{{ nlb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn')) }}"
          - "{{ idle_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn')) }}"
          - (target_facts.instance_target_groups | length) == 3
        msg: "target facts reflected the addition of the target to the idle group"

    - name: gather facts again, this time excluding the idle group
      elb_target_info:
        instance_id: "{{ ec2.instance_ids[0]}}"
        get_unused_target_groups: false
      register: target_facts

    - assert:
        that:
          - "{{ alb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn')) }}"
          - "{{ nlb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn')) }}"
          - "{{ idle_target_group.target_group_arn not in (target_facts.instance_target_groups | map(attribute='target_group_arn')) }}"
          - (target_facts.instance_target_groups | length) == 2
        msg: "target_facts.instance_target_groups did not gather unused target groups when variable was set"

    - name: register twice in the same target group
      elb_target:
        target_group_name: "{{ tg_name }}-ip"
        target_port: 22
        target_id: "{{ ec2.instances[0].private_ip }}"
        state: present
        target_status: "healthy"
        target_status_timeout: 400

    - name: gather facts
      elb_target_info:
        instance_id: "{{ ec2.instance_ids[0] }}"
        get_unused_target_groups: false
      register: target_facts

    - assert:
        that:
          - alb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))
          - nlb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))
          - (target_facts.instance_target_groups | length) == 2
          - (target_facts.instance_target_groups |
             selectattr('target_group_arn', 'equalto', nlb_target_group.target_group_arn) |
             map(attribute='targets') |
             flatten |
             list |
             length) == 2
        msg: "registering a target twice didn't affect the overall target group count, increased target count"

    - set_fact:
        original_target_groups: "{{ target_facts.instance_target_groups }}"

    - name: Deregister instance from all target groups
      elb_target:
          target_group_arn: "{{ item.0.target_group_arn }}"
          target_port: "{{ item.1.target_port }}"
          target_az: "{{ item.1.target_az }}"
          target_id: "{{ item.1.target_id }}"
          state: absent
          target_status: "draining"
      with_subelements:
        - "{{ original_target_groups }}"
        - "targets"

    - name: wait for all targets to deregister simultaneously
      elb_target_info:
        get_unused_target_groups: false
        instance_id: "{{ ec2.instance_ids[0] }}"
      register: target_facts
      until: (target_facts.instance_target_groups | length) == 0
      retries: 60
      delay: 10

    - name: reregister in elbv2s
      elb_target:
        target_group_arn: "{{ item.0.target_group_arn }}"
        target_port: "{{ item.1.target_port }}"
        target_az: "{{ item.1.target_az }}"
        target_id: "{{ item.1.target_id }}"
        state: present
        target_status: "initial"
      with_subelements:
        - "{{ original_target_groups }}"
        - "targets"

    # wait until all groups associated with this instance are 'healthy' or
    # 'unused'
    - name: wait for registration
      elb_target_info:
        get_unused_target_groups: false
        instance_id: "{{ ec2.instance_ids[0] }}"
      register: target_facts
      until: >
                (target_facts.instance_target_groups |
                 map(attribute='targets') |
                 flatten |
                 map(attribute='target_health') |
                 rejectattr('state', 'equalto', 'healthy') |
                 rejectattr('state', 'equalto', 'unused') |
                 list |
                 length) == 0
      retries: 61
      delay: 10

    - assert:
        that:
          - alb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))
          - nlb_target_group.target_group_arn in (target_facts.instance_target_groups | map(attribute='target_group_arn'))
          - (target_facts.instance_target_groups | length) == 2
          - (target_facts.instance_target_groups |
             selectattr('target_group_arn', 'equalto', nlb_target_group.target_group_arn) |
             map(attribute='targets') |
             flatten |
             list |
             length) == 2
        msg: "reregistration completed successfully"

    always:

    - name:
      debug: msg="********** Tearing down elb_target_info test dependencies **********"

    - name: remove ec2 instance
      ec2:
        group_id: "{{ sg.group_id }}"
        instance_type: t2.micro
        image: "{{ ec2_ami_image }}"
        vpc_subnet_id: "{{ subnet_2.subnet.id }}"
        instance_tags:
          Name: "{{ resource_prefix }}-inst"
        exact_count: 0
        count_tag:
          Name: "{{ resource_prefix }}-inst"
        assign_public_ip: true
        volumes: []
        wait: true
        ebs_optimized: false
      ignore_errors: true

    - name: remove application load balancer
      elb_application_lb:
        name: "{{ lb_name }}-alb"
        security_groups:
          - "{{ sg.group_id }}"
        subnets:
          - "{{ subnet_1.subnet.id }}"
          - "{{ subnet_2.subnet.id }}"
        listeners:
          - Protocol: HTTP
            Port: 80
            DefaultActions:
              - Type: forward
                TargetGroupName: "{{ tg_name }}-inst"
        state: absent
        wait: true
        wait_timeout: 200
      register: removed
      retries: 10
      until: removed is not failed
      ignore_errors: true

    - name: remove NLB
      ignore_errors: true
      elb_network_lb:
        name: "{{ lb_name }}-nlb"
        state: absent

    - name: remove testing target groups
      elb_target_group:
        name: "{{ item }}"
        health_check_port: 80
        protocol: http
        port: 80
        vpc_id: '{{ vpc.vpc.id }}'
        state: absent
        target_type: instance
        tags:
          Description: "Created by {{ resource_prefix }}"
        wait: true
        wait_timeout: 200
      register: removed
      retries: 10
      until: removed is not failed
      with_items:
        - "{{ tg_name }}-idle"
        - "{{ tg_name }}-ip"
        - "{{ tg_name }}-inst"
      ignore_errors: true

    - name: remove testing security group
      ec2_group:
        state: absent
        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: removed
      retries: 10
      until: removed is not failed
      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"
        resource_tags:
          Name: "{{ resource_prefix }}-subnet"
      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.64.0/18
        az: "{{ aws_region }}b"
        resource_tags:
          Name: "{{ resource_prefix }}-subnet"
      register: removed
      retries: 10
      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"
        state: absent
        cidr_block: 20.0.0.0/16
        tags:
          Name: "{{ resource_prefix }}-vpc"
          Description: "Created by ansible-test"
      register: removed
      retries: 10
      until: removed is not failed

    # ============================================================