-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaybook.yaml
427 lines (366 loc) · 13 KB
/
playbook.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
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
---
- hosts : pis
gather_facts : yes
become : no
vars:
user : pi
vim_dir : "{{ ansible_env.HOME }}/.vim"
vimrc : "{{ ansible_env.HOME }}/.vimrc"
tasks:
##############################
#
# UMASK
#
##############################
- name: (( UMASK )) Temporarily set the umask value to the more convenient 0022...
ansible.builtin.shell: umask 0022
##############################
#
# User config files
#
##############################
- name: (( USER CONFIG )) Ensure the existence of user files `~/{.bashrc,.bashrc.custom,.bash_profile,.bash_profile.custom,.bash_logout,.vimrc}`...
ansible.builtin.copy:
src : "{{ item }}"
dest : "{{ ansible_env.HOME }}"
owner : "{{ user }}"
group : "{{ user }}"
mode : '0600'
loop:
- .bashrc
- .bashrc.custom
- .bash_logout
- .bash_profile
- .bash_profile.custom
- .vimrc
##############################
#
# Package Management
#
##############################
- name: (( APT )) Update the cache and install packages aptitude, bat, firewalld, fzf, lsd, nftables, nginx, ripgrep, sqlite3, unbound, vim...
become : yes
become_method : sudo
ansible.builtin.apt:
update_cache : yes
state : latest
pkg:
- aptitude
- bat
- firewalld
- fzf
- lsd
- nftables
- nginx
- ripgrep
- sqlite3
- unbound
- vim
- name: (( APT )) Full upgrade...
become : yes
become_method : sudo
ansible.builtin.apt:
upgrade : full
- name: (( APT )) Remove any unused packages and dependencies...
become : yes
become_method : sudo
ansible.builtin.apt:
autoclean : yes
autoremove : yes
- name: (( APT )) Stop and disable the firewalld service so that the firewall doesn't interfere with SSH port 2222...
become : yes
become_method : sudo
ansible.builtin.systemd_service:
name : firewalld
state : stopped
enabled : false
- name: (( APT )) Stop and disable the nginx service...
become : yes
become_method : sudo
ansible.builtin.systemd_service:
name : nginx
state : stopped
enabled : false
##############################
#
# vim Configuration
#
##############################
- name: (( VIM )) Ensure the existence of directories `~/.vim/{after/ftplugin,autoload,bundle,colors}`...
ansible.builtin.file:
state : directory
path : "{{ item }}"
owner : "{{ user }}"
group : "{{ user }}"
mode : '0700'
loop:
- "{{ vim_dir }}/after/ftplugin"
- "{{ vim_dir }}/autoload"
- "{{ vim_dir }}/bundle"
- "{{ vim_dir }}/colors"
- "{{ vim_dir }}/plugged"
- name: (( VIM )) Ensure plugin manager `vim-plug` is downloaded to directory `~/.vim/autoload`...
ansible.builtin.get_url:
dest : "{{ vim_dir }}/autoload/plug.vim"
url : https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
owner : "{{ user }}"
group : "{{ user }}"
mode : '0600'
- name: (( VIM )) Deploy vim plugins vim-airline, nerdtree, ale...
ansible.builtin.git:
dest : "{{ vim_dir }}/plugged/{{ item.name }}"
repo : "{{ item.url }}"
clone : yes
update : yes
recursive : no
loop:
- name : vim-airline
url : https://github.com/vim-airline/vim-airline
- name : nerdtree
url : https://github.com/preservim/nerdtree
- name : ale
url : https://github.com/dense-analysis/ale
##############################
#
# SSH Hardening
#
# YOU ONLY NEED TO RUN THE FOLLOWING SET OF SSH HARDENING TASKS ONCE
#
##############################
- name: (( SSH )) Update config file `/etc/ssh/sshd_config`...
become : yes
become_method : sudo
ansible.builtin.lineinfile:
path : /etc/ssh/sshd_config
state : present
regexp : "{{ item.regexp }}"
line : "{{ item.line }}"
loop:
- regexp : '^#?AuthorizedKeysFile'
line : 'AuthorizedKeysFile .ssh/authorized_keys'
- regexp : '^#?LogLevel'
line : 'LogLevel VERBOSE'
- regexp : '^#?PasswordAuthentication'
line : 'PasswordAuthentication no'
- regexp : '^#?PermitRootLogin'
line : 'PermitRootLogin no'
- regexp : '^#?Port 22'
line : 'Port 22'
- regexp : '^#?PubkeyAuthentication'
line : 'PubkeyAuthentication yes'
#- regexp : '^#?StrictModes'
# line : 'StrictModes yes'
- name: (( SSH )) Find ssh host key files older than 2 years...
register: old_ssh_host_key
ansible.builtin.find:
paths : /etc/ssh
patterns : ssh_host_*
file_type : file
age : 2y
- name: (( SSH )) Delete old host key files...
become : yes
become_method : sudo
ansible.builtin.file:
path : "{{ item }}"
state : absent
loop:
- /etc/ssh/ssh_host_rsa_key
- /etc/ssh/ssh_host_rsa_key.pub
- /etc/ssh/ssh_host_ecdsa_key
- /etc/ssh/ssh_host_ecdsa_key.pub
- /etc/ssh/ssh_host_ed25519_key
- /etc/ssh/ssh_host_ed25519_key.pub
- name: (( SSH )) Generate new host key files...
become : yes
become_method : sudo
ansible.builtin.command : ssh-keygen -A
- name: (( SSH )) Delete generated DSA host key files...
become : yes
become_method : sudo
ansible.builtin.file:
path : "{{ item }}"
state : absent
loop:
- /etc/ssh/ssh_host_dsa_key
- /etc/ssh/ssh_host_dsa_key.pub
- name: (( SSH )) Update host key file permissions...
become : yes
become_method : sudo
ansible.builtin.file:
path : "{{ item.host_key_file }}"
mode : "{{ item.permission }}"
loop:
- host_key_file : /etc/ssh/ssh_host_rsa_key
permission : '400'
- host_key_file : /etc/ssh/ssh_host_rsa_key.pub
permission : '644'
- host_key_file : /etc/ssh/ssh_host_ecdsa_key
permission : '400'
- host_key_file : /etc/ssh/ssh_host_ecdsa_key.pub
permission : '644'
- host_key_file : /etc/ssh/ssh_host_ed25519_key
permission : '400'
- host_key_file : /etc/ssh/ssh_host_ed25519_key.pub
permission : '644'
- name: (( SSH )) Restart sshd...
become : yes
become_method : sudo
ansible.builtin.systemd_service:
name : sshd
state : restarted
enabled : true
daemon_reload : true
##############################
#
# Unbound - Recursive DNS
#
# hosts: pi1 pi2
#
##############################
- name: (( UNBOUND )) Create file `/etc/unbound/unbound.conf.d/pi-hole.conf` with the appropriate config values...
when : inventory_hostname in groups['piholes']
become : yes
become_method : sudo
ansible.builtin.copy:
src : unbound/pi-hole.conf
dest : /etc/unbound/unbound.conf.d/pi-hole.conf
owner : root
group : root
mode : '0644'
- name: (( UNBOUND )) Create directory `/etc/dnsmasq.d`...
when : inventory_hostname in groups['piholes']
become : yes
become_method : sudo
ansible.builtin.file:
state : directory
path : /etc/dnsmasq.d
owner : root
group : root
mode : '0755'
- name: (( UNBOUND )) Create file `/etc/dnsmasq.d/99-edns.conf` with the appropriate config values...
when : inventory_hostname in groups['piholes']
become : yes
become_method : sudo
ansible.builtin.copy:
src : unbound/99-edns.conf
dest : /etc/dnsmasq.d/99-edns.conf
owner : root
group : root
mode : '0644'
- name: (( UNBOUND )) Restart unbound...
when : inventory_hostname in groups['piholes']
become : yes
become_method : sudo
ansible.builtin.systemd_service:
name : unbound
state : restarted
enabled : true
daemon_reload : true
##############################
#
# Pi-Hole - DNS Sinkhole
#
# Use the following command to generate the appropriate value for WEBPASSWORD in file `setupVars.conf`
# echo -n 'PASSWORD' | sha256sum | awk '{printf "%s",$1}' | sha256sum | awk '{printf "%s",$1}'
#
##############################
- name: (( PI-HOLE )) Create group `pihole`...
when : inventory_hostname in groups['piholes']
become : yes
become_method : sudo
ansible.builtin.group:
name : pihole
state : present
- name: (( PI-HOLE )) Create service user `pihole`.
when : inventory_hostname in groups['piholes']
become : yes
become_method : sudo
ansible.builtin.user:
name : pihole
group : pihole
create_home : false
shell : /usr/sbin/nologin
state : present
- name: (( PI-HOLE )) Create directory `/etc/pihole`.
when : inventory_hostname in groups['piholes']
become : yes
become_method : sudo
ansible.builtin.file:
state : directory
path : /etc/pihole
owner : pihole
group : pihole
mode : '0775'
- name: (( PI-HOLE )) Check to see whether file `/etc/pihole/setupVars.conf` already exists.
when : inventory_hostname in groups['piholes']
register : setupvars
ansible.builtin.stat:
path: /etc/pihole/setupVars.conf
- name: (( PI-HOLE )) Create file `/etc/pihole/setupVars.conf` with the appropriate configuration values.
when : inventory_hostname in groups['piholes'] and setupvars.stat.exists == false
become : yes
become_method : sudo
ansible.builtin.copy:
src : pihole/setupVars.conf
dest : /etc/pihole/setupVars.conf
owner : root
group : root
mode : '0644'
- name: (( PI-HOLE )) Check whether pihole is already installed.
when : inventory_hostname in groups['piholes']
register : pihole_installed
ansible.builtin.stat:
path: /usr/local/bin/pihole
- name: (( PI-HOLE )) Install Pi-Hole in unattended mode.
when : inventory_hostname in groups['piholes'] and pihole_installed.stat.exists == false
become : yes
become_method : sudo
#ansible.builtin.shell: 'curl -L https://install.pi-hole.net | PIHOLE_SKIP_OS_CHECK=true bash /dev/stdin --unattended'
ansible.builtin.shell: 'curl -L https://install.pi-hole.net | bash /dev/stdin --unattended'
##############################
#
# UMASK
#
##############################
- name: (( UMASK )) Revert the umask value back to the more secure 0077...
ansible.builtin.shell: umask 0077
##############################
#
# Pi-Rack Pro OLED Display
#
##############################
- name: (( PI-RACK )) Clone the Pi-Rack Pro OLED Display repo to "/home/{{ user }}/SKU_RM0004..."
ansible.builtin.git:
dest : "{{ ansible_env.HOME }}/SKU_RM0004"
repo : https://github.com/UCTRONICS/SKU_RM0004
clone : yes
update : yes
recursive : no
- name: (( PI-RACK )) Compile via `make`...
# the repo doesn't like command make -C "{{ ansible_env.HOME }}/SKU_RM0004"
ansible.builtin.command:
cmd : make
chdir : "{{ ansible_env.HOME }}/SKU_RM0004"
# This task must still be tested.
# https://github.com/ansible/ansible/issues/76938
#
- name: (( PI-RACK )) Add an automatic startup script via `./deployment_service.sh`...
async : 1
poll : 0
ignore_errors : true
ansible.builtin.expect:
chdir : "{{ ansible_env.HOME }}/SKU_RM0004"
command : ./deployment_service.sh
responses :
"(?m)^The script needs to be restarted to take effect" : 'y'
"(?m)^Service already exists" : 'n'
- name: (( PI-RACK )) Wait for the host to come back up...
ansible.builtin.wait_for_connection:
delay : 10
timeout : 120
# - name: (( PI-RACK )) Reboot the system for changes to take effect...
# become : yes
# become_method : sudo
# ansible.builtin.reboot:
# msg : "Rebooting machine in 5 seconds..."
# reboot_timeout : 60