refactor(global_defaults): split system.yml into composable normalization stages
This commit is contained in:
100
roles/global_defaults/tasks/_normalize_disks.yml
Normal file
100
roles/global_defaults/tasks/_normalize_disks.yml
Normal file
@@ -0,0 +1,100 @@
|
||||
---
|
||||
- name: Normalize system disks input
|
||||
vars:
|
||||
system_disks: "{{ system_cfg.disks | default([]) }}"
|
||||
system_disk_letter_map: "abcdefghijklmnopqrstuvwxyz"
|
||||
system_disk_device_prefix: >-
|
||||
{{
|
||||
{'libvirt': '/dev/vd', 'xen': '/dev/xvd', 'proxmox': '/dev/sd', 'vmware': '/dev/sd'}.get(hypervisor_type, '')
|
||||
if system_cfg.type == 'virtual'
|
||||
else ''
|
||||
}}
|
||||
block:
|
||||
- name: Validate system disks structure
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- system_disks is sequence
|
||||
- (system_disks | length) <= 26
|
||||
fail_msg: "system.disks must be a list with at most 26 entries."
|
||||
quiet: true
|
||||
|
||||
- name: Validate system disk entries
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- item is mapping
|
||||
- item.mount is not defined or item.mount is mapping
|
||||
fail_msg: "Each disk entry must be a dictionary, and disk.mount (if set) must be a dictionary."
|
||||
quiet: true
|
||||
loop: "{{ system_disks }}"
|
||||
loop_control:
|
||||
label: "{{ item | to_json }}"
|
||||
|
||||
- name: Initialize normalized disk list
|
||||
ansible.builtin.set_fact:
|
||||
system_disks_cfg: []
|
||||
|
||||
- name: Build normalized system disk configuration
|
||||
vars:
|
||||
disk_idx: "{{ ansible_loop.index0 }}"
|
||||
disk_letter: "{{ system_disk_letter_map[disk_idx] }}"
|
||||
disk_cfg_base: "{{ system_disk_defaults | combine(item, recursive=True) }}"
|
||||
disk_mount: "{{ system_disk_defaults.mount | combine((disk_cfg_base.mount | default({})), recursive=True) }}"
|
||||
disk_mount_path: "{{ (disk_mount.path | default('') | string) | trim }}"
|
||||
disk_mount_fstype: >-
|
||||
{{
|
||||
disk_mount.fstype
|
||||
if (disk_mount.fstype | default('') | string | length) > 0
|
||||
else ('ext4' if disk_mount_path | length > 0 else '')
|
||||
}}
|
||||
disk_device: >-
|
||||
{{
|
||||
disk_cfg_base.device
|
||||
if (disk_cfg_base.device | string | length) > 0
|
||||
else (
|
||||
(system_disk_device_prefix ~ disk_letter)
|
||||
if system_cfg.type == 'virtual'
|
||||
else ''
|
||||
)
|
||||
}}
|
||||
disk_partition: >-
|
||||
{{
|
||||
disk_device ~ ('p1' if (disk_device | regex_search('\\d$')) else '1')
|
||||
if disk_device | length > 0
|
||||
else ''
|
||||
}}
|
||||
ansible.builtin.set_fact:
|
||||
system_disks_cfg: >-
|
||||
{{
|
||||
system_disks_cfg + [
|
||||
disk_cfg_base
|
||||
| combine(
|
||||
{
|
||||
'device': disk_device,
|
||||
'mount': {
|
||||
'path': disk_mount_path,
|
||||
'fstype': disk_mount_fstype,
|
||||
'label': disk_mount.label | default('') | string,
|
||||
'opts': disk_mount.opts | default('defaults') | string
|
||||
},
|
||||
'partition': disk_partition
|
||||
},
|
||||
recursive=True
|
||||
)
|
||||
]
|
||||
}}
|
||||
loop: "{{ system_disks }}"
|
||||
loop_control:
|
||||
loop_var: item
|
||||
extended: true
|
||||
label: "{{ item | to_json }}"
|
||||
|
||||
- name: Update system configuration with normalized disks
|
||||
ansible.builtin.set_fact:
|
||||
system_cfg: "{{ system_cfg | combine({'disks': system_disks_cfg}, recursive=True) }}"
|
||||
|
||||
- name: Set install_drive from primary disk
|
||||
when:
|
||||
- system_disks_cfg | length > 0
|
||||
- system_disks_cfg[0].device | string | length > 0
|
||||
ansible.builtin.set_fact:
|
||||
install_drive: "{{ system_disks_cfg[0].device }}"
|
||||
Reference in New Issue
Block a user