-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_vpc.yml
569 lines (497 loc) · 18.5 KB
/
create_vpc.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
---
# Exemplos de subredes usando CIDR 172.16.
# Cada subrede terá 4091 endereços para hosts.
# A AWS reserva os IPs .1(VPC router), .2(Amazon-provided DNS) e .3(uso futuro).
# Netmask: 255.255.240.0 = 20
# zona a publica 01 172.16.0.0/20 - 172.16.0.4 a 172.16.15.254
# zona a privada 01 172.16.16.0/20 - 172.16.16.4 a 172.16.31.254
# zona b publica 02 172.16.32.0/20 - 172.16.32.4 a 172.16.47.254
# zona b privada 02 172.16.48.0/20 - 172.16.48.4 a 172.16.63.254
# zona c publica 03 172.16.64.0/20 - 172.16.64.4 a 172.16.79.254
# zona c privada 03 172.16.80.0/20 - 172.16.80.4 a 172.16.95.254
# zona e publica 04 172.16.96.0/20 - 172.16.96.4 a 172.16.111.254
# zona e privada 04 172.16.112.0/20 - 172.16.112.4 a 172.16.127.254
- name: criação de VPC
hosts: localhost
gather_facts: False
vars:
region: us-east-1
profile: dimarino
vpc_name: vpc-dimarino-dev
vpc_tag_environment: development
vpc_cidr_block: 172.16.0.0/16
public_subnet_1_cidr_block: 172.16.0.0/20
private_subnet_1_cidr_block: 172.16.16.0/20
public_subnet_2_cidr_block: 172.16.32.0/20
private_subnet_2_cidr_block: 172.16.48.0/20
public_subnet_3_cidr_block: 172.16.64.0/20
private_subnet_3_cidr_block: 172.16.80.0/20
public_subnet_4_cidr_block: 172.16.96.0/20
private_subnet_4_cidr_block: 172.16.112.0/20
public_subnet_5_cidr_block: 172.16.128.0/20
private_subnet_5_cidr_block: 172.16.144.0/20
public_subnet_6_cidr_block: 172.16.160.0/20
private_subnet_6_cidr_block: 172.16.176.0/20
availability_zone_1: us-east-1a
availability_zone_2: us-east-1b
availability_zone_3: us-east-1c
availability_zone_4: us-east-1d
availability_zone_5: us-east-1e
availability_zone_6: us-east-1f
tasks:
- name: cria vpc e tags
ec2_vpc_net:
name: "{{ vpc_name }}"
profile: "{{ profile }}"
cidr_block: "{{ vpc_cidr_block }}"
region: "{{ region }}"
dns_hostnames: yes
dns_support: yes
tags:
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_net_return
# - name: print vpc return values
# debug:
# var: ec2_vpc_net_return
- name: cria internet gateway e tags
ec2_vpc_igw:
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
profile: "{{ profile }}"
region: "{{ region }}"
tags:
Name: "{{ vpc_name }} - igw"
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_igw_return
# - name: print igw return values
# debug:
# var: ec2_vpc_igw_return
- name: cria subrede publica 1
ec2_vpc_subnet:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
cidr: "{{ public_subnet_1_cidr_block }}"
az: "{{ availability_zone_1 }}"
region: "{{ region }}"
map_public: yes
wait: yes
wait_timeout: 300
resource_tags:
Name: "{{ vpc_name }} - Subrede Publica 01"
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_subnet_public_1_return
# - name: print public subnet 1 return values
# debug:
# var: ec2_vpc_subnet_public_1_return
- name: cria subrede privada 1
ec2_vpc_subnet:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
cidr: "{{ private_subnet_1_cidr_block }}"
az: "{{ availability_zone_1 }}"
region: "{{ region }}"
map_public: no
wait: yes
wait_timeout: 300
resource_tags:
Name: "{{ vpc_name }} - Subrede Privada 01"
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_subnet_private_1_return
# - name: print private subnet 1 return values
# debug:
# var: ec2_vpc_subnet_private_1_return
- name: cria subrede publica 2
ec2_vpc_subnet:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
cidr: "{{ public_subnet_2_cidr_block }}"
az: "{{ availability_zone_2 }}"
region: "{{ region }}"
map_public: yes
wait: yes
wait_timeout: 300
resource_tags:
Name: "{{ vpc_name }} - Subrede Publica 02"
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_subnet_public_2_return
# - name: print public subnet 2 return values
# debug:
# var: ec2_vpc_subnet_public_2_return
- name: cria subrede privada 2
ec2_vpc_subnet:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
cidr: "{{ private_subnet_2_cidr_block }}"
az: "{{ availability_zone_2 }}"
region: "{{ region }}"
map_public: no
wait: yes
wait_timeout: 300
resource_tags:
Name: "{{ vpc_name }} - Subrede Privada 02"
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_subnet_private_2_return
# - name: print private subnet 2 return values
# debug:
# var: ec2_vpc_subnet_private_2_return
- name: cria subrede publica 3
ec2_vpc_subnet:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
cidr: "{{ public_subnet_3_cidr_block }}"
az: "{{ availability_zone_3 }}"
region: "{{ region }}"
map_public: yes
wait: yes
wait_timeout: 300
resource_tags:
Name: "{{ vpc_name }} - Subrede Publica 03"
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_subnet_public_3_return
# - name: print public subnet 3 return values
# debug:
# var: ec2_vpc_subnet_public_3_return
- name: cria subrede privada 3
ec2_vpc_subnet:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
cidr: "{{ private_subnet_3_cidr_block }}"
az: "{{ availability_zone_3 }}"
region: "{{ region }}"
map_public: no
wait: yes
wait_timeout: 300
resource_tags:
Name: "{{ vpc_name }} - Subrede Privada 03"
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_subnet_private_3_return
# - name: print private subnet 3 return values
# debug:
# var: ec2_vpc_subnet_private_3_return
- name: cria subrede publica 4
ec2_vpc_subnet:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
cidr: "{{ public_subnet_4_cidr_block }}"
az: "{{ availability_zone_4 }}"
region: "{{ region }}"
map_public: yes
wait: yes
wait_timeout: 300
resource_tags:
Name: "{{ vpc_name }} - Subrede Publica 04"
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_subnet_public_4_return
# - name: print public subnet 4 return values
# debug:
# var: ec2_vpc_subnet_public_4_return
- name: cria subrede privada 4
ec2_vpc_subnet:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
cidr: "{{ private_subnet_4_cidr_block }}"
az: "{{ availability_zone_4 }}"
region: "{{ region }}"
map_public: no
wait: yes
wait_timeout: 300
resource_tags:
Name: "{{ vpc_name }} - Subrede Privada 04"
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_subnet_private_4_return
# - name: print private subnet 4 return values
# debug:
# var: ec2_vpc_subnet_private_4_return
- name: cria subrede publica 5
ec2_vpc_subnet:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
cidr: "{{ public_subnet_5_cidr_block }}"
az: "{{ availability_zone_5 }}"
region: "{{ region }}"
map_public: yes
wait: yes
wait_timeout: 300
resource_tags:
Name: "{{ vpc_name }} - Subrede Publica 05"
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_subnet_public_5_return
# - name: print public subnet 5 return values
# debug:
# var: ec2_vpc_subnet_public_5_return
- name: cria subrede privada 5
ec2_vpc_subnet:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
cidr: "{{ private_subnet_5_cidr_block }}"
az: "{{ availability_zone_5 }}"
region: "{{ region }}"
map_public: no
wait: yes
wait_timeout: 300
resource_tags:
Name: "{{ vpc_name }} - Subrede Privada 05"
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_subnet_private_5_return
# - name: print private subnet 5 return values
# debug:
# var: ec2_vpc_subnet_private_5_return
- name: cria subrede publica 6
ec2_vpc_subnet:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
cidr: "{{ public_subnet_6_cidr_block }}"
az: "{{ availability_zone_6 }}"
region: "{{ region }}"
map_public: yes
wait: yes
wait_timeout: 300
resource_tags:
Name: "{{ vpc_name }} - Subrede Publica 06"
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_subnet_public_6_return
# - name: print public subnet 6 return values
# debug:
# var: ec2_vpc_subnet_public_6_return
- name: cria subrede privada 6
ec2_vpc_subnet:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
cidr: "{{ private_subnet_6_cidr_block }}"
az: "{{ availability_zone_6 }}"
region: "{{ region }}"
map_public: no
wait: yes
wait_timeout: 300
resource_tags:
Name: "{{ vpc_name }} - Subrede Privada 06"
Environment: "{{ vpc_tag_environment }}"
register: ec2_vpc_subnet_private_6_return
# - name: print private subnet 5 return values
# debug:
# var: ec2_vpc_subnet_private_5_return
- name: cria nat gateway na subrede publica 1, se um não existir nesta subrede
ec2_vpc_nat_gateway:
subnet_id: "{{ ec2_vpc_subnet_public_1_return.subnet.id }}"
wait: yes
wait_timeout : 300
region: "{{ region }}"
if_exist_do_not_create: true
register: ec2_vpc_nat_gateway_subnet_public_1_return
# - name: print nat gateway under public subnet 1 return values
# debug:
# var: ec2_vpc_nat_gateway_subnet_public_1_return
- name: cria nat gateway na subrede publica 2, se um não existir nesta subrede
ec2_vpc_nat_gateway:
subnet_id: "{{ ec2_vpc_subnet_public_2_return.subnet.id }}"
wait: yes
wait_timeout : 300
region: "{{ region }}"
if_exist_do_not_create: true
register: ec2_vpc_nat_gateway_subnet_public_2_return
# - name: print nat gateway under public subnet 2 return values
# debug:
# var: ec2_vpc_nat_gateway_subnet_public_2_return
- name: cria nat gateway na subrede publica 3, se um não existir nesta subrede
ec2_vpc_nat_gateway:
subnet_id: "{{ ec2_vpc_subnet_public_3_return.subnet.id }}"
wait: yes
wait_timeout : 300
region: "{{ region }}"
if_exist_do_not_create: true
register: ec2_vpc_nat_gateway_subnet_public_3_return
# - name: print nat gateway under public subnet 3 return values
# debug:
# var: ec2_vpc_nat_gateway_subnet_public_3_return
- name: cria nat gateway na subrede publica 4, se um não existir nesta subrede
ec2_vpc_nat_gateway:
subnet_id: "{{ ec2_vpc_subnet_public_4_return.subnet.id }}"
wait: yes
wait_timeout : 300
region: "{{ region }}"
if_exist_do_not_create: true
register: ec2_vpc_nat_gateway_subnet_public_4_return
# - name: print nat gateway under public subnet 4 return values
# debug:
# var: ec2_vpc_nat_gateway_subnet_public_4_return
- name: cria nat gateway na subrede publica 5, se um não existir nesta subrede
ec2_vpc_nat_gateway:
subnet_id: "{{ ec2_vpc_subnet_public_5_return.subnet.id }}"
wait: yes
wait_timeout : 300
region: "{{ region }}"
if_exist_do_not_create: true
register: ec2_vpc_nat_gateway_subnet_public_5_return
# - name: print nat gateway under public subnet 5 return values
# debug:
# var: ec2_vpc_nat_gateway_subnet_public_5_return
- name: cria nat gateway na subrede publica 6, se um não existir nesta subrede
ec2_vpc_nat_gateway:
subnet_id: "{{ ec2_vpc_subnet_public_6_return.subnet.id }}"
wait: yes
wait_timeout : 300
region: "{{ region }}"
if_exist_do_not_create: true
register: ec2_vpc_nat_gateway_subnet_public_6_return
# - name: print nat gateway under public subnet 6 return values
# debug:
# var: ec2_vpc_nat_gateway_subnet_public_6_return
- name: recolhe fatos sobre a tabela de roteamento default (criada simultâneamente com a VPC)
ec2_vpc_route_table_facts:
profile: "{{ profile }}"
region: "{{ region }}"
filters:
vpc-id: "{{ ec2_vpc_net_return.vpc.id }}"
register: vpc_route_table_facts_default_return
# - name: print vpc_route_table_facts_default_return values
# debug:
# var: vpc_route_table_facts_default_return
- name: altera nome da tabela de roteamento default, adiciona subredes públicas e cria rota para internet gateway
ec2_vpc_route_table:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
region: "{{ region }}"
route_table_id: "{{ vpc_route_table_facts_default_return.route_tables[0].id }}"
lookup: id
subnets:
- "{{ ec2_vpc_subnet_public_1_return.subnet.id }}"
- "{{ ec2_vpc_subnet_public_2_return.subnet.id }}"
- "{{ ec2_vpc_subnet_public_3_return.subnet.id }}"
- "{{ ec2_vpc_subnet_public_4_return.subnet.id }}"
- "{{ ec2_vpc_subnet_public_5_return.subnet.id }}"
- "{{ ec2_vpc_subnet_public_6_return.subnet.id }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ ec2_vpc_igw_return.gateway_id }}"
tags:
Name: "{{ vpc_name }} - Tabela de Roteamento das Subredes Publicas"
Environment: "{{ vpc_tag_environment }}"
register: vpc_route_table_public_return
# - name: print vpc_route_table_public_return values
# debug:
# var: vpc_route_table_public_return
- name: cria tabela de roteamento para subrede privada 01
ec2_vpc_route_table:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
region: "{{ region }}"
subnets:
- "{{ ec2_vpc_subnet_private_1_return.subnet.id }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ ec2_vpc_nat_gateway_subnet_public_1_return.nat_gateway_id }}"
tags:
Name: "{{ vpc_name }} - Tabela de Roteamento da Subrede Privada 01"
Environment: "{{ vpc_tag_environment }}"
register: vpc_route_table_private_1_return
# - name: print vpc_route_table_private_1_return values
# debug:
# var: vpc_route_table_private_1_return
- name: cria tabela de roteamento para subrede privada 02
ec2_vpc_route_table:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
region: "{{ region }}"
subnets:
- "{{ ec2_vpc_subnet_private_2_return.subnet.id }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ ec2_vpc_nat_gateway_subnet_public_2_return.nat_gateway_id }}"
tags:
Name: "{{ vpc_name }} - Tabela de Roteamento da Subrede Privada 02"
Environment: "{{ vpc_tag_environment }}"
register: vpc_route_table_private_2_return
# - name: print vpc_route_table_private_2_return values
# debug:
# var: vpc_route_table_private_2_return
- name: cria tabela de roteamento para subrede privada 03
ec2_vpc_route_table:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
region: "{{ region }}"
subnets:
- "{{ ec2_vpc_subnet_private_3_return.subnet.id }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ ec2_vpc_nat_gateway_subnet_public_3_return.nat_gateway_id }}"
tags:
Name: "{{ vpc_name }} - Tabela de Roteamento da Subrede Privada 03"
Environment: "{{ vpc_tag_environment }}"
register: vpc_route_table_private_3_return
# - name: print vpc_route_table_private_3_return values
# debug:
# var: vpc_route_table_private_3_return
- name: cria tabela de roteamento para subrede privada 04
ec2_vpc_route_table:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
region: "{{ region }}"
subnets:
- "{{ ec2_vpc_subnet_private_4_return.subnet.id }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ ec2_vpc_nat_gateway_subnet_public_4_return.nat_gateway_id }}"
tags:
Name: "{{ vpc_name }} - Tabela de Roteamento da Subrede Privada 04"
Environment: "{{ vpc_tag_environment }}"
register: vpc_route_table_private_4_return
# - name: print vpc_route_table_private_4_return values
# debug:
# var: vpc_route_table_private_4_return
- name: cria tabela de roteamento para subrede privada 05
ec2_vpc_route_table:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
region: "{{ region }}"
subnets:
- "{{ ec2_vpc_subnet_private_5_return.subnet.id }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ ec2_vpc_nat_gateway_subnet_public_5_return.nat_gateway_id }}"
tags:
Name: "{{ vpc_name }} - Tabela de Roteamento da Subrede Privada 05"
Environment: "{{ vpc_tag_environment }}"
register: vpc_route_table_private_5_return
# - name: print vpc_route_table_private_5_return values
# debug:
# var: vpc_route_table_private_5_return
- name: cria tabela de roteamento para subrede privada 06
ec2_vpc_route_table:
profile: "{{ profile }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
region: "{{ region }}"
subnets:
- "{{ ec2_vpc_subnet_private_6_return.subnet.id }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ ec2_vpc_nat_gateway_subnet_public_6_return.nat_gateway_id }}"
tags:
Name: "{{ vpc_name }} - Tabela de Roteamento da Subrede Privada 06"
Environment: "{{ vpc_tag_environment }}"
register: vpc_route_table_private_6_return
# - name: print vpc_route_table_private_6_return values
# debug:
# var: vpc_route_table_private_6_return
- name: cria opções de DHCP e associa a vpc
ec2_vpc_dhcp_option:
domain_name: "{{ vpc_name }}"
region: "{{ region }}"
dns_servers: AmazonProvidedDNS
tags:
Name: "{{ vpc_name }} - Conjunto de Opcoes de DHCP"
Environment: "{{ vpc_tag_environment }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
register: ec2_vpc_dhcp_option_return
# - name: print ec2_vpc_dhcp_option_return values
# debug:
# var: ec2_vpc_dhcp_option_return
- name: cria zona de DNS privada para a vpc
route53_zone:
zone: "{{ vpc_name }}"
vpc_id: "{{ ec2_vpc_net_return.vpc.id }}"
vpc_region: "{{ region }}"
region: "{{ region }}"
comment: "Dominio da vpc {{ vpc_name }}"
register: route53_zone_return
# - name: print route53_zone_return values
# debug:
# var: route53_zone_return