Skip to content

Commit 0c86044

Browse files
authored
Merge pull request #2 from kilnfi/feature/integrate-auto-detect-in-arrays
Feature/integrate auto detect in arrays
2 parents 8d85b3c + bb66d2f commit 0c86044

File tree

3 files changed

+85
-22
lines changed

3 files changed

+85
-22
lines changed

README.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ None
1717

1818
## Example Playbook
1919

20-
### Manual configuration
20+
### Manual device configuration
2121
```yaml
2222
- hosts: all
2323
become: true
@@ -30,6 +30,7 @@ None
3030
filesystem: lvm
3131
level: 5
3232
state: present
33+
auto_detect: false # default, can be omitted
3334
roles:
3435
- role: ansible-mdadm
3536
```
@@ -39,18 +40,44 @@ None
3940
- hosts: all
4041
become: true
4142
vars:
42-
mdadm_auto_detect_arrays: true
43-
mdadm_auto_detect_config:
43+
mdadm_arrays:
44+
- name: md200
45+
devices: [] # empty list for auto-detection
46+
filesystem: lvm
47+
level: 5
48+
state: present
49+
auto_detect: true
50+
min_disks: 3 # minimum number of disks required
51+
roles:
52+
- role: ansible-mdadm
53+
```
54+
55+
### Mixed configuration
56+
```yaml
57+
- hosts: all
58+
become: true
59+
vars:
60+
mdadm_arrays:
61+
- name: md0
62+
devices:
63+
- /dev/sdb
64+
- /dev/sdc
65+
filesystem: ext4
66+
level: 1
67+
state: present
68+
auto_detect: false
4469
- name: md200
70+
devices: []
4571
filesystem: lvm
4672
level: 5
4773
state: present
74+
auto_detect: true
4875
min_disks: 3
4976
roles:
5077
- role: ansible-mdadm
5178
```
5279
53-
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.
80+
**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.
5481

5582
## License
5683

defaults/main.yml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
# state: 'present'
2323
# # Set mount options (optional)
2424
# opts: 'noatime'
25+
# # Auto-detect unused disks (optional, default false)
26+
# # If true and devices is empty, automatically detect unused disks
27+
# # If true and devices is defined, will trigger an error
28+
# auto_detect: false
2529
#
2630
# Example RAID5:
2731
# mdadm_arrays:
@@ -34,15 +38,15 @@
3438
# level: '5'
3539
# mountpoint: '/mnt/md0'
3640
# state: 'present'
41+
# auto_detect: false
42+
#
43+
# Example auto-detect RAID5:
44+
# mdadm_arrays:
45+
# - name: 'md200'
46+
# devices: []
47+
# filesystem: 'lvm'
48+
# level: '5'
49+
# state: 'present'
50+
# auto_detect: true
51+
# min_disks: 3 # Minimum number of disks required for auto-detection
3752
mdadm_arrays: []
38-
39-
# Auto-detection settings
40-
mdadm_auto_detect_arrays: false
41-
mdadm_auto_detect_config:
42-
# Default configuration for auto-detected arrays
43-
- name: md200
44-
filesystem: lvm
45-
level: 5
46-
state: present
47-
# Minimum number of disks required for auto-detection
48-
min_disks: 3

tasks/main.yml

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,49 @@
1313
mdadm_force_wipe is defined and
1414
mdadm_force_wipe
1515
16+
- name: main | Validate auto_detect configuration
17+
fail:
18+
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."
19+
with_items: "{{ mdadm_arrays }}"
20+
when: >
21+
item.auto_detect | default(false) and
22+
item.devices is defined and
23+
item.devices | length > 0
24+
1625
- include_tasks: detect_unused_disks.yml
26+
when: mdadm_arrays | selectattr('auto_detect', 'defined') | selectattr('auto_detect', 'equalto', true) | list | length > 0
27+
28+
- name: main | Validate sufficient disks for auto-detection
29+
fail:
30+
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."
31+
with_items: "{{ mdadm_arrays }}"
1732
when: >
18-
mdadm_auto_detect_arrays and
19-
(mdadm_arrays | length == 0)
33+
item.auto_detect | default(false) and
34+
(item.devices is not defined or item.devices | length == 0) and
35+
unused_disks is defined and
36+
unused_disks | length < item.min_disks | default(3)
2037
21-
- name: main | Set auto-detected arrays
38+
- name: main | Set auto-detected devices for arrays with auto_detect
2239
set_fact:
23-
mdadm_arrays: "{{ mdadm_auto_detect_config | map('combine', {'devices': unused_disks}) | list }}"
40+
mdadm_arrays: "{{ mdadm_arrays_updated }}"
41+
vars:
42+
mdadm_arrays_updated: >-
43+
{%- set result = [] -%}
44+
{%- for array in mdadm_arrays -%}
45+
{%- if array.auto_detect | default(false) and (array.devices is not defined or array.devices | length == 0) -%}
46+
{%- if unused_disks | length >= array.min_disks | default(3) -%}
47+
{%- set updated_array = array | combine({'devices': unused_disks}) -%}
48+
{%- set _ = result.append(updated_array) -%}
49+
{%- else -%}
50+
{%- set _ = result.append(array) -%}
51+
{%- endif -%}
52+
{%- else -%}
53+
{%- set _ = result.append(array) -%}
54+
{%- endif -%}
55+
{%- endfor -%}
56+
{{ result }}
2457
when: >
25-
mdadm_auto_detect_arrays and
26-
(mdadm_arrays | length == 0) and
27-
(unused_disks | length >= mdadm_auto_detect_config[0].min_disks)
58+
unused_disks is defined and
59+
mdadm_arrays | selectattr('auto_detect', 'defined') | selectattr('auto_detect', 'equalto', true) | list | length > 0
2860
2961
- include_tasks: arrays.yml

0 commit comments

Comments
 (0)