-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpc_setup.yml
160 lines (159 loc) · 5.19 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
- 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: vpcout
# - debug:
# var: vpcout
- name: create public subnet 1 in zone 1
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 zone 2
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 zone 3
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 zone 1
ec2_vpc_subnet:
vpc_id: "{{vpcout.vpc.id}}"
region: "{{ region }}"
az: "{{ zone1 }}"
state: "{{state}}"
cidr: "{{ PriSub1Cidr }}"
map_public: yes
resource_tags:
Name: vprofile-prisub1
register: prisub1_out
- name: create private subnet 2 in zone 2
ec2_vpc_subnet:
vpc_id: "{{vpcout.vpc.id}}"
region: "{{ region }}"
az: "{{ zone2 }}"
state: "{{state}}"
cidr: "{{ PriSub2Cidr }}"
map_public: yes
resource_tags:
Name: vprofile-prisub2
register: prisub2_out
- name: create private subnet 3 in zone 3
ec2_vpc_subnet:
vpc_id: "{{vpcout.vpc.id}}"
region: "{{ region }}"
az: "{{ zone3 }}"
state: "{{state}}"
cidr: "{{ PriSub3Cidr }}"
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: "{{ NATGW_out.nat_gateway_id }}"
register: privRT_out
- 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
- privRT_out.route_table.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}}"
privRTid: "{{privRT_out.route_table.id}}"
cacheable: yes
- name: create variables file for vpc output
copy:
content: "vpcid: {{vpcout.vpc.id}}\npubsub1id: {{pubsub1_out.subnet.id}}\npubsub2id: {{pubsub2_out.subnet.id}}\npubsub3id: {{pubsub3_out.subnet.id}}\nprisub1id: {{prisub1_out.subnet.id}}\nprisub2id: {{prisub2_out.subnet.id}}\nprisub3id: {{prisub3_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