我正在尝试查询当前主机的ansible_devices变量,以提取当前没有分区的所有SD磁盘设备。基于此,我需要创建/更新一个变量,该变量将根据磁盘的大小将磁盘分配给卷组名称,然后该磁盘将被另一个任务使用。
有人知道我该怎么做吗?例如,我有以下测试剧本可与ansible_devices var一起玩,但我一直未能达到最终目标。
---
- hosts: all
vars:
disks:
/dev/disk/notfound1 : vg_app
/dev/disk/notfound2 : vg_data
/dev/disk/notfound3 : vg_db
/dev/disk/notfound4 : vg_log
tasks:
- debug:
var: disks
- debug:
msg: " {{ item.key }} = {{ item.value.size }} "
with_dict: "{{ ansible_devices }}"
when: "item.key.startswith('sd') and not item.value.partitions "
现在,我要为vg_app分配10GB的磁盘,为vg_data分配20GB的磁盘,将剩余磁盘中的最大磁盘分配给vg_db,将剩余磁盘中的最后一个磁盘分配给vg_log。
所以现在我的可变磁盘看起来像
disks:
/dev/sdc : vg_app
/dev/sdf : vg_data
/dev/sde : vg_db
/dev/sdd : vg_log
我的下一个任务将使用此变量,并在具有正确大小的正确磁盘上创建正确的卷组。这必须是动态的,因为我不能保证添加到VM的磁盘将始终具有相同的名称。
更新:提出了我自己的丑陋解决方案
- name: Collect all SD disk devices along with their SIZES which don't have a partition
set_fact:
disk_sizes : "{{ disk_sizes|default({}) | combine( {item.value.size.split('.')[0]: '/dev/' + item.key } ) }}"
with_dict: "{{ ansible_devices }}"
when: "item.key.startswith('sd') and not item.value.partitions "
- name: Test we have 4 unprovisioned disks in our vm
fail:
msg: "4 disks were not found on the vm"
when: ( disk_sizes | length ) != 4
- name: reset our disks variable to an empty dictionary
set_fact:
disks: "{{ newdict|default({}) }}"
- name: create my new dictionary that will have my disks assigned
set_fact:
disks: "{{ disks|default({}) | combine( { item.key: item.value } ) }}"
with_items:
- { key: "{{disk_sizes['10']}}", value: 'vg_app' }
- { key: "{{disk_sizes['20']}}", value: 'vg_data' }
- name: removed the disks we have used so far
set_fact:
disks2: "{{ disks2|default({}) |combine({item.key: item.value})}}"
when: "item.key not in ['10','20']"
with_dict: "{{ disk_sizes }}"
- name: append the other disks to the disks dictionary
set_fact:
disks: "{{ disks|default({}) | combine( { item.key: item.value } ) }}"
with_items:
- { key: "{{disk_sizes[ disks2.keys() |max ]}}", value: 'vg_db' }
- { key: "{{disk_sizes[ disks2.keys() |min ]}}", value: 'vg_log' }