-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.yml
224 lines (201 loc) · 6.47 KB
/
main.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
---
- name: "Create A ASG with ELB"
hosts: localhost
vars_files:
- asg.vars
- cred.vars
tasks:
- name: "Dependancy Tasks 01-Install PIP"
yum:
name: pip
state: present
- name: "Depandancy Task 02-Install setuptools"
pip:
name: setuptools
state: present
- name: "Depandancy Task 03-Install virtualenv"
pip:
name: virtualenv
state: present
- name: "Depandancy Task 04-Install boto"
pip:
name: boto
state: present
- name: "Depandancy Task 05-Install boto3"
ignore_errors: true
pip:
name: boto3
state: present
- name: "Depandancy Task 06-Install botocore"
pip:
name: botocore
state: present
- name: "Task-01-AWS KeyPair Creation"
ec2_key:
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
region: "{{ region }}"
name: "{{ key }}"
state: present
register: key_status
- name: "Task 01.1-AWS KeyPair Copy to a PEM file"
when: key_status.changed == true
copy:
content: "{{ key_status.key.private_key }}"
dest: "{{ key }}.pem"
mode: 0400
- name: "Task 03-Create Security Group"
ec2_group:
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
region: "{{ region }}"
name: "{{ env }}_{{ sg_name }}"
description: "22,80,443,3306 Port Created by Ansible"
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
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 3306
to_port: 3306
cidr_ip: 0.0.0.0/0
register: sgweb
- debug:
var: "sgweb.group_id"
- name: "Task 04-Create Launch configuration"
ec2_lc:
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
region: "{{ region }}"
name: "{{ env }}_lc"
image_id: "{{ ami }}"
key_name: "{{ key }}"
security_groups: "{{ sgweb.group_id }}"
instance_type: "{{ type }}"
user_data_path: ./userdata.sh
register: lc_status
- debug:
var: "lc_status.name"
- name: "Task 05-Creating ELB"
ec2_elb_lb:
name: "elb"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
region: "{{ region }}"
connection_draining_timeout: 60
cross_az_load_balancing: true
security_group_ids: "{{ sgweb.group_id }}"
state: present
zones:
- "{{ region }}a"
- "{{ region }}b"
- "{{ region }}c"
- "{{ region }}d"
- "{{ region }}e"
- "{{ region }}f"
listeners:
- protocol: http
load_balancer_port: 80
instance_port: 80
health_check:
ping_protocol: http
ping_port: 80
ping_path: "/health.html"
response_timeout: 2
interval: 10
unhealthy_threshold: 2
healthy_threshold: 2
register: elb_status
- debug:
var: "elb_status.elb.dns_name"
- name: "Task 06-ASG Creation"
ec2_asg:
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
region: "{{ region }}"
name: "{{ env }}_asg"
load_balancers: "{{ elb_status.elb.name }}"
launch_config_name: "{{ lc_status.name }}"
health_check_period: 30
health_check_type: EC2
replace_all_instances: yes
min_size: "{{ count }}"
max_size: "{{ count }}"
desired_capacity: "{{ count }}"
tags:
- Name: ansible_app
propagate_at_launch: true
register: asg_status
- debug:
var: "asg_status.auto_scaling_group_name"
- name: "Task 07-Fetch ASG Created EC2 Details"
ec2_instance_info:
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
region: "{{ region }}"
filters:
"tag:aws:autoscaling:groupName": "{{ asg_status.auto_scaling_group_name }}"
instance-state-name: [ "running"]
register: asg_instances
- name: "Task 08-Autoscale - Creating Dynamic Inventory Of Autoscaling EC2"
add_host:
name: "{{ item.public_ip_address }}"
groups: "asg"
ansible_host: "{{ item.public_ip_address }}"
ansible_port: 22
ansible_user: "ec2-user"
ansible_ssh_private_key_file: "{{ key }}.pem"
ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
with_items:
- "{{ asg_instances.instances }}"
# Dyanamic Inventory Rolling Update Play
- name: "Task Dynamic Inventory Play 09-ASG Rolling Update Start"
hosts: asg
become: true
serial: 1
gather_facts: False
vars_files:
- instance.vars
tasks:
- name: "Task Dynamic Inventory Play 10-Application Updation - Cloning Git Repository"
git:
repo: "{{ git_url }}"
dest: /var/myapp/
register: git_status
- name: "Task Dynamic Inventory Play 11-Application Updation - Disabling Health Check"
when: git_status.changed == true
file:
path: /var/www/html/health.html
state: touch
mode: 0000
- name: "Task Dynamic Inventory Play 12-Application Updation - Off Loading From ElB"
when: git_status.changed == true
pause:
seconds: "{{ health_time }}"
- name: "Task Dynamic Inventory Play 13-Application Updation - Updating App Data"
when: git_status.changed == true
copy:
src: /var/myapp/
dest: /var/www/html/
owner: apache
group: apache
remote_src: true
- name: "Task Dynamic Inventory Play 14-Application Updation - Enabling Health Check"
when: git_status.changed == true
file:
path: /var/www/html/health.html
state: touch
mode: 0444
- name: "Task Dynamic Inventory Play 15-Application Updation - Loading EC2 To ELB"
when: git_status.changed == true
pause:
seconds: "{{ health_time }}"