Debian Codegraph @

ansible_collections/amazon/aws/tests/integration/targets/ec2_vpc_igw/tasks/main.yml

raw
1 ---
2 - name: Ec2_vpc_igw tests
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 block:
10 # ============================================================
11 - name: Fetch IGWs in check_mode
12 amazon.aws.ec2_vpc_igw_info:
13 register: igw_info
14 check_mode: true
15 - name: Assert success
16 ansible.builtin.assert:
17 that:
18 - igw_info is successful
19 - '"internet_gateways" in igw_info'
20
21 # ============================================================
22 - name: Create a VPC
23 amazon.aws.ec2_vpc_net:
24 name: "{{ vpc_name }}"
25 state: present
26 cidr_block: "{{ vpc_cidr }}"
27 tags:
28 Name: "{{ resource_prefix }}-vpc"
29 Description: Created by ansible-test
30 register: vpc_result
31 - name: Assert success
32 ansible.builtin.assert:
33 that:
34 - vpc_result is successful
35 - '"vpc" in vpc_result'
36 - '"id" in vpc_result.vpc'
37 - vpc_result.vpc.state == 'available'
38 - '"tags" in vpc_result.vpc'
39 - vpc_result.vpc.tags | length == 2
40 - vpc_result.vpc.tags["Name"] == resource_prefix+"-vpc"
41 - vpc_result.vpc.tags["Description"] == "Created by ansible-test"
42
43 # ============================================================
44 - name: Create a second VPC
45 amazon.aws.ec2_vpc_net:
46 name: "{{ vpc_name_2 }}"
47 state: present
48 cidr_block: "{{ vpc_cidr_2 }}"
49 tags:
50 Description: Created by ansible-test
51 register: vpc_2_result
52
53 # ============================================================
54 - name: Search for internet gateway by VPC - no matches
55 amazon.aws.ec2_vpc_igw_info:
56 filters:
57 attachment.vpc-id: "{{ vpc_result.vpc.id }}"
58 register: igw_info
59
60 - name: Assert success
61 ansible.builtin.assert:
62 that:
63 - igw_info is successful
64 - '"internet_gateways" in igw_info'
65 - (igw_info.internet_gateways | length) == 0
66
67 # ============================================================
68 - name: Create internet gateway (expected changed=true) - CHECK_MODE
69 amazon.aws.ec2_vpc_igw:
70 state: present
71 vpc_id: "{{ vpc_result.vpc.id }}"
72 tags:
73 tag_one: "{{ resource_prefix }} One"
74 Tag Two: two {{ resource_prefix }}
75 register: vpc_igw_create
76 check_mode: true
77
78 - name: Assert creation would happen (expected changed=true) - CHECK_MODE
79 ansible.builtin.assert:
80 that:
81 - vpc_igw_create is changed
82
83 - name: Create internet gateway (expected changed=true)
84 amazon.aws.ec2_vpc_igw:
85 state: present
86 vpc_id: "{{ vpc_result.vpc.id }}"
87 tags:
88 tag_one: "{{ resource_prefix }} One"
89 Tag Two: two {{ resource_prefix }}
90 register: vpc_igw_create
91
92 - name: Assert creation happened (expected changed=true)
93 ansible.builtin.assert:
94 that:
95 - "'ec2:CreateTags' not in vpc_igw_create.resource_actions"
96 - "'ec2:DeleteTags' not in vpc_igw_create.resource_actions"
97 - vpc_igw_create is changed
98 - vpc_igw_create.gateway_id.startswith("igw-")
99 - vpc_igw_create.vpc_id == vpc_result.vpc.id
100 - '"tags" in vpc_igw_create'
101 - vpc_igw_create.tags | length == 2
102 - vpc_igw_create.tags["tag_one"] == resource_prefix+' One'
103 - vpc_igw_create.tags["Tag Two"] == 'two '+resource_prefix
104 - '"gateway_id" in vpc_igw_create'
105
106 # ============================================================
107 - name: Save IDs for later
108 ansible.builtin.set_fact:
109 igw_id: "{{ vpc_igw_create.gateway_id }}"
110 vpc_id: "{{ vpc_result.vpc.id }}"
111 vpc_2_id: "{{ vpc_2_result.vpc.id }}"
112
113 - name: Search for internet gateway by VPC
114 amazon.aws.ec2_vpc_igw_info:
115 filters:
116 attachment.vpc-id: "{{ vpc_id }}"
117 convert_tags: false
118 register: igw_info
119
120 - name: Check standard IGW details
121 ansible.builtin.assert:
122 that:
123 - '"internet_gateways" in igw_info'
124 - igw_info.internet_gateways | length == 1
125 - '"attachments" in current_igw'
126 - current_igw.attachments | length == 1
127 - '"state" in current_igw.attachments[0]'
128 - current_igw.attachments[0].state == "available"
129 - '"vpc_id" in current_igw.attachments[0]'
130 - current_igw.attachments[0].vpc_id == vpc_id
131 - '"internet_gateway_id" in current_igw'
132 - current_igw.internet_gateway_id == igw_id
133 - '"tags" in current_igw'
134 - current_igw.tags | length == 2
135 - '"key" in current_igw.tags[0]'
136 - '"value" in current_igw.tags[0]'
137 - '"key" in current_igw.tags[1]'
138 - '"value" in current_igw.tags[1]'
139 # Order isn't guaranteed in boto3 style, so just check the keys and
140 # values we expect are in there.
141 - current_igw.tags[0].key in ["tag_one", "Tag Two"]
142 - current_igw.tags[1].key in ["tag_one", "Tag Two"]
143 - current_igw.tags[0].value in [resource_prefix + " One", "two " + resource_prefix]
144 - current_igw.tags[1].value in [resource_prefix + " One", "two " + resource_prefix]
145 vars:
146 current_igw: "{{ igw_info.internet_gateways[0] }}"
147
148 - name: Fetch IGW by ID
149 amazon.aws.ec2_vpc_igw_info:
150 internet_gateway_ids: "{{ igw_id }}"
151 register: igw_info
152
153 - name: Check standard IGW details
154 ansible.builtin.assert:
155 that:
156 - '"internet_gateways" in igw_info'
157 - igw_info.internet_gateways | length == 1
158 - '"attachments" in current_igw'
159 - current_igw.attachments | length == 1
160 - '"state" in current_igw.attachments[0]'
161 - current_igw.attachments[0].state == "available"
162 - '"vpc_id" in current_igw.attachments[0]'
163 - current_igw.attachments[0].vpc_id == vpc_id
164 - '"internet_gateway_id" in current_igw'
165 - current_igw.internet_gateway_id == igw_id
166 - '"tags" in current_igw'
167 - current_igw.tags | length == 2
168 - '"tag_one" in current_igw.tags'
169 - '"Tag Two" in current_igw.tags'
170 - current_igw.tags["tag_one"] == resource_prefix +' One'
171 - current_igw.tags["Tag Two"] == 'two '+ resource_prefix
172 vars:
173 current_igw: "{{ igw_info.internet_gateways[0] }}"
174
175 - name: Fetch IGW by ID (list)
176 amazon.aws.ec2_vpc_igw_info:
177 internet_gateway_ids:
178 - "{{ igw_id }}"
179 register: igw_info
180
181 - name: Check standard IGW details
182 ansible.builtin.assert:
183 that:
184 - '"internet_gateways" in igw_info'
185 - igw_info.internet_gateways | length == 1
186 - '"attachments" in current_igw'
187 - current_igw.attachments | length == 1
188 - '"state" in current_igw.attachments[0]'
189 - current_igw.attachments[0].state == "available"
190 - '"vpc_id" in current_igw.attachments[0]'
191 - current_igw.attachments[0].vpc_id == vpc_id
192 - '"internet_gateway_id" in current_igw'
193 - current_igw.internet_gateway_id == igw_id
194 - '"tags" in current_igw'
195 vars:
196 current_igw: "{{ igw_info.internet_gateways[0] }}"
197
198 - name: Attempt to recreate internet gateway on VPC (expected changed=false) - CHECK_MODE
199 amazon.aws.ec2_vpc_igw:
200 state: present
201 vpc_id: "{{ vpc_result.vpc.id }}"
202 register: vpc_igw_recreate
203 check_mode: true
204
205 - name: Assert recreation would do nothing (expected changed=false) - CHECK_MODE
206 ansible.builtin.assert:
207 that:
208 - vpc_igw_recreate is not changed
209 - vpc_igw_recreate.gateway_id == igw_id
210 - vpc_igw_recreate.vpc_id == vpc_id
211 - '"tags" in vpc_igw_create'
212 - vpc_igw_create.tags | length == 2
213 - vpc_igw_create.tags["tag_one"] == resource_prefix +' One'
214 - vpc_igw_create.tags["Tag Two"] == 'two '+ resource_prefix
215
216 - name: Attempt to recreate internet gateway on VPC (expected changed=false)
217 amazon.aws.ec2_vpc_igw:
218 state: present
219 vpc_id: "{{ vpc_result.vpc.id }}"
220 register: vpc_igw_recreate
221
222 - name: Assert recreation did nothing (expected changed=false)
223 ansible.builtin.assert:
224 that:
225 - vpc_igw_recreate is not changed
226 - vpc_igw_recreate.gateway_id == igw_id
227 - vpc_igw_recreate.vpc_id == vpc_id
228 - '"tags" in vpc_igw_create'
229 - vpc_igw_create.tags | length == 2
230 - vpc_igw_create.tags["tag_one"] == resource_prefix +' One'
231 - vpc_igw_create.tags["Tag Two"] == 'two '+ resource_prefix
232
233 # ============================================================
234 - name: Update the tags (no change) - CHECK_MODE
235 amazon.aws.ec2_vpc_igw:
236 state: present
237 vpc_id: "{{ vpc_result.vpc.id }}"
238 tags:
239 tag_one: "{{ resource_prefix }} One"
240 Tag Two: two {{ resource_prefix }}
241 register: vpc_igw_recreate
242 check_mode: true
243
244 - name: Assert tag update would do nothing (expected changed=false) - CHECK_MODE
245 ansible.builtin.assert:
246 that:
247 - vpc_igw_recreate is not changed
248 - vpc_igw_recreate.gateway_id == igw_id
249 - vpc_igw_recreate.vpc_id == vpc_id
250 - '"tags" in vpc_igw_recreate'
251 - vpc_igw_recreate.tags | length == 2
252 - vpc_igw_recreate.tags["tag_one"] == resource_prefix +' One'
253 - vpc_igw_recreate.tags["Tag Two"] == 'two '+ resource_prefix
254
255 - name: Update the tags (no change)
256 amazon.aws.ec2_vpc_igw:
257 state: present
258 vpc_id: "{{ vpc_result.vpc.id }}"
259 tags:
260 tag_one: "{{ resource_prefix }} One"
261 Tag Two: two {{ resource_prefix }}
262 register: vpc_igw_recreate
263
264 - name: Assert tag update did nothing (expected changed=false)
265 ansible.builtin.assert:
266 that:
267 - vpc_igw_recreate is not changed
268 - vpc_igw_recreate.gateway_id == igw_id
269 - vpc_igw_recreate.vpc_id == vpc_id
270 - '"tags" in vpc_igw_recreate'
271 - vpc_igw_recreate.tags | length == 2
272 - vpc_igw_recreate.tags["tag_one"] == resource_prefix +' One'
273 - vpc_igw_recreate.tags["Tag Two"] == 'two '+ resource_prefix
274
275 # ============================================================
276 - name: Update the tags (remove and add) - CHECK_MODE
277 amazon.aws.ec2_vpc_igw:
278 state: present
279 vpc_id: "{{ vpc_result.vpc.id }}"
280 tags:
281 tag_three: "{{ resource_prefix }} Three"
282 Tag Two: two {{ resource_prefix }}
283 register: vpc_igw_update
284 check_mode: true
285
286 - name: Assert tag update would happen (expected changed=true) - CHECK_MODE
287 ansible.builtin.assert:
288 that:
289 - vpc_igw_update is changed
290 - vpc_igw_update.gateway_id == igw_id
291 - vpc_igw_update.vpc_id == vpc_id
292 - '"tags" in vpc_igw_update'
293 - vpc_igw_update.tags | length == 2
294
295 - name: Update the tags (remove and add)
296 amazon.aws.ec2_vpc_igw:
297 state: present
298 vpc_id: "{{ vpc_result.vpc.id }}"
299 tags:
300 tag_three: "{{ resource_prefix }} Three"
301 Tag Two: two {{ resource_prefix }}
302 register: vpc_igw_update
303
304 - name: Assert tags are updated (expected changed=true)
305 ansible.builtin.assert:
306 that:
307 - vpc_igw_update is changed
308 - vpc_igw_update.gateway_id == igw_id
309 - vpc_igw_update.vpc_id == vpc_id
310 - '"tags" in vpc_igw_update'
311 - vpc_igw_update.tags | length == 2
312 - vpc_igw_update.tags["tag_three"] == resource_prefix +' Three'
313 - vpc_igw_update.tags["Tag Two"] == 'two '+ resource_prefix
314
315 # ============================================================
316 - name: Update the tags add without purge - CHECK_MODE
317 amazon.aws.ec2_vpc_igw:
318 state: present
319 vpc_id: "{{ vpc_result.vpc.id }}"
320 purge_tags: false
321 tags:
322 tag_one: "{{ resource_prefix }} One"
323 register: vpc_igw_update
324 check_mode: true
325
326 - name: Assert tags would be added - CHECK_MODE
327 ansible.builtin.assert:
328 that:
329 - vpc_igw_update is changed
330 - vpc_igw_update.gateway_id == igw_id
331 - vpc_igw_update.vpc_id == vpc_id
332 - '"tags" in vpc_igw_update'
333
334 - name: Update the tags add without purge
335 amazon.aws.ec2_vpc_igw:
336 state: present
337 vpc_id: "{{ vpc_result.vpc.id }}"
338 purge_tags: false
339 tags:
340 tag_one: "{{ resource_prefix }} One"
341 register: vpc_igw_update
342
343 - name: Assert tags added
344 ansible.builtin.assert:
345 that:
346 - vpc_igw_update is changed
347 - vpc_igw_update.gateway_id == igw_id
348 - vpc_igw_update.vpc_id == vpc_id
349 - '"tags" in vpc_igw_update'
350 - vpc_igw_update.tags | length == 3
351 - vpc_igw_update.tags["tag_one"] == resource_prefix +' One'
352 - vpc_igw_update.tags["tag_three"] == resource_prefix +' Three'
353 - vpc_igw_update.tags["Tag Two"] == 'two '+ resource_prefix
354
355 # ============================================================
356 - name: Update with CamelCase tags - CHECK_MODE
357 amazon.aws.ec2_vpc_igw:
358 state: present
359 vpc_id: "{{ vpc_result.vpc.id }}"
360 tags:
361 lowercase spaced: hello cruel world
362 Title Case: Hello Cruel World
363 CamelCase: SimpleCamelCase
364 snake_case: simple_snake_case
365 register: vpc_igw_update
366 check_mode: true
367
368 - name: Assert tag update would happen (expected changed=true) - CHECK_MODE
369 ansible.builtin.assert:
370 that:
371 - vpc_igw_update is changed
372 - vpc_igw_update.gateway_id == igw_id
373 - vpc_igw_update.vpc_id == vpc_id
374 - '"tags" in vpc_igw_update'
375
376 - name: Update the tags - remove and add
377 amazon.aws.ec2_vpc_igw:
378 state: present
379 vpc_id: "{{ vpc_result.vpc.id }}"
380 tags:
381 lowercase spaced: hello cruel world
382 Title Case: Hello Cruel World
383 CamelCase: SimpleCamelCase
384 snake_case: simple_snake_case
385 register: vpc_igw_update
386
387 - name: Assert tags are updated (expected changed=true)
388 ansible.builtin.assert:
389 that:
390 - vpc_igw_update is changed
391 - vpc_igw_update.gateway_id == igw_id
392 - vpc_igw_update.vpc_id == vpc_id
393 - '"tags" in vpc_igw_update'
394 - vpc_igw_update.tags | length == 4
395 - vpc_igw_update.tags["lowercase spaced"] == 'hello cruel world'
396 - vpc_igw_update.tags["Title Case"] == 'Hello Cruel World'
397 - vpc_igw_update.tags["CamelCase"] == 'SimpleCamelCase'
398 - vpc_igw_update.tags["snake_case"] == 'simple_snake_case'
399
400 # ============================================================
401 - name: Gather information about a filtered list of Internet Gateways using tags
402 amazon.aws.ec2_vpc_igw_info:
403 filters:
404 tag:Title Case: Hello Cruel World
405 register: igw_info
406
407 - name: Assert success
408 ansible.builtin.assert:
409 that:
410 - igw_info is successful
411 - '"internet_gateways" in igw_info'
412 - igw_id in (igw_info.internet_gateways | map(attribute='internet_gateway_id') | list)
413
414 - name: Gather information about a filtered list of Internet Gateways using tags - CHECK_MODE
415 amazon.aws.ec2_vpc_igw_info:
416 filters:
417 tag:Title Case: Hello Cruel World
418 register: igw_info
419 check_mode: true
420
421 - name: Assert success - CHECK_MODE
422 ansible.builtin.assert:
423 that:
424 - igw_info is successful
425 - '"internet_gateways" in igw_info'
426 - igw_id in (igw_info.internet_gateways | map(attribute='internet_gateway_id') | list)
427
428 # ============================================================
429 - name: Gather information about a filtered list of Internet Gateways using tags (no match)
430 amazon.aws.ec2_vpc_igw_info:
431 filters:
432 tag:tag_one: "{{ resource_prefix }} One"
433 register: igw_info
434
435 - name: Assert success
436 ansible.builtin.assert:
437 that:
438 - igw_info is successful
439 - '"internet_gateways" in igw_info'
440 - igw_info.internet_gateways | length == 0
441
442 - name: Gather information about a filtered list of Internet Gateways using tags (no match) - CHECK_MODE
443 amazon.aws.ec2_vpc_igw_info:
444 filters:
445 tag:tag_one: "{{ resource_prefix }} One"
446 register: igw_info
447 check_mode: true
448
449 - name: Assert success - CHECK_MODE
450 ansible.builtin.assert:
451 that:
452 - igw_info is successful
453 - '"internet_gateways" in igw_info'
454 - igw_info.internet_gateways | length == 0
455
456 # ============================================================
457 - name: Remove all tags - CHECK_MODE
458 amazon.aws.ec2_vpc_igw:
459 state: present
460 vpc_id: "{{ vpc_result.vpc.id }}"
461 tags: {}
462 register: vpc_igw_update
463 check_mode: true
464
465 - name: Assert tags would be removed - CHECK_MODE
466 ansible.builtin.assert:
467 that:
468 - vpc_igw_update is changed
469
470 - name: Remove all tags
471 amazon.aws.ec2_vpc_igw:
472 state: present
473 vpc_id: "{{ vpc_result.vpc.id }}"
474 tags: {}
475 register: vpc_igw_update
476
477 - name: Assert tags removed
478 ansible.builtin.assert:
479 that:
480 - vpc_igw_update is changed
481 - vpc_igw_update.gateway_id == igw_id
482 - vpc_igw_update.vpc_id == vpc_id
483 - '"tags" in vpc_igw_update'
484 - vpc_igw_update.tags | length == 0
485
486 # ============================================================
487 - name: Test state=absent (expected changed=true) - CHECK_MODE
488 amazon.aws.ec2_vpc_igw:
489 state: absent
490 vpc_id: "{{ vpc_result.vpc.id }}"
491 register: vpc_igw_delete
492 check_mode: true
493
494 - name: Assert state=absent (expected changed=true) - CHECK_MODE
495 ansible.builtin.assert:
496 that:
497 - vpc_igw_delete is changed
498
499 - name: Test state=absent (expected changed=true)
500 amazon.aws.ec2_vpc_igw:
501 state: absent
502 vpc_id: "{{ vpc_result.vpc.id }}"
503 register: vpc_igw_delete
504
505 - name: Assert state=absent (expected changed=true)
506 ansible.builtin.assert:
507 that:
508 - vpc_igw_delete is changed
509
510 # ============================================================
511 - name: Fetch IGW by ID (list)
512 amazon.aws.ec2_vpc_igw_info:
513 internet_gateway_ids:
514 - "{{ igw_id }}"
515 register: igw_info
516 ignore_errors: true
517
518 - name: Check IGW does not exist
519 ansible.builtin.assert:
520 that:
521 # Deliberate choice not to change bevahiour when searching by ID
522 - igw_info is failed
523
524 # ============================================================
525 - name: Test state=absent when already deleted (expected changed=false) - CHECK_MODE
526 amazon.aws.ec2_vpc_igw:
527 state: absent
528 vpc_id: "{{ vpc_result.vpc.id }}"
529 register: vpc_igw_delete
530 check_mode: true
531
532 - name: Assert state=absent (expected changed=false) - CHECK_MODE
533 ansible.builtin.assert:
534 that:
535 - vpc_igw_delete is not changed
536
537 - name: Test state=absent when already deleted (expected changed=false)
538 amazon.aws.ec2_vpc_igw:
539 state: absent
540 vpc_id: "{{ vpc_result.vpc.id }}"
541 register: vpc_igw_delete
542
543 - name: Assert state=absent (expected changed=false)
544 ansible.builtin.assert:
545 that:
546 - vpc_igw_delete is not changed
547
548 # ============================================================
549 - name: Create new detached internet gateway - CHECK_MODE
550 amazon.aws.ec2_vpc_igw:
551 state: present
552 register: detached_igw_result
553 check_mode: true
554
555 - name: Assert creation would happen (expected changed=true) - CHECK_MODE
556 ansible.builtin.assert:
557 that:
558 - detached_igw_result is changed
559
560 - name: Create new detached internet gateway (expected changed=true)
561 amazon.aws.ec2_vpc_igw:
562 state: present
563 register: detached_igw_result
564
565 - name: Assert creation happened (expected changed=true)
566 ansible.builtin.assert:
567 that:
568 - detached_igw_result is changed
569 - '"gateway_id" in detached_igw_result'
570 - detached_igw_result.gateway_id.startswith("igw-")
571 - not detached_igw_result.vpc_id
572
573 # ============================================================
574 - name: Test state=absent when supplying only a gateway id (expected chaged=true) - CHECK_MODE
575 amazon.aws.ec2_vpc_igw:
576 state: absent
577 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
578 register: vpc_igw_delete
579 check_mode: true
580
581 - name: Assert state=absent when supplying only a gateway id (expected chaged=true) - CHECK_MODE
582 ansible.builtin.assert:
583 that:
584 - vpc_igw_delete is changed
585
586 - name: Search for IGW by ID
587 amazon.aws.ec2_vpc_igw_info:
588 internet_gateway_ids: "{{ detached_igw_result.gateway_id }}"
589 register: igw_info
590
591 - name: Check that IGW was not deleted in check mode
592 ansible.builtin.assert:
593 that:
594 - '"internet_gateways" in igw_info'
595 - igw_info.internet_gateways | length == 1
596
597 - name: Test state=absent when supplying only a gateway id (expected chaged=true)
598 amazon.aws.ec2_vpc_igw:
599 state: absent
600 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
601 register: vpc_igw_delete
602
603 - name: Assert state=absent when supplying only a gateway id (expected chaged=true)
604 ansible.builtin.assert:
605 that:
606 - vpc_igw_delete is changed
607
608 - name: Fetch removed IGW by ID
609 amazon.aws.ec2_vpc_igw_info:
610 internet_gateway_ids: "{{ detached_igw_result.gateway_id }}"
611 register: igw_info
612 ignore_errors: true
613
614 - name: Check IGW does not exist
615 ansible.builtin.assert:
616 that:
617 # Deliberate choice not to change bevahiour when searching by ID
618 - igw_info is failed
619
620 # ============================================================
621 - name: Create new internet gateway for vpc tests
622 amazon.aws.ec2_vpc_igw:
623 state: present
624 register: detached_igw_result
625
626 # ============================================================
627 - name: Test attaching VPC to gateway - CHECK_MODE
628 amazon.aws.ec2_vpc_igw:
629 state: present
630 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
631 vpc_id: "{{ vpc_id }}"
632 register: attach_vpc_result
633 check_mode: true
634
635 - name: Assert that VPC was attached - CHECK_MODE
636 ansible.builtin.assert:
637 that:
638 - attach_vpc_result is changed
639
640 - name: Test attaching VPC to gateway
641 amazon.aws.ec2_vpc_igw:
642 state: present
643 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
644 vpc_id: "{{ vpc_id }}"
645 register: attach_vpc_result
646
647 - name: Assert that VPC was attached
648 ansible.builtin.assert:
649 that:
650 - attach_vpc_result is changed
651 - attach_vpc_result.vpc_id == vpc_id
652 - attach_vpc_result.gateway_id == detached_igw_result.gateway_id
653
654 # ============================================================
655 - name: Test detaching VPC from gateway - CHECK_MODE
656 amazon.aws.ec2_vpc_igw:
657 state: present
658 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
659 detach_vpc: true
660 register: detach_vpc_result
661 check_mode: true
662
663 - name: Assert that VPC was detached - CHECK_MODE
664 ansible.builtin.assert:
665 that:
666 - detach_vpc_result is changed
667
668 - name: Test detaching VPC from gateway
669 amazon.aws.ec2_vpc_igw:
670 state: present
671 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
672 detach_vpc: true
673 register: detach_vpc_result
674
675 - name: Assert that VPC was detached
676 ansible.builtin.assert:
677 that:
678 - detach_vpc_result is changed
679 - not detach_vpc_result.vpc_id
680 - detach_vpc_result.gateway_id == detached_igw_result.gateway_id
681
682 # ============================================================
683 - name: Attach VPC to gateway for VPC change tests
684 amazon.aws.ec2_vpc_igw:
685 state: present
686 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
687 vpc_id: "{{ vpc_id }}"
688
689 # ============================================================
690 - name: Attempt change attached VPC with force_attach=false (default) - CHECK_MODE
691 amazon.aws.ec2_vpc_igw:
692 state: present
693 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
694 vpc_id: "{{ vpc_2_result.vpc.id }}"
695 register: igw_vpc_changed_result
696 check_mode: true
697
698 - name: Assert VPC changed with force_attach=false (default) - CHECK_MODE
699 ansible.builtin.assert:
700 that:
701 - igw_vpc_changed_result is changed
702 - vpc_id not in igw_vpc_changed_result
703
704 - name: Attempt change with force_attach=false (default) (expected failure)
705 amazon.aws.ec2_vpc_igw:
706 state: present
707 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
708 vpc_id: "{{ vpc_2_result.vpc.id }}"
709 register: igw_vpc_changed_result
710 ignore_errors: true
711
712 - name: Assert VPC changed with force_attach=false (default)
713 ansible.builtin.assert:
714 that:
715 - igw_vpc_changed_result is failed
716 - igw_vpc_changed_result.msg is search('VPC already attached, but does not match requested VPC.')
717
718 # ============================================================
719 - name: Attempt change attached VPC with force_attach=true - CHECK_MODE
720 amazon.aws.ec2_vpc_igw:
721 state: present
722 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
723 vpc_id: "{{ vpc_2_result.vpc.id }}"
724 force_attach: true
725 register: igw_vpc_changed_result
726 check_mode: true
727
728 - name: Assert VPC changed with force_attach=true - CHECK_MODE
729 ansible.builtin.assert:
730 that:
731 - igw_vpc_changed_result is changed
732 - vpc_id not in igw_vpc_changed_result
733
734 - name: Attempt change with force_attach=true
735 amazon.aws.ec2_vpc_igw:
736 state: present
737 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
738 vpc_id: "{{ vpc_2_result.vpc.id }}"
739 force_attach: true
740 register: igw_vpc_changed_result
741
742 - name: Assert VPC changed with force_attach=true
743 ansible.builtin.assert:
744 that:
745 - igw_vpc_changed_result is changed
746 - igw_vpc_changed_result.vpc_id == vpc_2_id
747
748 # ============================================================
749 - name: Test state=absent when supplying a gateway id and wrong vpc id (expected failure) - CHECK_MODE
750 amazon.aws.ec2_vpc_igw:
751 state: absent
752 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
753 vpc_id: vpc-xxxxxxxxx
754 register: vpc_igw_delete
755 check_mode: true
756 ignore_errors: true
757
758 - name: Assert state=absent when supplying a gateway id and wrong vpc id (expected failure) - CHECK_MODE
759 ansible.builtin.assert:
760 that:
761 - vpc_igw_delete is failed
762 - vpc_igw_delete.msg is search('Supplied VPC.*does not match found VPC.*')
763
764 - name: Test state=absent when supplying a gateway id and wrong vpc id (expected failure)
765 amazon.aws.ec2_vpc_igw:
766 state: absent
767 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
768 vpc_id: vpc-xxxxxxxxx
769 register: vpc_igw_delete
770 ignore_errors: true
771
772 - name: Assert state=absent when supplying a gateway id and wrong vpc id (expected failure)
773 ansible.builtin.assert:
774 that:
775 - vpc_igw_delete is failed
776 - vpc_igw_delete.msg is search('Supplied VPC.*does not match found VPC.*')
777
778 # ============================================================
779 - name: Test state=absent when supplying a gateway id and vpc id (expected chaged=true) - CHECK_MODE
780 amazon.aws.ec2_vpc_igw:
781 state: absent
782 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
783 vpc_id: "{{ vpc_2_id }}"
784 register: vpc_igw_delete
785 check_mode: true
786
787 - name: Assert state=absent when supplying a gateway id and vpc id (expected chaged=true) - CHECK_MODE
788 ansible.builtin.assert:
789 that:
790 - vpc_igw_delete is changed
791
792 - name: Test state=absent when supplying a gateway id and vpc id (expected chaged=true)
793 amazon.aws.ec2_vpc_igw:
794 state: absent
795 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
796 vpc_id: "{{ vpc_2_id }}"
797 register: vpc_igw_delete
798
799 - name: Assert state=absent when supplying a gateway id and vpc id (expected chaged=true)
800 ansible.builtin.assert:
801 that:
802 - vpc_igw_delete is changed
803
804 - name: Fetch removed IGW by ID
805 amazon.aws.ec2_vpc_igw_info:
806 internet_gateway_ids: "{{ detached_igw_result.gateway_id }}"
807 register: igw_info
808 ignore_errors: true
809
810 - name: Check IGW does not exist
811 ansible.builtin.assert:
812 that:
813 # Deliberate choice not to change bevahiour when searching by ID
814 - igw_info is failed
815
816 always:
817 # ============================================================
818 - name: Tidy up IGW
819 amazon.aws.ec2_vpc_igw:
820 state: absent
821 vpc_id: "{{ vpc_result.vpc.id }}"
822
823 - name: Tidy up IGW on second VPC
824 amazon.aws.ec2_vpc_igw:
825 state: absent
826 vpc_id: "{{ vpc_2_result.vpc.id }}"
827
828 - name: Tidy up detached IGW
829 amazon.aws.ec2_vpc_igw:
830 state: absent
831 internet_gateway_id: "{{ detached_igw_result.gateway_id }}"
832
833 - name: Tidy up VPC
834 amazon.aws.ec2_vpc_net:
835 name: "{{ vpc_name }}"
836 state: absent
837 cidr_block: "{{ vpc_cidr }}"
838 ignore_errors: true
839
840 - name: Tidy up second VPC
841 amazon.aws.ec2_vpc_net:
842 name: "{{ vpc_name_2 }}"
843 state: absent
844 cidr_block: "{{ vpc_cidr_2 }}"
845 ignore_errors: true