-
Notifications
You must be signed in to change notification settings - Fork 2
/
bastion_lb_web_TLS-Terminated.yml
481 lines (435 loc) · 14.6 KB
/
bastion_lb_web_TLS-Terminated.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
---
# FUJITSU Cloud Service for OSS IaaS Design and Configuration Guide
# Create network, router, sg, keypair and a bastion instance
- name: create network and bastion server
hosts: localhost
connection: local
gather_facts: no
vars_files:
- bastion_lb_web_TLS-Terminated_vars.yml
environment: "{{ proxy_env }}"
tasks:
- name: create a network
os_network:
name: "{{ network_name }}"
cloud: "{{ target_cloud | default(omit) }}"
state: present
- name: create a subnet
os_subnet:
name: "{{ subnet_name }}"
network_name: "{{ network_name }}"
cidr: "{{ subnet_cidr }}"
dns_nameservers: "{{ dns_servers }}"
cloud: "{{ target_cloud | default(omit) }}"
state: present
register: subnet
- name: create a router
os_router:
name: "{{ router_name }}"
network: "{{ router_attached_network }}"
interfaces:
- "{{ subnet_name }}"
cloud: "{{ target_cloud | default(omit) }}"
state: present
- name: create SSH security group
os_security_group:
name: ssh
cloud: "{{ target_cloud | default(omit) }}"
state: present
- name: add rule to allow SSH connections
os_security_group_rule:
security_group: ssh
protocol: tcp
port_range_min: 22
port_range_max: 22
remote_ip_prefix: 0.0.0.0/0
cloud: "{{ target_cloud | default(omit) }}"
state: present
- name: create internal security group
os_security_group:
name: internal
cloud: "{{ target_cloud | default(omit) }}"
state: present
- name: add rule for internal connection (SSH and HTTP)
os_security_group_rule:
security_group: internal
direction: "{{ item[0] }}"
protocol: tcp
port_range_min: "{{ item[1] }}"
port_range_max: "{{ item[1] }}"
remote_group: internal
cloud: "{{ target_cloud | default(omit) }}"
state: present
with_nested:
- [ "ingress", "egress" ]
- [ 22, 80 ]
- name: add rule to allow HTTP connections(internal, for LBaaS)
os_security_group_rule:
security_group: internal
protocol: tcp
port_range_min: 80
port_range_max: 80
remote_ip_prefix: "{{ subnet_cidr }}"
cloud: "{{ target_cloud | default(omit) }}"
state: present
- name: register an public key as keypair
os_keypair:
name: "{{ keypair_name }}"
cloud: "{{ target_cloud | default(omit) }}"
public_key_file: "{{ keypair_public_key_file_path }}"
state: present
- name: create bastion instance and boot from the volume
os_server:
name: "{{ bastion_name }}"
flavor: "{{ bastion_flavor }}"
key_name: "{{ keypair_name }}"
image: "{{ volume_image }}"
volume_size: "{{ volume_size }}"
boot_from_volume: yes
terminate_volume: yes
security_groups:
- default
- ssh
- internal
network: "{{ network_name }}"
floating_ip_pools: "{{ router_attached_network }}"
userdata: "{{ server_userdata | default(omit) }}"
meta: "{{ server_meta | default(omit) }}"
timeout: 600
cloud: "{{ target_cloud | default(omit) }}"
state: present
- name: retrieve facts about the bastion instance
os_server_facts:
server: "{{ bastion_name }}"
cloud: "{{ target_cloud | default(omit) }}"
register: nova_bastion
- name: add bastion to inventory
add_host:
name: "{{ item.name }}"
groups: bastion
ansible_host: "{{ item.accessIPv4 }}"
ansible_user: "{{ cloud_user_name }}"
ansible_ssh_private_key_file: "{{ keypair_private_key_file_path }}"
ansible_ssh_extra_args: "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
with_items: "{{ nova_bastion.ansible_facts.openstack_servers }}"
changed_when: False
- name: keep bastion ip address
set_fact:
bastion_ip: "{{ nova_bastion.ansible_facts.openstack_servers[0].accessIPv4 }}"
# Setup bastion
- name: wait for bastion port 22 to be ready
hosts: bastion
gather_facts: no
tasks:
- name: wait 300 seconds for bastion port 22 to become open
wait_for_connection:
delay: 10
timeout: 300
- name: perform yum update on bastion
hosts: bastion
vars_files:
- bastion_lb_web_TLS-Terminated_vars.yml
become: yes
tasks:
- name: perform yum update on bastion
yum:
name: '*'
state: latest
- name: restart the bastion instance
hosts: localhost
connection: local
gather_facts: no
vars_files:
- bastion_lb_web_TLS-Terminated_vars.yml
environment: "{{ proxy_env }}"
tasks:
- name: stop the bastion instance
os_server_action:
action: stop
server: "{{ item.name }}"
cloud: "{{ target_cloud | default(omit) }}"
timeout: 180
with_items: "{{ nova_bastion.ansible_facts.openstack_servers }}"
- name: start the bastion instance
os_server_action:
action: start
server: "{{ item.name }}"
cloud: "{{ target_cloud | default(omit) }}"
timeout: 180
with_items: "{{ nova_bastion.ansible_facts.openstack_servers }}"
# Create servergroup and webserver instances
- name: create webservers
hosts: localhost
connection: local
gather_facts: no
vars_files:
- bastion_lb_web_TLS-Terminated_vars.yml
environment: "{{ proxy_env }}"
tasks:
- name: create server group
os_server_group:
name: "{{ server_group_name }}"
policies:
- anti-affinity
cloud: "{{ target_cloud | default(omit) }}"
state: present
register: server_group_result
- name: create webserver instances and boot from the volume
os_server:
name: "{{ webserver_name_prefix }}-{{ item }}"
flavor: "{{ webserver_flavor }}"
key_name: "{{ keypair_name }}"
image: "{{ volume_image }}"
volume_size: "{{ volume_size }}"
boot_from_volume: yes
terminate_volume: yes
security_groups:
- default
- internal
network: "{{ network_name }}"
auto_ip: no
userdata: "{{ server_userdata | default(omit) }}"
meta: "{{ server_meta | default(omit) }}"
cloud: "{{ target_cloud | default(omit) }}"
state: present
scheduler_hints:
group: "{{ server_group_result.id }}"
with_sequence: "count={{ num_webservers }}"
- name: retrieve facts about the webserver instances
os_server_facts:
server: "{{ webserver_name_prefix }}-*"
cloud: "{{ target_cloud | default(omit) }}"
register: nova_webservers
- name: keep webserver ip private addresses
set_fact:
webserver_private_ips: "{{ nova_webservers.ansible_facts.openstack_servers | map(attribute='private_v4') | list }}"
- name: add webservers to inventory
add_host:
name: "{{ item.name }}"
groups: webservers
ansible_host: "{{ item.private_v4 }}"
ansible_user: "{{ cloud_user_name }}"
ansible_ssh_private_key_file: "{{ keypair_private_key_file_path }}"
ansible_ssh_extra_args: "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
ansible_ssh_common_args: '-o ProxyCommand="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i {{ keypair_private_key_file_path }} -W %h:%p {{ cloud_user_name }}@{{ bastion_ip }}"'
with_items: "{{ nova_webservers.ansible_facts.openstack_servers }}"
changed_when: False
# Setup webservers
- name: wait for webserver port 22 to be ready
hosts: webservers
gather_facts: no
tasks:
- name: wait 300 seconds for webservers port 22 to become open
wait_for_connection:
delay: 10
timeout: 300
- name: perform yum update and httpd installation on webservers
hosts: webservers
vars_files:
- bastion_lb_web_TLS-Terminated_vars.yml
become: yes
tasks:
- name: perform yum update on webservers
yum:
name: '*'
state: latest
- name: install the latest version of httpd
yum:
name: httpd
state: latest
- name: enable httpd service
systemd:
name: httpd
state: started
enabled: yes
- name: put a simple web page
copy:
content: 'FUJITSU Cloud Service for OSS on {{ ansible_ssh_host }}'
dest: /var/www/html/index.html
owner: root
group: root
mode: 0644
- name: restart the webserver instances
hosts: localhost
connection: local
gather_facts: no
vars_files:
- bastion_lb_web_TLS-Terminated_vars.yml
environment: "{{ proxy_env }}"
tasks:
- name: stop the webserver instances
os_server_action:
action: stop
server: "{{ item.name }}"
cloud: "{{ target_cloud | default(omit) }}"
timeout: 180
with_items: "{{ nova_webservers.ansible_facts.openstack_servers }}"
- name: start the webserver instances
os_server_action:
action: start
server: "{{ item.name }}"
cloud: "{{ target_cloud | default(omit) }}"
timeout: 180
with_items: "{{ nova_webservers.ansible_facts.openstack_servers }}"
# Create and setup loadbalancer
- name: create and setup loadbalancer
hosts: localhost
connection: local
gather_facts: no
vars_files:
- bastion_lb_web_TLS-Terminated_vars.yml
- clouds.yml
environment:
- OS_AUTH_URL: "{{ clouds[target_cloud]['auth']['auth_url'] }}"
- OS_DOMAIN_NAME: "{{ clouds[target_cloud]['auth']['user_domain_name'] }}"
- OS_USER_DOMAIN_NAME: "{{ clouds[target_cloud]['auth']['user_domain_name'] }}"
- OS_USERNAME: "{{ clouds[target_cloud]['auth']['username'] }}"
- OS_PASSWORD: "{{ clouds[target_cloud]['auth']['password'] }}"
- OS_PROJECT_ID: "{{ clouds[target_cloud]['auth']['project_id'] }}"
- OS_IDENTITY_API_VERSION: "{{ clouds[target_cloud]['identity_api_version'] }}"
- "{{ proxy_env }}"
tasks:
- name: create loadbalancer/listener/pool
os_loadbalancer:
name: "{{ lb_name }}"
vip_subnet: "{{ subnet.id }}"
state: present
auto_public_ip: yes
public_network: "{{ router_attached_network }}"
listeners:
- name: "{{ listener_name }}"
protocol: "{{ listener_protocol }}"
protocol_port: "{{ listener_protocol_port }}"
pool:
name: "{{ pool_name }}"
cloud: "{{ target_cloud | default(omit) }}"
- name: create members
os_member:
name: "member-{{ item }}"
pool: "{{ pool_name }}"
address: "{{ item }}"
subnet_id: "{{ subnet.id }}"
state: present
cloud: "{{ target_cloud | default(omit) }}"
with_items: "{{ webserver_private_ips }}"
- name: create a healthmonitor
command: >
openstack
loadbalancer
healthmonitor
create
--name "{{ hm_name }}"
--delay "{{ delay_time }}"
--timeout "{{ timeout }}"
--max-retries "{{ max_retries }}"
--type "{{ hm_type }}"
--url-path "{{ hm_path }}"
"{{ pool_name }}"
- name: setup tls-terminated loadbalancer
block:
- name: get secrets
command: >
openstack
secret
list
--format json
register: secrets
changed_when: False
- name: set list of existing secret names
set_fact:
secret_list: "{{ secret_list | default([]) + [ item.Name ] }}"
with_items: "{{ secrets.stdout }}"
- name: cat server.crt
command: >
cat
"{{ crt_file }}"
register: cat_server_crt
changed_when: False
when: crt_name not in secret_list
- name: create secret (certificate)
command: >
openstack
secret store
--payload-content-type='text/plain'
--name="{{ crt_name }}"
--payload="{{ cat_server_crt.stdout }}"
when: crt_name not in secret_list
- name: cat server.key
command: >
cat
"{{ key_file }}"
register: cat_server_key
changed_when: False
when: key_name not in secret_list
- name: create secret (private_key)
command: >
openstack
secret store
--payload-content-type='text/plain'
--name="{{ key_name }}"
--payload="{{ cat_server_key.stdout }}"
when: key_name not in secret_list
- name: get secrets
command: >
openstack
secret
list
--format json
register: secrets
changed_when: False
- name: set fact for certificate
set_fact:
crt_url: "{{ item['Secret href'] }}"
with_items: "{{ secrets.stdout }}"
when: item.Name == crt_name
- name: set fact for private_key
set_fact:
key_url: "{{ item['Secret href'] }}"
with_items: "{{ secrets.stdout }}"
when: item.Name == key_name
- name: get secret containers
command: >
openstack
secret
container
list
--format json
register: containers
changed_when: False
- name: set list of existing secret containers names
set_fact:
container_list: "{{ container_list | default([]) + [ item.Name ] }}"
with_items: "{{ containers.stdout }}"
- name: create secret container
command: >
openstack
secret container create
--name="{{ container_name }}"
--type='certificate'
--secret="certificate={{ crt_url }}"
--secret="private_key={{ key_url }}"
when: container_name not in container_list
- name: get secret containers
command: >
openstack
secret
container
list
--format json
register: containers
changed_when: False
- name: set fact for secret container
set_fact:
container_url: "{{ item['Container href'] }}"
with_items: "{{ containers.stdout }}"
when: item.Name == container_name
- name: set default TLS container ref
command: >
openstack
loadbalancer
listener
set
--default-tls-container-ref
"{{ container_url }}"
"{{ listener_name }}"
when: listener_protocol == 'TERMINATED_HTTPS'