-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.yaml
351 lines (309 loc) · 9.61 KB
/
deploy.yaml
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
---
- hosts: localhost
tasks:
- name: create network
os_network:
name: "{{ network_name }}"
- name: create subnet
os_subnet:
network_name: "{{ network_name }}"
name: "{{ subnet_name }}"
cidr: 192.168.0.0/24
dns_nameservers:
- "{{ dns_server_1 }}"
- "{{ dns_server_2 }}"
register: subnet
- name: create router
os_router:
name: "{{ router_name }}"
network: "{{ external_network_name }}"
interfaces:
- "{{ subnet_name }}"
# Enable if you experience problems (re-creating) the router
# enable_snat: yes # cf. https://github.com/ansible/ansible/issues/29903
register: router
- name: Set variables
set_fact:
state: present
routeruuid: "{{ router.id }}"
subnetuuid: "{{ subnet.id }}"
- name: Create OpenStack instance
os_server:
security_groups: "{{security_group}}"
name: "{{ nodes_name }}{{ item }}"
image: "{{ nodes_image }}"
key_name: "{{ key_name }}"
flavor_ram: "{{ nodes_flavor_ram if not nodes_flavor_name else omit }}"
flavor: "{{ nodes_flavor_name if nodes_flavor_name else omit }}"
nics:
- net-name: "{{ network_name }}"
floating_ip_pools: "{{ floating_ip_pools }}"
userdata: |
#cloud-config
package_upgrade: true
hostname: "{{ nodes_name }}{{ item }}"
manage_etc_hosts: false
packages:
- python3
when: state == "present"
register: "instances"
with_sequence: count={{ nodes_count }}
- name: Remove any existing public keys in local '{{ ssh_known_hosts_file }}'
known_hosts:
name: "{{ item.openstack.accessIPv4 }}"
state: "absent"
path: "{{ ssh_known_hosts_file }}"
with_items: "{{ instances.results }}"
- name: Update inventory
add_host:
name: "{{ item.server.name }}"
ansible_ssh_host: "{{ item.openstack.accessIPv4 }}"
ansible_ssh_user: ubuntu
groupname: nodes
with_items: "{{ instances.results }}"
when: state == "present"
- name: Save inventory to file
template:
src: files/inventory-out-template.j2
dest: generated-server-list.txt
when: state == "present"
- hosts: all
gather_facts: False
tasks:
- name: Wait during nodes boot
wait_for:
host: "{{ hostvars[item]['ansible_ssh_host'] }}"
port: 22
connect_timeout: 20
timeout: 600
search_regex: OpenSSH
vars:
ansible_connection: local
with_items: "{{ groups.nodes }}"
- hosts: all
become: yes
become_user: root
gather_facts: False
tasks:
- name: Wait for cloud-init to finish
raw: while ! test -f /var/lib/cloud/instance/boot-finished; do sleep 1; done
retries: 5
delay: 1
tags:
- skip_ansible_lint
- name: Symlink /usr/bin/python -> /usr/bin/python3
raw: |
if [ -f /usr/bin/python3 ] && [ ! -f /usr/bin/python ]; then
ln --symbolic /usr/bin/python3 /usr/bin/python;
fi
become: true
- hosts: localhost
connection: local
tasks:
- name: For each host, scan for its ssh public key
shell: "ssh-keyscan -trsa {{ hostvars[item]['ansible_ssh_host'] }}"
with_items: "{{ groups.nodes }}"
register: ssh_known_host_results
until: ssh_known_host_results.stdout != ''
retries: 75
ignore_errors: yes
- debug: msg="{{ groups.nodes }}"
- name: Add/update the public key in the '{{ ssh_known_hosts_file }}'
known_hosts:
name: "{{ hostvars[item.item]['ansible_ssh_host'] }}"
key: "{{ item.stdout }}"
state: "present"
path: "{{ ssh_known_hosts_file }}"
with_items: "{{ ssh_known_host_results.results }}"
- hosts: all
become: yes
become_user: root
tasks:
- name: Enable swap file
shell: |
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
args:
creates: /swapfile
- name: MS VS Code - install apt key
become: yes
apt_key:
url: "https://packages.microsoft.com/keys/microsoft.asc"
state: present
- name: MS VS Code - add repo (apt)
become: yes
apt_repository:
repo: deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main
filename: vscode
state: present
- name: Install packages
apt:
name: [
# Remote desktop
"nginx",
"x11vnc",
"xvfb",
"novnc",
"xrdp",
"net-tools", # required by novnc
# Desktop environment
"lightdm",
"xfce4",
"xarchiver",
"chromium-browser",
"code", # ms vs code
# Software development
"nodejs",
"npm",
"docker.io",
"python3",
"python3-pip",
"conntrack", # minikube
"maven",
"git",
"libpostgresql-jdbc-java",
"openjdk-8-jdk",
"openjdk-8-demo",
"openjdk-8-doc",
"openjdk-8-jre-headless",
"openjdk-8-source",
]
state: present
update_cache: yes
# https://github.com/huashengdun/webssh
- name: Install web ssh
pip:
name: ["webssh", "pyOpenSSL", "jupyter"]
#-------------------------------------------------------
# Generate self-signed certificate
#-------------------------------------------------------
- name: Create certificate directory
file:
path: /etc/ssl/private/
state: directory
mode: 0755
- name: Generate an OpenSSL private key (requires pyOpenSSL python package).
openssl_privatekey:
path: /etc/ssl/private/privkey.pem
- name: Generate an OpenSSL CSR
openssl_csr:
path: /etc/ssl/private/bla.csr
privatekey_path: /etc/ssl/private/privkey.pem
common_name: "bla"
- name: Generate a Self Signed OpenSSL certificate
openssl_certificate:
path: /etc/ssl/private/bla.cert
privatekey_path: /etc/ssl/private/privkey.pem
csr_path: /etc/ssl/private/bla.csr
provider: selfsigned
- name: Create combined key/cert file for novnc
shell: cat /etc/ssl/private/bla.cert /etc/ssl/private/privkey.pem > /etc/ssl/private/combined.pem
args:
creates: /etc/ssl/private/combined.pem
#-------------------------------------------------------
# Configure password-based login for user ubuntu
#-------------------------------------------------------
- name: Allow password-based login
lineinfile:
path: /etc/ssh/sshd_config
regexp: "^PasswordAuthentication no$"
line: "PasswordAuthentication yes"
owner: root
group: root
- name: Change Password for user ubuntu
user: name=ubuntu password="{{ (lookup('env','NODE_PASSWORD') | default('bla', true)) | password_hash('sha512', 'saltdfjalsdf') }}" update_password=always
#-------------------------------------------------------
# Configure systemd services
#-------------------------------------------------------
- name: copy systemd files
template:
src: "{{ item }}"
dest: /etc/systemd/system/
owner: root
mode: 0664
with_fileglob:
- files/systemd/*.service
- name: Create directory for xstartup file
file:
path: /home/ubuntu/.vnc/
state: directory
owner: ubuntu
mode: 0755
- name: copy xstartup file
copy:
src: files/xstartup
dest: /home/ubuntu/.vnc/
owner: ubuntu
mode: 0755
#-------------------------------------------------------
# Configure nginx
#-------------------------------------------------------
- name: copy nginx www files
copy:
src: "{{ item }}"
dest: /var/www/html
owner: www-data
mode: 0664
with_fileglob:
- files/www/*
- name: copy nginx config file
copy:
src: files/nginx/nginx.conf
dest: /etc/nginx/
owner: root
mode: 0664
- name: XXX
replace:
path: "/etc/default/grub"
regexp: ".*GRUB_CMDLINE_LINUX_DEFAULT.*"
replace: '#GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"'
- name: XXX
replace:
path: "/etc/default/grub"
regexp: ".*GRUB_CMDLINE_LINUX=.*"
replace: 'GRUB_CMDLINE_LINUX="text" '
- name: CCC
shell: sudo update-grub
- name: CCC
shell: systemctl set-default multi-user.target
- hosts: all
tasks:
#-------------------------------------------------------
# Set VNC password as user ubuntu
#-------------------------------------------------------
- name: Set VNC password
shell: "x11vnc -storepasswd {{ lookup('env','NODE_PASSWORD') | default('bla', true) }} ~/.vnc/passwd"
- hosts: all
become: yes
become_user: root
tasks:
#-------------------------------------------------------
# Enable and start services
#-------------------------------------------------------
- name: Enable services
systemd:
enabled: yes
name: "{{ item }}"
daemon_reload: yes
loop:
- xvfb
- x11vnc
- novnc
- xrdp
- wssh
- nginx
- name: (Re-) start services
systemd:
state: restarted
name: "{{ item }}"
daemon_reload: yes
loop:
- sshd
- nginx
- xvfb
- x11vnc
- novnc
- xrdp
- wssh