-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpc-setup.yaml
173 lines (158 loc) · 5.4 KB
/
vpc-setup.yaml
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
---
- name: Configure VPC for Vprofile Project
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: "{{ vpc_cidr }}"
region: "{{ region }}"
dns_support: yes
dns_hostnames: yes
tenancy: default
state: "{{ state }}"
register: vpcout
- ansible.builtin.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: "{{ pub_sub1_cidr }}"
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: "{{ pub_sub2_cidr }}"
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: "{{ pub_sub3_cidr }}"
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: "{{ priv_sub1_cidr }}"
resource_tags:
Name: vprofile_privsub1
register: privsub1_out
- name: Create Private Subnet 2 in Zone2
ec2_vpc_subnet:
vpc_id: "{{ vpcout.vpc.id }}"
region: "{{ region }}"
az: "{{ zone2 }}"
state: "{{ state }}"
cidr: "{{ priv_sub2_cidr }}"
resource_tags:
Name: vprofile_privsub2
register: privsub2_out
- name: Create Private Subnet 3 in Zone3
ec2_vpc_subnet:
vpc_id: "{{ vpcout.vpc.id }}"
region: "{{ region }}"
az: "{{ zone3 }}"
state: "{{ state }}"
cidr: "{{ priv_sub3_cidr }}"
resource_tags:
Name: vprofile_privsub3
register: privsub3_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_pub_rt
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_priv_rt
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
- ansible.builtin.debug:
var: "{{ item }}"
loop:
- vpcout.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
- ansible.builtin.set_fact:
vpcid: "{{ vpcout.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 Output
ansible.builtin.copy:
content: "region: {{ region }}\nvpcid: {{ vpcout.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 }}\n"
dest: vars/vpc_output_vars