aboutsummaryrefslogtreecommitdiffstats
path: root/roles/host/tasks/main.yml
blob: 01cffb7142927c47c7ce9eb6ab68897c1c78b5fa (plain)
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
- name: Disable resolvconf
  copy:
    content: "resolvconf=NO\n"
    dest: /etc/resolvconf.conf
    owner: root
    group: wheel
    mode: "0644"

- name: Set up resolv.conf
  template:
    src: etc_resolv.conf.j2
    dest: /etc/resolv.conf
    owner: root
    group: wheel
    mode: "0644"

- name: Install packages
  package:
    name: "{{ pkg_name }}"
    state: present
  loop:
    - rsync
    - dma
    - jq
    - curl
    - bash
    - git
    - python311
    - py311-pip
    - py311-setuptools
    - py311-build
    - py311-installer
    - py311-wheel
    - fastfetch
    - vim
  loop_control:
    loop_var: pkg_name

- name: Set up sshd
  template:
    src: etc_ssh_sshd_config.j2
    dest: /etc/ssh/sshd_config
    owner: root
    group: wheel
    mode: "0644"
  notify: Restart sshd

- name: Start sshd
  service:
    name: sshd
    state: started

- name: Start syslogd
  service:
    name: syslogd
    state: started

- name: Start auditd
  service:
    name: auditd
    state: started

- name: Set up periodic.conf
  template:
    src: etc_periodic.conf.j2
    dest: /etc/periodic.conf
    owner: root
    group: wheel
    mode: "0644"

- name: Install .bashrc
  copy:
    src: root_bashrc
    dest: /root/.bashrc
    owner: root
    group: wheel
    mode: "0644"

# Jail infrastructure
- name: Fetch ports tree
  shell: |
    if [ ! -d /usr/ports/.git ]; then
      git clone --depth 1 https://github.com/jantuomi/freebsd-ports.git /usr/ports
    else
      git -C /usr/ports pull --ff-only
    fi
  register: _ports_fetch
  changed_when: "'Already up to date' not in _ports_fetch.stdout"
- name: Discover jail directories
  find:
    paths: "{{ playbook_dir }}/roles/jails"
    patterns: "main.yml"
    recurse: true
  delegate_to: localhost
  register: _jail_specs

- name: Load jail definitions
  set_fact:
    jail_defs: "{{ jail_defs | default([]) + [_content | combine({'num': _num, 'name': _name})] }}"
  vars:
    _content: "{{ lookup('file', item.path) | from_yaml }}"
    _num: "{{ item.path | regex_replace('.*/jails/([^/]+)/.*', '\\1') | split('_') | first | int }}"
    _name: "{{ item.path | regex_replace('.*/jails/([^/]+)/.*', '\\1') | regex_replace('^[0-9]+_', '') }}"
  loop: "{{ _jail_specs.files | sort(attribute='path') }}"
  loop_control:
    label: "{{ item.path | regex_replace('.*/jails/([^/]+)/.*', '\\1') }}"
  when: "'/defaults/' in item.path"

- name: Collect unique userlands
  set_fact:
    jail_userlands: "{{ jail_defs | map(attribute='userland') | unique | list }}"

- name: Create base ZFS datasets
  community.general.zfs:
    name: "{{ item.name }}"
    state: present
    extra_zfs_properties:
      mountpoint: "{{ item.mountpoint | default(omit) }}"
  loop:
    - { name: "zroot/jails", mountpoint: "/usr/local/jails" }
    - { name: "zroot/jails/media" }
    - { name: "zroot/jails/templates" }
    - { name: "zroot/jails/containers" }
    - { name: "zroot/jails/volumes", mountpoint: "none" }
    - {
        name: "zroot/jails/volumes/goaccess_www",
        mountpoint: "/usr/local/jails/volumes/goaccess_www",
      }
    - {
        name: "zroot/jails/volumes/postgres_data",
        mountpoint: "/usr/local/jails/containers/postgres/var/db/postgres",
      }
    - {
        name: "zroot/jails/volumes/irc_thelounge",
        mountpoint: "/usr/local/jails/containers/irc_thelounge/var/db/thelounge",
      }
    - {
        name: "zroot/jails/volumes/taulubot",
        mountpoint: "/usr/local/jails/containers/taulubot/usr/local/taulubot/photos",
      }
    - {
        name: "zroot/jails/volumes/komga_data",
        mountpoint: "/usr/local/jails/containers/komga/root/.komga",
      }
    - { name: "zroot/storage", mountpoint: "/usr/local/jails/volumes/storage" }
  loop_control:
    label: "{{ item.name }}"

- name: Install custom ports
  shell: cd /usr/ports/{{ item }} && make install
  loop:
    - sysutils/pylogsentinel
  register: _port_install
  failed_when: "_port_install.rc != 0 and 'is already installed' not in _port_install.stdout"
  changed_when: "'is already installed' not in _port_install.stdout"

- name: Deploy pylogsentinel.conf
  template:
    src: usr_local_etc_pylogsentinel.conf.j2
    dest: /usr/local/etc/pylogsentinel.conf
    owner: root
    group: wheel
    mode: "0644"

- name: Deploy pylogsentinel-batch-email.sh
  template:
    src: usr_local_bin_pylogsentinel-batch-email.sh.j2
    dest: /usr/local/bin/pylogsentinel-batch-email.sh
    owner: root
    group: wheel
    mode: "0755"

- name: Create storage group
  group:
    name: storage
    gid: 1001

- name: Create storage user
  user:
    name: storage
    uid: 1001
    group: storage
    home: /nonexistent
    shell: /usr/sbin/nologin
    create_home: false

- name: Set storage volume permissions
  file:
    path: /usr/local/jails/volumes/storage
    state: directory
    owner: storage
    group: storage
    mode: "0777"

- name: Create storage directories
  file:
    path: "/usr/local/jails/volumes/storage/{{ item.name }}"
    state: directory
    owner: "1001"
    group: "1001"
    mode: "{{ item.mode }}"
  loop:
    - { name: media, mode: "0777" }
    - { name: docs, mode: "0775" }
    - { name: downloads, mode: "0777" }
    - { name: projects-ableton, mode: "0755" }
    - { name: vault, mode: "0755" }
    - { name: jan-systems-2025-content, mode: "0755" }

- name: Set up userland templates
  include_tasks: userland.yml
  loop: "{{ jail_userlands }}"
  loop_control:
    loop_var: userland

- name: Deploy /etc/jail.conf
  template:
    src: jail.conf.j2
    dest: /etc/jail.conf
    owner: root
    group: wheel
    mode: "0644"

- name: Deploy devfs.rules
  template:
    src: devfs.rules.j2
    dest: /etc/devfs.rules
    owner: root
    group: wheel
    mode: "0644"

- name: Set up crontab
  template:
    src: etc_crontab.j2
    dest: /etc/crontab
    owner: root
    group: wheel
    mode: "0644"
  notify: Restart cron