prole/infrastructure/roles/iscsi/tasks/mount.yml

98 lines
3.0 KiB
YAML

---
- name: Compute legacy mountpoints to clean up (/prole/d00x -> /synology/d00x)
ansible.builtin.set_fact:
_iscsi_legacy_absent_mounts: >-
{{
(iscsi_targets | default([])
| map(attribute='mounts') | list | flatten
| selectattr('path', 'defined')
| map(attribute='path') | list
| select('match', '^/synology/d[0-9]{3}$')
| map('regex_replace', '^/synology/', '/prole/')
| list)
}}
changed_when: false
tags:
- iscsi_cleanup
- name: Unmount legacy iSCSI mounts (old /prole/d00x layout)
ansible.builtin.mount:
path: "{{ item }}"
state: unmounted
loop: "{{ _iscsi_legacy_absent_mounts | default([]) }}"
loop_control:
label: "{{ item }}"
tags:
- iscsi_cleanup
- name: Remove legacy iSCSI fstab entries (old /prole/d00x layout)
ansible.builtin.lineinfile:
path: /etc/fstab
state: absent
regexp: "^\\s*\\S+\\s+{{ item | regex_escape }}\\s+"
loop: "{{ _iscsi_legacy_absent_mounts | default([]) }}"
loop_control:
label: "{{ item }}"
tags:
- iscsi_cleanup
- name: Unmount stale iSCSI mounts
ansible.builtin.mount:
path: "{{ item.path | default(item) }}"
state: unmounted
loop: "{{ iscsi_absent_mounts | default([]) }}"
loop_control:
label: "{{ item.path | default(item) }}"
tags:
- iscsi_cleanup
- name: Guardrail - iSCSI must not manage /var/lib/rancher
ansible.builtin.assert:
that:
- >-
(iscsi_targets | default([])
| map(attribute='mounts') | list | flatten
| selectattr('path', 'equalto', '/var/lib/rancher')
| list | length) == 0
fail_msg: >-
Inventory attempts to mount `/var/lib/rancher` via iSCSI. K3s state storage is
intended to be local host storage (e.g. USB3 SSD on myrddin). Remove
`/var/lib/rancher` from `iscsi_targets[*].mounts` (or set
`iscsi_allow_rancher_mount: true` if you are intentionally overriding this).
when: not (iscsi_allow_rancher_mount | default(false) | bool)
- name: Remove stale iSCSI fstab entries
ansible.builtin.lineinfile:
path: /etc/fstab
state: absent
regexp: "^\\s*\\S+\\s+{{ (item.path | default(item)) | regex_escape }}\\s+"
loop: "{{ iscsi_absent_mounts | default([]) }}"
loop_control:
label: "{{ item.path | default(item) }}"
tags:
- iscsi_cleanup
- name: Mount filesystems for iSCSI targets
ansible.builtin.include_tasks: iscsi_target_mounts.yml
loop: "{{ iscsi_targets | default([]) }}"
loop_control:
loop_var: t
label: "{{ t.iqn | default('<unknown-iqn>') }}"
tags:
- iscsi
- iscsi_mount
- iscsi_storage
- name: Guardrail - ensure required mountpoints are mounted
ansible.builtin.command: "findmnt -n {{ item.path }}"
register: _findmnt
changed_when: false
failed_when: _findmnt.rc != 0
loop: "{{ iscsi_targets | default([]) | map(attribute='mounts') | list | flatten }}"
loop_control:
label: "{{ item.path }}"
tags:
- iscsi
- iscsi_mount
- iscsi_storage