-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.yml
134 lines (116 loc) · 3.22 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
---
- name: "fetching instance details"
become: false
hosts: localhost
vars:
region: "ap-south-1"
asg_name: "asg-rolling" #name of autoscaling group
tasks:
- name: "gathering instance details"
amazon.aws.ec2_instance_info:
region: "{{ region }}"
filters:
"tag:aws:autoscaling:groupName": "{{ asg_name }}"
"tag:Project": "zomato"
instance-state-name: [ "running" ]
register: instance_details
- name: "creating dynamic inventory"
add_host:
groups: "asg_instances"
hostname: "{{ item.public_ip_address }}"
ansible_ssh_user: "ec2-user"
ansible_ssh_host: '{{ item.public_ip_address }}'
ansible_ssh_port: "22"
ansible_ssh_private_key_file: "aws.pem"
ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
loop: "{{ instance_details.instances }}"
- name: "Deploying a site from github repo"
hosts: all
become: true
serial: 1
vars:
repo_url: https://github.com/sreehariskumar/aws-elb-site.git
httpd_owner: "apache"
httpd_group: "apache"
httpd_port: "80"
httpd_domain: "shopping.1by2.online"
health_check_delay: 40
packages:
- httpd
- php
- git
clone_dir: "/var/website/"
tasks:
- name: "installing packages"
yum:
name: "{{ packages }}"
state: present
- name: "creating conf from template"
template:
src: "./httpd.conf.j2"
dest: "/etc/httpd/conf/httpd.conf"
owner: "{{ httpd_owner }}"
group: "{{ httpd_group }}"
notify:
- apache-reload
- name: "creating virtualhost from template"
template:
src: "./virtualhost.conf.j2"
dest: "/etc/httpd/conf.d/{{ httpd_domain }}.conf"
owner: "{{ httpd_owner }}"
group: "{{ httpd_group }}"
notify:
- apache-reload
- name: "creating document root"
file:
path: "/var/www/html/{{ httpd_domain }}"
state: directory
owner: "{{ httpd_owner }}"
group: "{{ httpd_group }}"
- name: "creating cloning directory"
file:
path: "{{ clone_dir }}"
state: directory
- name: "cloning from repo"
git:
repo: "{{ repo_url }}"
dest: "{{ clone_dir }}"
register: clone_status
notify:
- apache-restart
- up-delay
- name: "stopping instances"
when: clone_status.changed
service:
name: httpd
state: stopped
notify:
- apache-restart
- up-delay
- name: "connection drain waiting"
when: clone_status.changed
wait_for:
timeout: "{{ health_check_delay }}"
- name: "copying contents to document root"
when: clone_status.changed
copy:
src: "{{ clone_dir }}"
dest: "/var/www/html/{{ httpd_domain }}"
remote_src: true
notify:
- apache-restart
- up-delay
handlers:
- name: "apache-restart"
service:
name: httpd
state: restarted
enabled: true
- name: "apache-reload"
service:
name: httpd
state: reloaded
enabled: true
- name: "up-delay"
wait_for:
timeout: "40"