-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.yml
245 lines (212 loc) · 5.6 KB
/
project.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
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
---
- hosts: local
connection: local
tasks:
- name: Get Client IP
uri:
url: http://checkip.amazonaws.com/
return_content: yes
register: clt_ip
- name: Create a httpd group
ec2_group:
name: httpd-server
description: A security group for access
region: ap-southeast-2
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
rules_egress:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
register: httpd_group
tags:
create_httpd_group
- name: Create ELB
ec2_elb_lb:
name: httpd-balancer
state: present
region: ap-southeast-2
zones:
- ap-southeast-2a
- ap-southeast-2b
listeners:
- protocol: http
load_balancer_port: 80
instance_port: 80
register: elb
tags:
create_elb
- action:
module: ec2_elb_facts
region: ap-southeast-2
register: elb_facts
- local_action:
module: copy
content: "{{ item.dns_name }}"
dest: url_loadbalance_httpd.txt
with_items: "{{ elb_facts.elbs }}"
- name: Generate a EC2 Key
ec2_key:
name: ec2key
region: ap-southeast-2
register: ec2_key_result
tags:
generate_ec2_key
- name: Save key
copy: content="{{ ec2_key_result.key.private_key }}" dest="./ec2key" mode=0600
when: ec2_key_result.changed
tags:
save_key
- name: Create EC2
ec2:
key_name: ec2key
instance_type: t2.micro
image: ami-04481c741a0311bbb
region: ap-southeast-2
group_id:
- "{{ httpd_group.group_id }}"
- "{{ elb.elb.security_group_ids | first }}"
wait: yes
instance_tags:
application: WebApp-AWS
exact_count: 1
count_tag:
application: WebApp-AWS
register: ec2_server
tags:
create_ec2
- name: Get EC2 information
add_host:
hostname: "{{ item.public_ip }}"
groupname: ec2_server_ip
with_items: "{{ ec2_server.tagged_instances }}"
- name: Test server connection
wait_for:
host: "{{ item.public_ip }}"
port: 22
timeout: 300
with_items: "{{ ec2_server.tagged_instances }}"
tags:
test_connection
- name: Include EC2 keys to known hosts
known_hosts:
name: "{{ item.public_ip }}"
key: "{{ lookup('pipe', 'ssh-keyscan -t rsa ' + item.public_ip) }}"
with_items: "{{ ec2_server.tagged_instances }}"
- name: Add pem to local ssh
local_action:
module: shell
ssh-add ec2key
- name: Create a new disk
ec2_vol:
region: ap-southeast-2
instance: "{{ item.id }}"
volume_size: 5
device_name: /dev/xvdg
with_items: "{{ ec2_server.tagged_instances }}"
tags:
create_disk
- hosts: ec2_server_ip
remote_user: ec2-user
become: True
tasks:
- name: Modify Sysctl to disable IPV6
shell:
echo -e "net.ipv6.conf.all.disable_ipv6 = 1\nnet.ipv6.conf.default.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.conf
- name: Reload values for Sysctl
shell:
sudo sysctl -p
- name: Include Address Family to sshd
shell:
echo "AddressFamily inet" | sudo tee -a /etc/ssh/sshd_config
- name: Restart SSHD
systemd:
name: sshd
state: restarted
- name: Install lvm packages
yum:
name: 'lvm*'
state: present
- name: Create a VG
lvg:
vg: vg
pvs: /dev/xvdg
state: present
- name: Create a LV on top of the new VG
lvol:
vg: vg
lv: lv01
size: 100%FREE
- name: Format the volume with XFS
filesystem:
fstype: xfs
dev: /dev/mapper/vg-lv01
- name: Mount a new fileshare
mount:
path: /var/www/html/
src: /dev/mapper/vg-lv01
fstype: xfs
state: mounted
- name: Install httpd
yum:
name: httpd
state: present
- name: Install git
yum:
name: git-all
state: present
- name: Clonar repositorio GIT
git:
repo: 'https://github.com/Renatovnctavares/WebServer-AWS-byAnsible.git'
dest: /var/www/html/
- name: Restart httpd
systemd:
name: httpd
state: restarted
- name: Disable SELinux
shell:
sudo setenforce 0 && sudo sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &
- name: Stop auditd
shell:
sudo service auditd stop
- name: Disable auditd
shell:
sudo systemctl disable auditd
- hosts: local
connection: local
tasks:
- name: Test server connection again
wait_for:
delay: 30
host: "{{ item.public_ip }}"
port: 22
timeout: 300
with_items: "{{ ec2_server.tagged_instances }}"
tags:
test_connection2
- hosts: ec2_server_ip
remote_user: ec2-user
become: True
tasks:
- name: Restart httpd service
systemd:
name: httpd
state: restarted
- hosts: local
connection: local
tasks:
- name: Adding EC2 to ELB
ec2_elb:
state: present
ec2_elbs: httpd-balancer
region: ap-southeast-2
instance_id: "{{ item.id }}"
with_items: "{{ ec2_server.tagged_instances }}"