-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc_setup.yml
171 lines (155 loc) · 5.12 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
---
- name: Set up VPC network to host EC2s
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Import VPC variables
ansible.builtin.include_vars: vars/vpc_setup
- name: Create project VPC
amazon.aws.ec2_vpc_net:
name: "{{ vpc_name }}"
cidr_block: "{{ vpcCidr }}"
region: "{{ region }}"
dns_support: true
dns_hostnames: true
tenancy: default
state: "{{ state }}"
register: vpcout
# - debug:
# var: vpcout
- name: Create public subnet 1 in zone 1
amazon.aws.ec2_vpc_subnet:
vpc_id: "{{ vpcout.vpc.id }}"
region: "{{ region }}"
az: "{{ zone1 }}"
state: "{{ state }}"
cidr: "{{ PubSub1Cidr }}"
map_public: true
resource_tags:
Name: aav-pubsub1
register: pubsub1_out
- name: Create public subnet 2 in zone 2
amazon.aws.ec2_vpc_subnet:
vpc_id: "{{ vpcout.vpc.id }}"
region: "{{ region }}"
az: "{{ zone2 }}"
state: "{{ state }}"
cidr: "{{ PubSub2Cidr }}"
map_public: true
resource_tags:
Name: aav-pubsub2
register: pubsub2_out
- name: Create public subnet 3 in zone 3
amazon.aws.ec2_vpc_subnet:
vpc_id: "{{ vpcout.vpc.id }}"
region: "{{ region }}"
az: "{{ zone3 }}"
state: "{{ state }}"
cidr: "{{ PubSub3Cidr }}"
map_public: true
resource_tags:
Name: aav-pubsub3
register: pubsub3_out
- name: Create private subnet 1 in zone 1
amazon.aws.ec2_vpc_subnet:
vpc_id: "{{ vpcout.vpc.id }}"
region: "{{ region }}"
az: "{{ zone1 }}"
state: "{{ state }}"
cidr: "{{ PrivSub1Cidr }}"
map_public: true
resource_tags:
Name: aav-privsub1
register: privsub1_out
- name: Create private subnet 2 in zone 2
amazon.aws.ec2_vpc_subnet:
vpc_id: "{{ vpcout.vpc.id }}"
region: "{{ region }}"
az: "{{ zone2 }}"
state: "{{ state }}"
cidr: "{{ PrivSub2Cidr }}"
map_public: true
resource_tags:
Name: aav-privsub2
register: privsub2_out
- name: Create private subnet 3 in zone 3
amazon.aws.ec2_vpc_subnet:
vpc_id: "{{ vpcout.vpc.id }}"
region: "{{ region }}"
az: "{{ zone3 }}"
state: "{{ state }}"
cidr: "{{ PrivSub3Cidr }}"
map_public: true
resource_tags:
Name: aav-privsub3
register: privsub3_out
- name: Internet Gateway setup
amazon.aws.ec2_vpc_igw:
vpc_id: "{{ vpcout.vpc.id }}"
region: "{{ region }}"
state: "{{ state }}"
resource_tags:
Name: aav-IGW
register: igw_out
- name: Set up public subnet route table
amazon.aws.ec2_vpc_route_table:
vpc_id: "{{ vpcout.vpc.id }}"
region: "{{ region }}"
resource_tags:
Name: aav-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: publicrt_out
- name: Create new nat gateway and allocate new EIP if a nat gateway does not yet exist in the subnet.
amazon.aws.ec2_vpc_nat_gateway:
state: "{{ state }}"
subnet_id: "{{ pubsub1_out.subnet.id }}"
wait: true
region: "{{ region }}"
if_exist_do_not_create: true
register: natgw_out
# - debug:
# var: natgw_out
- name: Set up private subnet route table
amazon.aws.ec2_vpc_route_table:
vpc_id: "{{ vpcout.vpc.id }}"
region: "{{ region }}"
tags:
Name: aav-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
- name: List all registered variables to create output file
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
- publicrt_out.route_table.id
- natgw_out.nat_gateway_id
- privrt_out.route_table.id
- name: Create variables vile for VPC output
ansible.builtin.copy:
content: "vpcid: {{ vpcout.vpc.id }}\npubsub1id: {{ pubsub1_out.subnet.id }}\npubsub2id: {{ pubsub2_out.subnet.id }}\n
pubsub3id: {{ pubsub3_out.subnet.id }}\nprivsub1id: {{ privsub1_out.subnet.id }}\n
privsub2id: {{ privsub2_out.subnet.id }}\npubsub3id: {{ pubsub3_out.subnet.id }}\nigwid: {{ igw_out.gateway_id }}\n
pubRTid: {{ publicrt_out.route_table.id }}\nNATGWid: {{ natgw_out.nat_gateway_id }}\nprivRTid: {{ privrt_out.route_table.id }}\n"
dest: vars/output_vars
mode: "0600"