-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathticket-monster-aws.yml
245 lines (214 loc) · 6.79 KB
/
ticket-monster-aws.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
---
- name: Create new EC2 instances
hosts: localhost
gather_facts: false
connection: local
vars:
region: us-east-1
ami: ami-81cf0c97
keypair: "{{ ec2_keypair_name | default('aws-jmarc') }}"
set_dns: False
count: 2
tags_db:
env: "{{ tags_env | default('prod') }}"
type: "{{ tags_type | default('db') }}"
tags_app:
env: "{{ tags_env | default('prod') }}"
type: "{{ tags_type | default('app') }}"
wait_for_ssh: False
provider_id: "{{ provider_id }}"
max_retries: 9
retry_interval: 20
miq_username: "{{ miq_username }}"
miq_password: "{{ miq_password }}"
tasks:
- name: Launch DB instance
local_action:
module: ec2
keypair: "{{ keypair }}"
instance_type: "{{ type | default('t2.micro') }}"
vpc_subnet_id: "subnet-{{ vpc_subnet | default('d931e1e5') }}"
image: "{{ ami }}"
region: "{{ region }}"
count: 1
wait: yes
instance_tags: "{{ tags_db }}"
assign_public_ip: "{{ assign_eip | default(true) }}"
register: ec2_db
- name: Launch App instances
local_action:
module: ec2
keypair: "{{ keypair }}"
instance_type: "{{ type | default('t2.micro') }}"
vpc_subnet_id: "subnet-{{ vpc_subnet | default('d931e1e5') }}"
image: "{{ ami }}"
region: "{{ region }}"
count: "{{ count }}"
wait: yes
instance_tags: "{{ tags_app }}"
assign_public_ip: "{{ assign_eip | default(true) }}"
register: ec2_app
- name: Wait for SSH to come up
wait_for:
host: "{{ item.public_dns_name }}"
port: 22
delay: 60
timeout: 320
state: started
with_items:
- "{{ ec2_db.instances }}"
- "{{ ec2_app.instances }}"
- name: Instance info
debug:
msg: "{{ item.id }} {{ item.public_ip }}"
with_items:
- "{{ ec2_db.instances }}"
- "{{ ec2_app.instances }}"
- name: Add DB instances to a group
add_host:
name: "{{ item.public_ip }}"
groups: db_provisioned
with_items: "{{ ec2_db.instances }}"
- name: Add App instances to a group
add_host:
name: "{{ item.public_ip }}"
groups: app_provisioned
with_items: "{{ ec2_app.instances }}"
- name: Set the Provider URL
set_fact:
provider_url: "{{ manageiq.api_url }}/api/providers/{{ provider_id }}"
- name: Refresh Amazon EC2 provider in CloudForms
uri:
url: "{{ provider_url }}"
method: POST
user: "{{ miq_username }}"
password: "{{ miq_password }}"
body:
action: refresh
body_format: json
validate_certs: False
headers:
#X-Auth-Token: "{{ manageiq.api_token }}"
Content-Type: "application/json"
status_code: 200
register: output
- name: Set the task href
set_fact:
task_url: "{{ output.json.task_href}}"
- name: Wait for the provider refresh to end
uri:
url: "{{ task_url }}"
method: GET
user: "{{ miq_username }}"
password: "{{ miq_password }}"
validate_certs: False
headers:
#X-Auth-Token: "{{ manageiq.api_token }}"
Content-Type: "application/json"
status_code: 200
register: task_result
until: task_result.json.state == 'Finished' or task_result.json.status == 'Error'
failed_when: task_result.json.status == 'Error'
retries: "{{max_retries}}"
delay: "{{retry_interval}}"
- name: Lookup instance href
uri:
url: "{{ manageiq.api_url }}/api/vms?filter[]=name={{ item.id }}&expand=resources"
method: GET
user: "{{ miq_username }}"
password: "{{ miq_password }}"
body:
action: refresh
body_format: json
validate_certs: False
headers:
#X-Auth-Token: "{{ manageiq.api_token }}"
Content-Type: "application/json"
status_code: 200
register: output
with_items:
- "{{ ec2_db.instances }}"
- "{{ ec2_app.instances }}"
- name: Set the Service URL
set_fact:
svc_url: "/api/{{ manageiq.service }}"
- name: Initialize an empty list for vms
set_fact:
vms: []
- name: Append resource href to vms list
set_fact:
vms: "{{ vms }} + [ { 'href': svc_url, 'resource': { 'href': '/api/vms/{{ item.json.resources[0].id }}' } } ]"
with_items: "{{ output.results }}"
- debug: var=vms
- name: Register vms with the service
uri:
url: "{{ manageiq.api_url }}/api/services"
method: POST
user: "{{ miq_username }}"
password: "{{ miq_password }}"
body_format: json
body:
action: add_resource
resources: "{{ vms }}"
validate_certs: False
headers:
#X-Auth-Token: "{{ manageiq.api_token }}"
Content-Type: "application/json"
status_code: 200
register: output
- debug: var=output.json.results[0].success
- name: Check if the VM was successfully attached to service
fail: msg="{{output.json.results[0].message}}"
when: output.json.results[0].success == false
- hosts: db_provisioned
name: Deploy PostgreSQL
user: ec2-user
become: true
gather_facts: true
vars:
postgresql_pg_hba_conf_default:
- { type: local, database: all, user: postgres, address: "", method: peer }
- { type: host, database: all, user: all, address: "127.0.0.1/32", method: md5 }
- { type: host, database: all, user: all, address: "::1/128", method: md5 }
- { type: host, database: all, user: all, address: "0.0.0.0/0", method: md5 }
postgresql_databases:
- name: ticketmonster
postgresql_users:
- name: test
password: test
roles:
- { role: sfromm.postgresql }
post_tasks:
- name: Configure TCP/IP Client Authorizations
lineinfile: dest=/var/lib/pgsql/data/postgresql.conf regexp=^listen_addresses line=listen_addresses='*'
become: true
- name: Restart PostgreSQL
service: name=postgresql state=restarted
become: true
- hosts: app_provisioned
name: Deploy Ticket Monster
user: ec2-user
become: true
gather_facts: true
vars:
region: us-east-1
pre_tasks:
- name: Get ext_database_name
set_fact:
ext_database_name: "{{ hostvars[item]['inventory_hostname'] }}"
with_items:
- "{{ groups['db_provisioned'] }}"
roles:
- jboss
tasks:
- ec2_facts:
- local_action:
module: ec2_elb
ec2_region: "{{ region }}"
instance_id: "{{ ansible_ec2_instance_id }}"
ec2_elbs: "ticketmonster"
state: present
wait: yes
wait_timeout: 60
become: false
ignore_errors: yes