-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc_setup.yml
180 lines (164 loc) · 5.22 KB
/
vpc_setup.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
- hosts: localhost
connection: local
gather_facts: False
tasks:
- name: Import VPC Variables
include_vars: vars/vpc_setup
- name: Create vprofile VPC
ec2_vpc_net:
name: "{{vpc_name}}"
cidr_block: "{{vpcCidr}}"
region: "{{region}}"
dns_support: yes
dns_hostnames: yes
tenancy: default
state: "{{state}}"
register: vpc_out
# - debug:
# var: vpc_out
- name: Create Public Subnet 1 in Zone 1
ec2_vpc_subnet:
vpc_id: "{{vpc_out.vpc.id}}"
region: "{{region}}"
az: "{{zone1}}"
state: "{{state}}"
cidr: "{{PubSub1Cidr}}"
map_public: yes
resource_tags:
Name: vprofile-pubsub1
register: pubsub1_out
- name: Create Public Subnet 2 in Zone 2
ec2_vpc_subnet:
vpc_id: "{{vpc_out.vpc.id}}"
region: "{{region}}"
az: "{{zone2}}"
state: "{{state}}"
cidr: "{{PubSub2Cidr}}"
map_public: yes
resource_tags:
Name: vprofile-pubsub2
register: pubsub2_out
- name: Create Public Subnet 3 in Zone 3
ec2_vpc_subnet:
vpc_id: "{{vpc_out.vpc.id}}"
region: "{{region}}"
az: "{{zone3}}"
state: "{{state}}"
cidr: "{{PubSub3Cidr}}"
map_public: yes
resource_tags:
Name: vprofile-pubsub3
register: pubsub3_out
- name: Create Private Subnet 1 in Zone 1
ec2_vpc_subnet:
vpc_id: "{{vpc_out.vpc.id}}"
region: "{{region}}"
az: "{{zone1}}"
state: "{{state}}"
cidr: "{{PrivSub1Cidr}}"
map_public: yes
resource_tags:
Name: vprofile-privsub1
register: privsub1_out
- name: Create Private Subnet 2 in Zone 2
ec2_vpc_subnet:
vpc_id: "{{vpc_out.vpc.id}}"
region: "{{region}}"
az: "{{zone2}}"
state: "{{state}}"
cidr: "{{PrivSub2Cidr}}"
map_public: yes
resource_tags:
Name: vprofile-privsub2
register: privsub2_out
- name: Create Private Subnet 3 in Zone 3
ec2_vpc_subnet:
vpc_id: "{{vpc_out.vpc.id}}"
region: "{{region}}"
az: "{{zone3}}"
state: "{{state}}"
cidr: "{{PrivSub3Cidr}}"
map_public: yes
resource_tags:
Name: vprofile-privsub3
register: privsub3_out
- name: Internet Gateway Setup
ec2_vpc_igw:
vpc_id: "{{vpc_out.vpc.id}}"
region: "{{region}}"
state: "{{state}}"
resource_tags:
Name: vprofile-IGW
register: igw_out
- name : Create public subnet route table
ec2_vpc_route_table:
vpc_id: "{{vpc_out.vpc.id}}"
region: "{{region}}"
tags:
Name: VProfile-PubRT
subnets:
- "{{pubsub1_out.subnet.id}}"
- "{{pubsub2_out.subnet.id}}"
- "{{pubsub3_out.subnet.id}}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{igw_out.gateway_id}}"
register: pubRT_out
- name: Create NAT gateway and allocate EIP
ec2_vpc_nat_gateway:
state: "{{state}}"
subnet_id: "{{pubsub1_out.subnet.id}}"
wait: yes
region: "{{region}}"
if_exist_do_not_create: true
tags:
Name: VProfile-NATGW
register: NATGW_out
- name : Create Private subnet route table
ec2_vpc_route_table:
vpc_id: "{{vpc_out.vpc.id}}"
region: "{{region}}"
tags:
Name: VProfile-PrivRT
subnets:
- "{{privsub1_out.subnet.id}}"
- "{{privsub2_out.subnet.id}}"
- "{{privsub3_out.subnet.id}}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ NATGW_out.nat_gateway_id }}"
register: privRT_out
- debug:
var: "{{item}}"
loop:
- vpc_out.vpc.id
- pubsub1_out.subnet.id
- pubsub2_out.subnet.id
- pubsub3_out.subnet.id
- privsub1_out.subnet.id
- privsub2_out.subnet.id
- privsub3_out.subnet.id
- igw_out.gateway_id
- pubRT_out.route_table.id
- NATGW_out.nat_gateway_id
- privRT_out.route_table.id
- set_fact:
vpcid: "{{vpc_out.vpc.id}}"
pubsub1id: "{{pubsub1_out.subnet.id}}"
pubsub2id: "{{pubsub2_out.subnet.id}}"
pubsub3id: "{{pubsub3_out.subnet.id}}"
privsub1id: "{{privsub1_out.subnet.id}}"
privsub2id: "{{privsub2_out.subnet.id}}"
privsub3id: "{{privsub3_out.subnet.id}}"
igwid: "{{igw_out.gateway_id}}"
pubRTid: "{{pubRT_out.route_table.id}}"
NATGWid: "{{NATGW_out.nat_gateway_id}}"
privRTid: "{{privRT_out.route_table.id}}"
cacheable: yes
- name: Create variables file for vpc_setup output
copy:
content: "vpcid: {{vpc_out.vpc.id}}\npubsub1id: {{pubsub1_out.subnet.id}}\npubsub2id: {{pubsub2_out.subnet.id}}\
\npubsub3id: {{pubsub3_out.subnet.id}}\nprivsub1id: {{privsub1_out.subnet.id}}\nprivsub2id: {{privsub2_out.subnet.id}}\
\nprivsub3id: {{privsub3_out.subnet.id}}\nigwid: {{igw_out.gateway_id}}\npubRTid: {{pubRT_out.route_table.id}}\
\nNATGWid: {{NATGW_out.nat_gateway_id}}\nprivRTid: {{privRT_out.route_table.id}}"
dest: vars/output_vars