Skip to content

Feature/integrate auto detect in arrays #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ None

## Example Playbook

### Manual configuration
### Manual device configuration
```yaml
- hosts: all
become: true
Expand All @@ -30,6 +30,7 @@ None
filesystem: lvm
level: 5
state: present
auto_detect: false # default, can be omitted
roles:
- role: ansible-mdadm
```
Expand All @@ -39,18 +40,44 @@ None
- hosts: all
become: true
vars:
mdadm_auto_detect_arrays: true
mdadm_auto_detect_config:
mdadm_arrays:
- name: md200
devices: [] # empty list for auto-detection
filesystem: lvm
level: 5
state: present
auto_detect: true
min_disks: 3 # minimum number of disks required
roles:
- role: ansible-mdadm
```

### Mixed configuration
```yaml
- hosts: all
become: true
vars:
mdadm_arrays:
- name: md0
devices:
- /dev/sdb
- /dev/sdc
filesystem: ext4
level: 1
state: present
auto_detect: false
- name: md200
devices: []
filesystem: lvm
level: 5
state: present
auto_detect: true
min_disks: 3
roles:
- role: ansible-mdadm
```

Note: Auto-detection will automatically use all unused disks (not mounted, not in RAID, not in LVM, not formatted) if `mdadm_arrays` is empty and `mdadm_auto_detect_arrays` is true.
**Note**: Auto-detection will automatically use all unused disks (not mounted, not in RAID, not in LVM, not formatted) when `auto_detect: true` and `devices: []`. Setting both `auto_detect: true` and defining `devices` will result in an error.

## License

Expand Down
26 changes: 15 additions & 11 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
# state: 'present'
# # Set mount options (optional)
# opts: 'noatime'
# # Auto-detect unused disks (optional, default false)
# # If true and devices is empty, automatically detect unused disks
# # If true and devices is defined, will trigger an error
# auto_detect: false
#
# Example RAID5:
# mdadm_arrays:
Expand All @@ -34,15 +38,15 @@
# level: '5'
# mountpoint: '/mnt/md0'
# state: 'present'
# auto_detect: false
#
# Example auto-detect RAID5:
# mdadm_arrays:
# - name: 'md200'
# devices: []
# filesystem: 'lvm'
# level: '5'
# state: 'present'
# auto_detect: true
# min_disks: 3 # Minimum number of disks required for auto-detection
mdadm_arrays: []

# Auto-detection settings
mdadm_auto_detect_arrays: false
mdadm_auto_detect_config:
# Default configuration for auto-detected arrays
- name: md200
filesystem: lvm
level: 5
state: present
# Minimum number of disks required for auto-detection
min_disks: 3
46 changes: 39 additions & 7 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,49 @@
mdadm_force_wipe is defined and
mdadm_force_wipe

- name: main | Validate auto_detect configuration
fail:
msg: "Error: auto_detect is set to true but devices are also defined for array {{ item.name }}. Use either devices OR auto_detect, not both."
with_items: "{{ mdadm_arrays }}"
when: >
item.auto_detect | default(false) and
item.devices is defined and
item.devices | length > 0

- include_tasks: detect_unused_disks.yml
when: mdadm_arrays | selectattr('auto_detect', 'defined') | selectattr('auto_detect', 'equalto', true) | list | length > 0

- name: main | Validate sufficient disks for auto-detection
fail:
msg: "Error: Not enough unused disks found for auto-detection. Found {{ unused_disks | length }} disks, but {{ item.name }} requires at least {{ item.min_disks | default(3) }} disks."
with_items: "{{ mdadm_arrays }}"
when: >
mdadm_auto_detect_arrays and
(mdadm_arrays | length == 0)
item.auto_detect | default(false) and
(item.devices is not defined or item.devices | length == 0) and
unused_disks is defined and
unused_disks | length < item.min_disks | default(3)

- name: main | Set auto-detected arrays
- name: main | Set auto-detected devices for arrays with auto_detect
set_fact:
mdadm_arrays: "{{ mdadm_auto_detect_config | map('combine', {'devices': unused_disks}) | list }}"
mdadm_arrays: "{{ mdadm_arrays_updated }}"
vars:
mdadm_arrays_updated: >-
{%- set result = [] -%}
{%- for array in mdadm_arrays -%}
{%- if array.auto_detect | default(false) and (array.devices is not defined or array.devices | length == 0) -%}
{%- if unused_disks | length >= array.min_disks | default(3) -%}
{%- set updated_array = array | combine({'devices': unused_disks}) -%}
{%- set _ = result.append(updated_array) -%}
{%- else -%}
{%- set _ = result.append(array) -%}
{%- endif -%}
{%- else -%}
{%- set _ = result.append(array) -%}
{%- endif -%}
{%- endfor -%}
{{ result }}
when: >
mdadm_auto_detect_arrays and
(mdadm_arrays | length == 0) and
(unused_disks | length >= mdadm_auto_detect_config[0].min_disks)
unused_disks is defined and
mdadm_arrays | selectattr('auto_detect', 'defined') | selectattr('auto_detect', 'equalto', true) | list | length > 0

- include_tasks: arrays.yml