-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.yaml
More file actions
609 lines (533 loc) · 21.9 KB
/
setup.yaml
File metadata and controls
609 lines (533 loc) · 21.9 KB
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
---
- name: Setup Machine
hosts: localhost
connection: local
handlers:
- name: Update package information
become: true
ansible.builtin.apt:
update_cache: true
changed_when: false
- name: Restart SSH
become: true
ansible.builtin.service:
name: sshd
state: restarted
when: dotenv_environment != "development"
- name: Restart Docker
become: true
ansible.builtin.service:
name: docker
state: restarted
when: dotenv_environment != "development"
- name: Restart containerd
become: true
ansible.builtin.service:
name: containerd
state: restarted
when: dotenv_environment != "development"
- name: Restart SSHGuard
become: true
ansible.builtin.service:
name: sshguard
state: restarted
when: dotenv_environment != "development"
- name: Reload Vector
become: true
ansible.builtin.service:
name: vector
state: reloaded
when: dotenv_environment != "development"
- name: Reload Monit
become: true
ansible.builtin.service:
name: monit
state: reloaded
when: dotenv_environment != "development"
- name: Send test email as root
become: true
ansible.builtin.shell:
cmd: |
set -o pipefail
echo "Test email as root user from new/updated MSMTP at" `hostname` \
| mail -s "Test SMTP `hostname`" root
executable: /bin/bash
changed_when: false
when: dotenv_environment != "development"
- name: Send test email as cloud
ansible.builtin.shell:
cmd: |
set -o pipefail
echo "Test email as {{ ansible_facts['user_id'] | ansible.builtin.mandatory }} user from new/updated MSMTP at" `hostname` \
| mail -s "Test SMTP `hostname`" {{ ansible_facts['user_id'] | ansible.builtin.mandatory }}
executable: /bin/bash
changed_when: false
when: dotenv_environment != "development"
pre_tasks:
- name: Always set facts from environment variables
ansible.builtin.set_fact:
# The filter `ansible.builtin.default(undef(), true)` yields undefined if the value evaluates to false or an empty string.
dotenv_environment: "{{ lookup('ansible.builtin.env', 'ENVIRONMENT') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
host: "{{ lookup('ansible.builtin.env', 'HOST') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
production_subdomain: "{{ lookup('ansible.builtin.env', 'PRODUCTION_SUBDOMAIN') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
staging_subdomain: "{{ lookup('ansible.builtin.env', 'STAGING_SUBDOMAIN') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
telemetry_subdomain: "{{ lookup('ansible.builtin.env', 'TELEMETRY_SUBDOMAIN') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
monitor_http_port: "{{ lookup('ansible.builtin.env', 'MONITOR_HTTP_PORT') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
logs_http_port: "{{ lookup('ansible.builtin.env', 'LOGS_HTTP_PORT') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
metrics_http_port: "{{ lookup('ansible.builtin.env', 'METRICS_HTTP_PORT') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
telemetry_grpc_port: "{{ lookup('ansible.builtin.env', 'TELEMETRY_GPRC_PORT') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
telemetry_http_port: "{{ lookup('ansible.builtin.env', 'TELEMETRY_HTTP_PORT') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
email_address: "{{ lookup('ansible.builtin.env', 'EMAIL_ADDRESS') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
smtp_host: "{{ lookup('ansible.builtin.env', 'SMTP_HOST') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
smtp_port: "{{ lookup('ansible.builtin.env', 'SMTP_PORT') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
network_interface: "{{ lookup('ansible.builtin.env', 'NETWORK_INTERFACE') | ansible.builtin.default(undef(), true) | ansible.builtin.mandatory }}"
tags: always
- name: Always update package information
become: true
ansible.builtin.apt:
update_cache: true
changed_when: false
tags: always
- name: Always install access control list utilities (needed to escalate privileges, see https://github.com/ansible/ansible/issues/16052)
become: true
ansible.builtin.package:
name: acl
state: present
tags: always
post_tasks:
- name: Clean-up Advanced Package Tool (APT) cache and remove unused dependencies
become: true
block:
- name: Remove unused dependencies (autoremove)
ansible.builtin.apt:
autoremove: true
update_cache: true
- name: Clear obsolete package files (autoclean)
ansible.builtin.apt:
autoclean: true
tasks:
- name: Install curl
become: true
ansible.builtin.package:
name: curl
state: present
- name: Grant user `cloud` read-only access to journald
become: true
ansible.builtin.user:
name: "{{ ansible_facts['user_id'] | ansible.builtin.mandatory }}"
groups: systemd-journal
append: true
- name: Install Bash completion
become: true
ansible.builtin.package:
name: bash-completion
state: present
- name: Upgrades
block:
# Inspired by https://wiki.debian.org/UnattendedUpgrades
# and https://askubuntu.com/questions/1305955/ansible-dpkg-reconfigure-plow-unattended-upgrades-stopped-while-running/1323117#1323117
- name: Install `unattended-upgrades`
become: true
ansible.builtin.package:
name: unattended-upgrades
state: present
- name: Install `apt-listchanges`
become: true
ansible.builtin.package:
name: apt-listchanges
state: present
- name: Configure Debian to auto install security updates
become: true
ansible.builtin.debconf:
name: unattended-upgrades
question: unattended-upgrades/enable_auto_updates
vtype: boolean
value: "true"
- name: Activate unattended upgrades
become: true
ansible.builtin.command:
cmd: dpkg-reconfigure --force --frontend=noninteractive unattended-upgrades
creates: /etc/apt/apt.conf.d/20auto-upgrades
changed_when: false
- name: Install GNU Make
become: true
ansible.builtin.package:
name: make
state: present
- name: Hard disk management
block:
- name: Install Small Computer System Interface (SCSI) tools
# Provides `rescan-scsi-bus.sh` used by `make scan`.
become: true
ansible.builtin.package:
name: scsitools
state: present
# See https://opensource.com/article/18/11/partition-format-drive-linux
- name: Install parted to partition hard disks
become: true
ansible.builtin.package:
name: parted
state: present
- name: Install ext2/ext3/ext4 file system utilities to format hard disks
become: true
ansible.builtin.package:
name: e2fsprogs
state: present
- name: Install Apache 2 utils to manage passwords for HTTP Basic Authentication using htpasswd
become: true
ansible.builtin.package:
name: apache2-utils
state: present
- name: Install `moreutils` as it includes `chronic` that we use to cure Cron's chronic email problem
# For details on the problem see https://habilis.net/cronic/
become: true
ansible.builtin.package:
name: moreutils
state: present
- name: Install and configure `msmtp` to be used by Cron and other services to send emails
vars:
from_email_address: "{{ email_address }}"
block:
- name: Uninstall `mailutils` which includes GNU `mail`
# The problem with GNU `mail` combined with `msmtp` is that aliases
# in `/etc/aliases` are ignored. For example, when sending an email
# by running `echo 'body' | mail -s 'subject' root`, the recipient
# address is `root@hostname` instead of whatever email address is
# given in `/etc/aliases` for `root`.
become: true
ansible.builtin.package:
name: mailutils
state: absent
- name: Install `bsd-mailx` which provides the `mail` command
# An alternative would be `s-nail`
become: true
ansible.builtin.package:
name: bsd-mailx
state: present
- name: Install `msmtp` to send emails from mail user agents
# For details see https://wiki.debian.org/msmtp
# The manual is published under https://marlam.de/msmtp/msmtp.html
# We do not use `sSMTP` because it is unmaintained according to https://wiki.debian.org/sSMTP
become: true
ansible.builtin.package:
name: msmtp
state: present
- name: Install `msmtp-mta` to symbolically link `/usr/sbin/sendmail` to `msmtp` that other software can use to send mail
become: true
ansible.builtin.package:
name: msmtp-mta
state: present
- name: Copy `mail.rc` configuration file
become: true
ansible.builtin.template:
src: mail.rc.j2
dest: /etc/mail.rc
owner: root
group: root
mode: "0644"
notify:
- Send test email as root
- Send test email as cloud
- name: Copy system-wide `mstprc` configuration file
become: true
ansible.builtin.template:
src: msmtprc.j2
dest: /etc/msmtprc
owner: root
group: root
mode: "0644"
notify: Send test email as root
- name: Copy user-specific `mstprc` configuration file
ansible.builtin.template:
src: msmtprc.j2
dest: "{{ ansible_facts['env']['HOME'] | ansible.builtin.mandatory }}/.msmtprc"
mode: "0644"
notify: Send test email as cloud
- name: Copy `aliases` configuration file
become: true
ansible.builtin.template:
src: aliases.j2
dest: /etc/aliases
owner: root
group: root
mode: "0644"
notify:
- Send test email as root
- Send test email as cloud
- name: Flush handlers to send test emails
ansible.builtin.meta: flush_handlers
- name: Configure Cron
block:
- name: Add paths to environment variable `PATH` for root's crontab
become: true
community.general.cronvar:
name: PATH
value: /usr/local/bin/:/usr/bin:/bin
state: present
- name: Add paths to environment variable `PATH` for user's crontab
community.general.cronvar:
name: PATH
value: /usr/local/bin/:/usr/bin:/bin
state: present
- name: Backup database and prune backups
ansible.builtin.cron:
name: database-backup
minute: 0
hour: 3
job: "chronic make --directory=/app/machine --file=/app/machine/maintenance.mk backup prune-backups"
- name: Renew Transport Layer Security (TLS) certificates needed for the `S` in `HTTPS`
ansible.builtin.cron:
name: tls-renewal
minute: 5
hour: 3
job: "chronic make --directory=/app/machine --file=/app/machine/maintenance.mk renew-tls"
- name: Set periodic docker system prune
# See https://docs.docker.com/config/pruning/#prune-everything
ansible.builtin.cron:
name: docker-prune
minute: 10
hour: 3
job: "chronic make --directory=/app/machine --file=/app/machine/maintenance.mk prune-docker"
- name: Restart Docker backend service in production to reload rotated JWT certificates
ansible.builtin.cron:
name: restart-backend-service-in-production
minute: 15
hour: 3
job: "chronic make --directory=/app/machine --file=/app/machine/maintenance.mk restart SERVICE=backend ENV=production"
- name: Restart Docker backend service in staging to reload rotated JWT certificates
ansible.builtin.cron:
name: restart-backend-service-in-staging
minute: 15
hour: 3
job: "chronic make --directory=/app/machine --file=/app/machine/maintenance.mk restart SERVICE=backend ENV=staging"
- name: Rotate logs, vacuum `journald`, and clean-up backed up logs (as the root user)
become: true
ansible.builtin.cron:
name: journald-vacuuming
minute: 20
hour: 3
job: "chronic make --directory=/app/machine --file=/app/machine/logs.mk rotate vacuum clean-up"
- name: Harden SSH
become: true
block:
- name: Install nftables and SSHGuard
ansible.builtin.package:
name:
- nftables
- sshguard
state: present
- name: Configure SSHGuard whitelist
vars:
host: "{{ host }}"
ansible.builtin.template:
src: sshguard-whitelist.j2
dest: /etc/sshguard/whitelist
owner: root
group: root
mode: "0644"
notify:
- Restart SSHGuard
- name: Enable SSHGuard service to start on boot
ansible.builtin.service:
name: sshguard
enabled: true
state: started
- name: Ensure ~/.ssh directory exists with 0700 permissions
ansible.builtin.file:
path: "{{ ansible_facts['env']['HOME'] | ansible.builtin.mandatory }}/.ssh"
mode: "0700"
owner: "{{ ansible_facts['user_id'] | ansible.builtin.mandatory }}"
state: directory
- name: Ensure ~/.ssh/authorized_keys file exists with 0600 permissions
ansible.builtin.file:
path: "{{ ansible_facts['env']['HOME'] | ansible.builtin.mandatory }}/.ssh/authorized_keys"
mode: "0600"
owner: "{{ ansible_facts['user_id'] | ansible.builtin.mandatory }}"
state: touch
- name: Ensure at least one SSH key is authorized
block:
- name: Count authorized SSH keys
ansible.builtin.command:
cmd: "grep --count --invert-match '^#|^$' {{ ansible_facts['env']['HOME'] | ansible.builtin.mandatory }}/.ssh/authorized_keys"
register: authorized_key_count
# `rc` below means return code, aka, status code.
failed_when: authorized_key_count.rc == 2
changed_when: false
- name: Fail if there are no authorized SSH keys
ansible.builtin.fail:
msg: "The ~/.ssh/authorized_keys file does not contain valid keys."
# Check if grep found 0 lines or if the file was missing entirely
when: authorized_key_count.stdout == "0"
- name: Configure SSH
ansible.builtin.copy:
src: sshd_config.conf
dest: /etc/ssh/sshd_config
owner: root
group: root
mode: "0644"
notify:
- Restart SSH
- name: Flush handlers to restart SSHGuard and SSH
ansible.builtin.meta: flush_handlers
- name: GnuPG
become: true
block:
- name: Add GnuPG repository
ansible.builtin.deb822_repository:
name: gnupg2
types: deb
uris: "https://repos.gnupg.org/deb/gnupg/{{ ansible_facts['distribution_release'] | ansible.builtin.mandatory }}/"
suites: "{{ ansible_facts['distribution_release'] | ansible.builtin.mandatory }}"
components: main
architectures: amd64
signed_by: "https://repos.gnupg.org/deb/gnupg/{{ ansible_facts['distribution_release'] | ansible.builtin.mandatory }}/gnupg-signing-key.gpg"
state: present
notify: Update package information
- name: Flush handlers to update package information
ansible.builtin.meta: flush_handlers
- name: Install GnuPG
ansible.builtin.package:
name: gnupg2
state: present
- name: Docker
# Inspired by https://ops.tips/blog/docker-ansible-role/
become: true
block:
- name: Add Docker repository
ansible.builtin.deb822_repository:
name: docker
types: deb
uris: "https://download.docker.com/linux/debian"
suites: "{{ ansible_facts['distribution_release'] | ansible.builtin.mandatory }}"
components: stable
architectures: amd64
signed_by: "https://download.docker.com/linux/debian/gpg"
state: present
notify: Update package information
- name: Flush handlers to update package information
ansible.builtin.meta: flush_handlers
- name: Install Docker Engine, containerd, and Docker Compose
ansible.builtin.package:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose-plugin
state: present
- name: Prepare default daemon configuration
# For a list of options see
# https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file
ansible.builtin.copy:
src: ./docker-daemon.json
dest: /etc/docker/daemon.json
mode: "0644"
notify:
- Restart Docker
- name: Add user `cloud` to group `docker`
ansible.builtin.user:
name: "{{ ansible_facts['user_id'] | ansible.builtin.mandatory }}"
groups: docker
append: true
- name: Enable Docker systemd service to start on boot
ansible.builtin.service:
name: docker
enabled: true
state: started
- name: Configure containerd
# For a list of options see
# https://containerd.io/docs/main/man/containerd-config.toml.5/
ansible.builtin.copy:
src: ./containerd.toml
dest: /etc/containerd/config.toml
mode: "0644"
notify:
- Restart containerd
- name: Flush handlers to restart Docker and containerd
ansible.builtin.meta: flush_handlers
- name: Vector
# https://vector.dev/
become: true
block:
- name: Add Vector repository
ansible.builtin.deb822_repository:
name: vector
types: deb
uris: "https://apt.vector.dev"
suites: stable
components: vector-0
architectures: amd64
signed_by: "https://keys.datadoghq.com/DATADOG_APT_KEY_CURRENT.public"
state: present
notify: Update package information
- name: Flush handlers to update package information
ansible.builtin.meta: flush_handlers
- name: Install Vector
ansible.builtin.package:
name: vector
state: present
- name: Configure Vector
ansible.builtin.template:
src: vector.yaml.j2
dest: /etc/vector/vector.yaml
owner: root
group: root
mode: "0644"
notify:
- Reload Vector
- name: Add user `vector` to group `docker`
ansible.builtin.user:
name: vector
groups: docker
append: true
- name: Enable Vector systemd service to start on boot
ansible.builtin.systemd:
name: vector.service
enabled: true
state: started
- name: Flush handlers to reload Vector
ansible.builtin.meta: flush_handlers
- name: Monitor system with Monit
# https://mmonit.com/monit/
become: true
block:
- name: Install Monit
ansible.builtin.package:
name: monit
state: present
- name: Configure Monit
vars:
from_email_address: "{{ email_address }}"
production_host: "{{ production_subdomain }}.{{ host }}"
staging_host: "{{ staging_subdomain }}.{{ host }}"
telemetry_host: "{{ telemetry_subdomain }}.{{ host }}"
ansible.builtin.template:
src: monitrc.j2
dest: /etc/monit/monitrc
owner: root
group: root
mode: "0600"
notify:
- Reload Monit
- name: Make Monit a systemd service
ansible.builtin.copy:
src: monit.service
dest: /etc/systemd/system/monit.service
owner: root
group: root
mode: "0644"
- name: Enable Monit systemd service to start on boot
ansible.builtin.systemd:
name: monit.service
enabled: true
# reload daemon to make it notice the new monit.service file
daemon_reload: true
state: started
- name: Grant user `cloud` read-only access to Monit without a password
community.general.sudoers:
name: monit-nopasswd
user: "{{ ansible_facts['user_id'] | ansible.builtin.mandatory }}"
commands: /usr/bin/monit
nopassword: true
state: present
- name: Flush handlers to reload Monit
ansible.builtin.meta: flush_handlers