-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc-setup.yml
206 lines (185 loc) · 5.75 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
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
- hosts: localhost
connection: local
gather_facts: False
tasks:
- name: Import VPC Variables
include_vars: vars/vpc_setup
- name: Create profile VPC
ec2_vpc_net:
name: "{{vpc_name}}"
cidr_block: "{{ vpcCidr }}"
region: "{{region}}"
dns_support: yes
dns_hostnames: yes
tenancy: default
state: "{{state}}"
register: vpc_creation_result
- name: Debug VPC creation Output
debug:
var: vpc_creation_result
- name: Set vpcout Variable
set_fact:
vpcout: "{{ vpc_creation_result}}"
- name: debug vpcout Variable
debug:
var: vpcout
- name: Create Public Subnet 1 in Zone1
ec2_vpc_subnet:
vpc_id: "{{ vpcout.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 Zone2
ec2_vpc_subnet:
vpc_id: "{{vpcout.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 zone3
ec2_vpc_subnet:
vpc_id: "{{vpcout.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 zone1
ec2_vpc_subnet:
vpc_id: "{{vpcout.vpc.id}}"
region: "{{region}}"
az: "{{zone1}}"
state: "{{state}}"
cidr: "{{ PriSub4Cidr }}"
map_public: yes
resource_tags:
Name: vprofile_prisub1
register: prisub1_out
- name: create Private subnet 2 in zone2
ec2_vpc_subnet:
vpc_id: "{{vpcout.vpc.id}}"
region: "{{region}}"
az: "{{zone2}}"
state: "{{state}}"
cidr: "{{ PriSub5Cidr }}"
map_public: yes
resource_tags:
Name: vprofile_prisub2
register: prisub2_out
- name: create Private subnet 3 in zone3
ec2_vpc_subnet:
vpc_id: "{{vpcout.vpc.id}}"
region: "{{region}}"
az: "{{zone3}}"
state: "{{state}}"
cidr: "{{ PriSub6Cidr }}"
map_public: yes
resource_tags:
Name: vprofile_prisub3
register: prisub3_out
- name: Internet Gateway setup
ec2_vpc_igw:
vpc_id: "{{vpcout.vpc.id}}"
region: "{{region}}"
state: "{{state}}"
resource_tags:
Name: vprofile_IGW
register: igw_out
- name: Set up public subnet route table
ec2_vpc_route_table:
vpc_id: "{{vpcout.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 new nat gateway and allocate new EIP if a nat gateway does not yet exist in the subnet
ec2_vpc_nat_gateway:
state: "{{state}}"
subnet_id: "{{pubsub1_out.subnet.id}}"
wait: yes
region: "{{region}}"
if_exist_do_not_create: true
register: NATGW_out
- name: Set up Private subnet route table
ec2_vpc_route_table:
vpc_id: "{{vpcout.vpc.id}}"
region: "{{region}}"
tags:
Name: Vprofile_PriRT
subnets:
- "{{prisub1_out.subnet.id}}"
- "{{prisub2_out.subnet.id}}"
- "{{prisub3_out.subnet.id}}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{nat_gateway_variable.nat_gateway.id}}"
when: nat_gateway_variable is defined and nat_gateway_variable.nat_gateway is defined
- debug:
var: "{{item}}"
loop:
- vpcout.vpc.id
- pubsub1_out.subnet.id
- pubsub2_out.subnet.id
- pubsub3_out.subnet.id
- prisub1_out.subnet.id
- prisub2_out.subnet.id
- prisub3_out.subnet.id
- igw_out.gateway.id
- pubRT_out.route_table.id
- NATGW_out.nat_gateway_id
- set_fact:
vpcid: "{{vpcout.vpc.id}}"
pubsub1id: "{{pubsub1_out.subnet.id}}"
pubsub2id: "{{pubsub2_out.subnet.id}}"
pubsub3id: "{{pubsub3_out.subnet.id}}"
prisub1id: "{{prisub1_out.subnet.id}}"
prisub2id: "{{prisub2_out.subnet.id}}"
prisub3id: "{{prisub3_out.subnet.id}}"
igwid: "{{igw_out.gateway_id}}"
pubRTid: "{{pubRT_out.route_table.id}}"
NATGWid: "{{NATGW_out.nat_gateway_id}}"
cacheable: yes
- name: Create Variables file for vpc output
copy:
content: |
vpcid: {{vpcout.vpc.id}}
pubsub1id: {{pubsub1_out.subnet.id}}
pubsub2id: {{pubsub2_out.subnet.id}}
pubsub3id: {{pubsub3_out.subnet.id}}
prisub1id: {{prisub1_out.subnet.id}}
prisub2id: {{prisub2_out.subnet.id}}
prisub3id: {{prisub3_out.subnet.id}}
igwid: {{igw_out.gateway_id}}
pubRTid: {{pubRT_out.route_table.id}}
NATGWid: {{NATGW_out.nat_gateway_id}}
dest: vars/output_vars
when:
- vpcout is defined
- pubsub1_out is defined
- pubsub2_out is defined
- pubsub3_out is defined
- prisub1_out is defined
- prisub2_out is defined
- prisub3_out is defined
- igw_out is defined
- pubRT_out is defined
- NATGW_out is defined