-
Notifications
You must be signed in to change notification settings - Fork 0
/
vprofile-ec2-stack.yaml
250 lines (225 loc) · 6.96 KB
/
vprofile-ec2-stack.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
---
- name: Setup Vprofile Stack
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Import VPC Setup Variables
include_vars: vars/vpc_output_vars
- name: Import Vprofile Setup Variables
include_vars: vars/vprofile_stack_setup
- name: Create Vprofile EC2 Key
ec2_key:
name: vprokey
region: "{{ region }}"
register: vprokey_out
- name: Save Private Key into file loginkey_vpro.pem
ansible.builtin.copy:
content: "{{ vprokey_out.key.private_key }}"
dest: "./loginkey_vpro.pem"
mode: 0600
when: vprokey_out.changed
- name: Create Securiry Group for Load Balancer
ec2_group:
name: vproelb-sg
description: Allow port 80 from everywhere and all port within sg
region: "{{ region }}"
vpc_id: "{{ vpcid }}"
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
register: vproelbsg_out
- name: Create Securiry Group for Vprofile Stack
ec2_group:
name: vprostack-sg
description: Allow port 22 from everywhere and all port within sg
region: "{{ region }}"
vpc_id: "{{ vpcid }}"
purge_rules: no
rules:
- proto: tcp
from_port: 80
to_port: 80
group_id: "{{ vproelbsg_out.group_id }}"
- proto: tcp
from_port: 22
to_port: 22
group_id: "{{ bastion_sgid }}"
register: vprostacksg_out
- name: Update Securiry Group with its own SG ID
ec2_group:
name: vprostack-sg
description: Allow port 22 from everywhere and all port within sg
region: "{{ region }}"
vpc_id: "{{ vpcid }}"
purge_rules: no
rules:
- proto: all
group_id: "{{ vprostacksg_out.group_id }}"
- name: Creating Nginx web01
ec2:
key_name: vprokey
region: "{{ region }}"
instance_type: t2.micro
image: "{{ nginx_ami }}"
wait: yes
wait_timeout: 300
instance_tags:
Name: "web01"
Project: Vprofile
Owner: DevOps Team
exact_count: 1
count_tag:
Name: "web01"
Project: Vprofile
Owner: DevOps Team
group_id: "{{ vprostacksg_out.group_id }}"
vpc_subnet_id: "{{ privsub1id }}"
register: web01_out
- name: Creating Tomcat app01
ec2:
key_name: vprokey
region: "{{ region }}"
instance_type: t2.micro
image: "{{ tomcat_ami }}"
wait: yes
wait_timeout: 300
instance_tags:
Name: "app01"
Project: Vprofile
Owner: DevOps Team
exact_count: 1
count_tag:
Name: "app01"
Project: Vprofile
Owner: DevOps Team
group_id: "{{ vprostacksg_out.group_id }}"
vpc_subnet_id: "{{ privsub1id }}"
register: app01_out
- name: Creating Memcache mc01
ec2:
key_name: vprokey
region: "{{ region }}"
instance_type: t2.micro
image: "{{ memcache_ami }}"
wait: yes
wait_timeout: 300
instance_tags:
Name: "mc01"
Project: Vprofile
Owner: DevOps Team
exact_count: 1
count_tag:
Name: "mc01"
Project: Vprofile
Owner: DevOps Team
group_id: "{{ vprostacksg_out.group_id }}"
vpc_subnet_id: "{{ privsub1id }}"
register: mc01_out
- name: Creating RabbitMQ rmq01
ec2:
key_name: vprokey
region: "{{ region }}"
instance_type: t2.micro
image: "{{ rmq_ami }}"
wait: yes
wait_timeout: 300
instance_tags:
Name: "rmq01"
Project: Vprofile
Owner: DevOps Team
exact_count: 1
count_tag:
Name: "rmq01"
Project: Vprofile
Owner: DevOps Team
group_id: "{{ vprostacksg_out.group_id }}"
vpc_subnet_id: "{{ privsub1id }}"
register: rmq01_out
- name: Creating Mysql db01
ec2:
key_name: vprokey
region: "{{ region }}"
instance_type: t2.micro
image: "{{ mysql_ami }}"
wait: yes
wait_timeout: 300
instance_tags:
Name: "db01"
Project: Vprofile
Owner: DevOps Team
exact_count: 1
count_tag:
Name: "db01"
Project: Vprofile
Owner: DevOps Team
group_id: "{{ vprostacksg_out.group_id }}"
vpc_subnet_id: "{{ privsub1id }}"
register: db01_out
- ansible.builtin.debug:
var: db01_out.tagged_instances[0].id
- local_action:
module: ec2_elb_lb
name: "vprofile-elb"
region: "{{ region }}"
state: present
instance_ids:
- "{{ web01_out.tagged_instances[0].id }}"
purge_instance_ids: true
security_group_ids: "{{ vproelbsg_out.group_id }}"
subnets:
- "{{ pubsub1id }}"
- "{{ pubsub2id }}"
- "{{ pubsub3id }}"
listeners:
- protocol: http
load_balancer_port: 80
instance_port: 80
- name: Insert/Update Hosts IP and Name in file provision-stack/group_vars/hostsip
ansible.builtin.blockinfile:
path: provision-stack/group_vars/hostsip
block: |
web01_ip: {{ web01_out.tagged_instances[0].private_ip }}
app01_ip: {{ app01_out.tagged_instances[0].private_ip }}
rmq01_ip: {{ rmq01_out.tagged_instances[0].private_ip }}
mc01_ip: {{ mc01_out.tagged_instances[0].private_ip }}
db01_ip: {{ db01_out.tagged_instances[0].private_ip }}
- name: Copy Login Key to provision-stack directory
ansible.builtin.copy:
src: loginkey_vpro.pem
dest: provision-stack/loginkey_vpro.pem
mode: "0400"
- name: Insert/Update Inventory file provision-stack/inventory-vpro
ansible.builtin.blockinfile:
path: provision-stack/inventory-vpro
block: |
web01 ansible_host={{ web01_out.tagged_instances[0].private_ip }}
app01 ansible_host={{ app01_out.tagged_instances[0].private_ip }}
rmq01 ansible_host={{ rmq01_out.tagged_instances[0].private_ip }}
mc01 ansible_host={{ mc01_out.tagged_instances[0].private_ip }}
db01 ansible_host={{ db01_out.tagged_instances[0].private_ip }}
cntl ansible_host=127.0.0.1 ansible_connection=local
[websrvgrp]
web01
[appsrvgrp]
app01
[rmqsrvgrp]
rmq01
[mcsrvgrp]
mc01
[dbsrvgrp]
db01
[control]
cntl
[stack_inst:children]
websrvgrp
appsrvgrp
rmqsrvgrp
mcsrvgrp
dbsrvgrp
[stack_inst:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=loginkey_vpro.pem
#ansible_python_interpreter=/usr/bin/python3