我试图使用可见的hostvars来选择一组磁盘,这是我的代码:
- name: Output disk information
debug:
var: hostvars[inventory_hostname].ansible_devices.keys() | map('regex_search', 'nvme.*') | select('string') | list
我期望的是一个以nvme
或空列表开头的设备列表,但我得到var:
值(如果您愿意查询)以及列表:
ok: [cm42.ag6hq.net] => {
"hostvars[inventory_hostname].ansible_devices.keys() | map('regex_search', 'nvme.*') | select('string') | list": [
"nvme0n1"
]
}
ok: [cm43.ag6hq.net] => {
"hostvars[inventory_hostname].ansible_devices.keys() | map('regex_search', 'nvme.*') | select('string') | list": []
}
我期望的结果是:
ok: [cm42.ag6hq.net] => {
[
"nvme0n1"
]
}
ok: [cm43.ag6hq.net] => {
[]
}
因此,当我在with_items:
上下文中使用此查询时,它试图处理整个查询,而不是结果列表(这是我所期望的):
failed: [cm42.ag6hq.net] (item=hostvars[inventory_hostname].ansible_devices.keys() | map('regex_search', 'nvme.*') | select('string') | list) => {"ansible_loop_var": "item", "changed": false, "item": "hostvars[inventory_hostname].ansible_devices.keys() | map('regex_search', 'nvme.*') | select('string') | list", "msg": "Device /dev/hostvars[inventory_hostname].ansible_devices.keys() | map('regex_search', 'nvme.*') | select('string') | list not found."}
我在哪里越轨了?
让我看看是否可以简化:剧本:
- name: Get list of NVMe block devices
set_fact:
nvme_devices: "{{ hostvars[inventory_hostname]['ansible_devices'] | dict2items | selectattr('key', 'match', '^nvme') | map(attribute='value') | list }}"
- name: Create partitions on the NVMe disks
parted:
device: "/dev/{{ item }}"
number: 1
state: present
align: optimal
unit: "%"
part_type: primary
fs_type: ext4
with_items: "{{ nvme_devices }}"
输出:
TASK [Create partitions on the NVMe disks] *****************************************************************************************************************************
failed: [cm41.ag6hq.net] (item={'virtual': 1, 'links': {'ids': ['nvme-INTEL_SSDPEKNU010TZ_PHKA220103081P0B', 'nvme-eui.0000000001000000e4d25c78cf5e5501'], 'uuids': [], 'labels': [], 'masters': []}, 'vendor': None, 'model': 'INTEL SSDPEKNU010TZ', 'sas_address': None, 'sas_device_handle': None, 'serial': 'PHKA220103081P0B', 'removable': '0', 'support_discard': '512', 'partitions': {}, 'rotational': '0', 'scheduler_mode': 'none', 'sectors': '2000409264', 'sectorsize': '512', 'size': '953.87 GB', 'host': 'Non-Volatile memory controller: Intel Corporation Device f1aa (rev 03)', 'holders': []}) => {"ansible_loop_var": "item", "changed": false, "err": "Error: Could not stat device /dev/{virtual: - No such file or directory.n", "item": {"holders": [], "host": "Non-Volatile memory controller: Intel Corporation Device f1aa (rev 03)", "links": {"ids": ["nvme-INTEL_SSDPEKNU010TZ_PHKA220103081P0B", "nvme-eui.0000000001000000e4d25c78cf5e5501"], "labels": [], "masters": [], "uuids": []}, "model": "INTEL SSDPEKNU010TZ", "partitions": {}, "removable": "0", "rotational": "0", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "none", "sectors": "2000409264", "sectorsize": "512", "serial": "PHKA220103081P0B", "size": "953.87 GB", "support_discard": "512", "vendor": null, "virtual": 1}, "msg": "Error while getting device information with parted script: '/usr/sbin/parted -s -m /dev/{'virtual': 1, 'links': {'ids': ['nvme-INTEL_SSDPEKNU010TZ_PHKA220103081P0B', 'nvme-eui.0000000001000000e4d25c78cf5e5501'], 'uuids': [], 'labels': [], 'masters': []}, 'vendor': None, 'model': 'INTEL SSDPEKNU010TZ', 'sas_address': None, 'sas_device_handle': None, 'serial': 'PHKA220103081P0B', 'removable': '0', 'support_discard': '512', 'partitions': {}, 'rotational': '0', 'scheduler_mode': 'none', 'sectors': '2000409264', 'sectorsize': '512', 'size': '953.87 GB', 'host': 'Non-Volatile memory controller: Intel Corporation Device f1aa (rev 03)', 'holders': []} -- unit '%' print'", "out": "", "rc": 1}
failed: [cm42.ag6hq.net] (item={'virtual': 1, 'links': {'ids': ['nvme-INTEL_SSDPEKNU010TZ_PHKA219401S71P0B', 'nvme-eui.0000000001000000e4d25cc6ae5c5501'], 'uuids': [], 'labels': [], 'masters': []}, 'vendor': None, 'model': 'INTEL SSDPEKNU010TZ', 'sas_address': None, 'sas_device_handle': None, 'serial': 'PHKA219401S71P0B', 'removable': '0', 'support_discard': '512', 'partitions': {}, 'rotational': '0', 'scheduler_mode': 'none', 'sectors': '2000409264', 'sectorsize': '512', 'size': '953.87 GB', 'host': 'Non-Volatile memory controller: Intel Corporation Device f1aa (rev 03)', 'holders': []}) => {"ansible_loop_var": "item", "changed": false, "err": "Error: Could not stat device /dev/{virtual: - No such file or directory.n", "item": {"holders": [], "host": "Non-Volatile memory controller: Intel Corporation Device f1aa (rev 03)", "links": {"ids": ["nvme-INTEL_SSDPEKNU010TZ_PHKA219401S71P0B", "nvme-eui.0000000001000000e4d25cc6ae5c5501"], "labels": [], "masters": [], "uuids": []}, "model": "INTEL SSDPEKNU010TZ", "partitions": {}, "removable": "0", "rotational": "0", "sas_address": null, "sas_device_handle": null, "scheduler_mode": "none", "sectors": "2000409264", "sectorsize": "512", "serial": "PHKA219401S71P0B", "size": "953.87 GB", "support_discard": "512", "vendor": null, "virtual": 1}, "msg": "Error while getting device information with parted script: '/usr/sbin/parted -s -m /dev/{'virtual': 1, 'links': {'ids': ['nvme-INTEL_SSDPEKNU010TZ_PHKA219401S71P0B', 'nvme-eui.0000000001000000e4d25cc6ae5c5501'], 'uuids': [], 'labels': [], 'masters': []}, 'vendor': None, 'model': 'INTEL SSDPEKNU010TZ', 'sas_address': None, 'sas_device_handle': None, 'serial': 'PHKA219401S71P0B', 'removable': '0', 'support_discard': '512', 'partitions': {}, 'rotational': '0', 'scheduler_mode': 'none', 'sectors': '2000409264', 'sectorsize': '512', 'size': '953.87 GB', 'host': 'Non-Volatile memory controller: Intel Corporation Device f1aa (rev 03)', 'holders': []} -- unit '%' print'", "out": "", "rc": 1}
从"Error: Could not stat device /dev/{virtual: - No such file or directory.n"
错误可以明显看出,nvme_devices
不是设备名称列表,而是整个设备(参见输出中的item='virtual...
)。
那么为什么"{{ hostvars[inventory_hostname]['ansible_devices'] | dict2items | selectattr('key', 'match', '^nvme') | map(attribute='value') | list }}"
不创建一个名字列表呢?
回答:因为map属性是'value'
而不是'key'
"{{ hostvars[inventory_hostname]['ansible_devices'] | dict2items | selectattr('key', 'match', '^nvme') | map(attribute='key') | list }}"
将创建一个以nvme开头的名称列表。