-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.yml
216 lines (188 loc) · 5.12 KB
/
deploy.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
---
- name: Create VPC and EC2
hosts: localhost
connection: local
gather_facts: no
vars_files:
- vars.yml
tasks:
#-------- Deploy VPC ----------
- name: Create VPC
amazon.aws.ec2_vpc_net:
name: "{{ vpc_name }}"
cidr_block: "{{ vpc_cidr_block }}"
region: "{{ region }}"
dns_hostnames: yes
dns_support: yes
multi_ok: no
tags:
Name: "{{ vpc_name }}"
state: present
register: vpc
- name: Add VPC ID to vars.yml
ansible.builtin.lineinfile:
path: ./vars.yml
regexp: 'vpc_id: '
line: 'vpc_id: {{vpc.vpc.id}}'
state: present
create: True
- name: Create Subnets
amazon.aws.ec2_vpc_subnet:
vpc_id: "{{ vpc.vpc.id }}"
cidr: "{{ subnet_cidr_block }}"
region: "{{ region }}"
az: "{{ region }}a"
state: present
wait: yes
register: subnet
with_items:
- {subnet_name: "{{vpc_name}}_public"}
- name: Add Subnet ID to vars.yml
ansible.builtin.lineinfile:
path: ./vars.yml
regexp: 'subnet_id: '
line: 'subnet_id: {{subnet.results[0].subnet.id}}'
state: present
create: True
- name: Create Internet Gateway
amazon.aws.ec2_vpc_igw:
vpc_id: "{{ vpc.vpc.id }}"
region: "{{ region }}"
tags:
Name: "{{ vpc_name }}_IGW"
state: present
register: igw
- name: Create Route Table
amazon.aws.ec2_vpc_route_table:
vpc_id: "{{ vpc.vpc.id }}"
tags:
Name: "{{ vpc_name }}_RT"
region: "{{region}}"
subnets:
- "{{subnet.results[0].subnet.id}}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{igw.gateway_id}}"
state: present
register: rt
#-------- Deploy Server ----------
- name: Create security group
amazon.aws.ec2_group:
name: "{{ security_group_name }}"
description: "My security group"
vpc_id: "{{vpc.vpc.id}}"
region: "{{ region }}"
rules:
- proto: tcp
ports:
- 80
- 22
cidr_ip: 0.0.0.0/0
state: present
register: sg
- name: Add SG ID to vars.yml
ansible.builtin.lineinfile:
path: ./vars.yml
regexp: 'sg_id: '
line: 'sg_id: {{sg.group_id}}'
state: present
create: True
- name: Launch EC2 instance
amazon.aws.ec2_instance:
key_name: "{{ key_name }}"
instance_type: "{{ instance_type }}"
image_id: "{{ ami_id }}"
region: "{{ region }}"
tags:
Name: "{{server_name}}"
Environment: "{{env}}"
exact_count: 1
wait_timeout: 300
security_group: "{{ sg_id }}"
vpc_subnet_id: "{{ subnet_id }}"
network:
assign_public_ip: yes
state: running
wait: yes
register: ec2
- name: Add EC2 ID to vars.yml
ansible.builtin.lineinfile:
path: ./vars.yml
regexp: 'ec2_id: '
line: 'ec2_id: {{ec2.instance_ids[0]}}'
state: present
create: True
- name: Get ip of EC2
amazon.aws.ec2_instance_info:
filters:
"tag:Name": "{{server_name}}"
instance-state-name: ["running"]
register: ec2_info
- name: Add EC2 IP to vars.yml
ansible.builtin.lineinfile:
path: ./vars.yml
regexp: 'ec2_ip: '
line: 'ec2_ip: {{ec2.instances[0].public_ip_address}}'
state: present
create: True
- name: Wait for SSH
wait_for:
host: "{{ec2_ip}}"
port: 22
timeout: 300
- name: Add EC2 instance to inventory
add_host:
name: "{{ ec2_ip }}"
groups: ec2_hosts
#-------- Installations ----------
- name: Installations
hosts: ec2_hosts
user: ec2-user
become: yes
gather_facts: no
vars_files:
- vars.yml
vars:
ansible_host_key_checking: false
ansible_ssh_private_key_file: "{{ private_key_path }}"
tasks:
- name: Install dependencies
yum:
name:
- amazon-linux-extras
- docker
- git
state: present
- name: Create Docker group if it does not exist
group:
name: docker
state: present
- name: Add EC2 user to Docker group
user:
name: ec2-user
groups: docker
append: yes
- name: Start docker
service:
name: docker
state: started
enabled: yes
- name: Install Docker Compose
get_url:
url: https://github.com/docker/compose/releases/download/1.29.2/docker-compose-Linux-x86_64
dest: /usr/local/bin/docker-compose
mode: '0755'
- name: Clone Git repository
git:
repo: "{{ git_repo_url }}"
dest: "{{ git_repo_dest }}"
version: main
force: yes
- name: Docker compose up
shell: |
/usr/local/bin/docker-compose up -d
args:
chdir: "{{ git_repo_dest }}"
- name: Print docker compose output
debug:
msg: "app is running on http://{{ec2_ip}}"