json 通过Ansible获取特定大小的磁盘

vxf3dgd4  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(187)

我有json格式的ansible_devices变量,我只想得到一些特定大小的磁盘名称列表。我尝试在this howto之后使用json_queryfilter,但我无法获取数据,变量my_disks为空。但是如果我不使用json_query过滤器,它会在下面用json填充。

- name: set facts
      vars:
        query_disks: "[?sectors==`7814037168`].dev"
      set_fact:
        my_disks: "{{ ansible_devices | json_query('[?sectors==`7814037168`]') }}"

    - name: print found disks with specific size 7814037168
      debug:
        msg:
          - my_disks "{{ my_disks }}"

JSON:

{
"ansible_devices": {
    "dm-0": {
        "holders": [],
        "host": "",
        "links": {
            "ids": [
                "dm-name-ubuntu--vg-ubuntu--lv",
                "dm-uuid-LVM-JTbEQFwBnnRZ8qLNZZZJJSAfYxYnnbUyVJzynNhorlBulf0g1H2tTpVX8lPGGpdP"
            ],
            "labels": [],
            "masters": [],
            "uuids": [
                "c634c808-d0fe-4579-a6fa-747b36ab6627"
            ]
        },
        "model": null,
        "partitions": {},
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "",
        "sectors": "209715200",
        "sectorsize": "512",
        "size": "100.00 GB",
        "support_discard": "0",
        "vendor": null,
        "virtual": 1
    },
    "loop0": {
        "holders": [],
        "host": "",
        "links": {
            "ids": [],
            "labels": [],
            "masters": [],
            "uuids": []
        },
        "model": null,
        "partitions": {},
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "none",
        "sectors": "113896",
        "sectorsize": "512",
        "size": "55.61 MB",
        "support_discard": "4096",
        "vendor": null,
        "virtual": 1
    },
    "loop1": {
        "holders": [],
        "host": "",
        "links": {
            "ids": [],
            "labels": [],
            "masters": [],
            "uuids": []
        },
        "model": null,
        "partitions": {},
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "none",
        "sectors": "113888",
        "sectorsize": "512",
        "size": "55.61 MB",
        "support_discard": "4096",
        "vendor": null,
        "virtual": 1
    },
    "loop2": {
        "holders": [],
        "host": "",
        "links": {
            "ids": [],
            "labels": [],
            "masters": [],
            "uuids": []
        },
        "model": null,
        "partitions": {},
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "none",
        "sectors": "129672",
        "sectorsize": "512",
        "size": "63.32 MB",
        "support_discard": "4096",
        "vendor": null,
        "virtual": 1
    },
    "loop3": {
        "holders": [],
        "host": "",
        "links": {
            "ids": [],
            "labels": [],
            "masters": [],
            "uuids": []
        },
        "model": null,
        "partitions": {},
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "none",
        "sectors": "129712",
        "sectorsize": "512",
        "size": "63.34 MB",
        "support_discard": "4096",
        "vendor": null,
        "virtual": 1
    },
    "loop4": {
        "holders": [],
        "host": "",
        "links": {
            "ids": [],
            "labels": [],
            "masters": [],
            "uuids": []
        },
        "model": null,
        "partitions": {},
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "none",
        "sectors": "188072",
        "sectorsize": "512",
        "size": "91.83 MB",
        "support_discard": "4096",
        "vendor": null,
        "virtual": 1
    },
    "loop5": {
        "holders": [],
        "host": "",
        "links": {
            "ids": [],
            "labels": [],
            "masters": [],
            "uuids": []
        },
        "model": null,
        "partitions": {},
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "none",
        "sectors": "188032",
        "sectorsize": "512",
        "size": "91.81 MB",
        "support_discard": "4096",
        "vendor": null,
        "virtual": 1
    },
    "loop6": {
        "holders": [],
        "host": "",
        "links": {
            "ids": [],
            "labels": [],
            "masters": [],
            "uuids": []
        },
        "model": null,
        "partitions": {},
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "none",
        "sectors": "109032",
        "sectorsize": "512",
        "size": "53.24 MB",
        "support_discard": "4096",
        "vendor": null,
        "virtual": 1
    },
    "loop7": {
        "holders": [],
        "host": "",
        "links": {
            "ids": [],
            "labels": [],
            "masters": [],
            "uuids": []
        },
        "model": null,
        "partitions": {},
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "none",
        "sectors": "109032",
        "sectorsize": "512",
        "size": "53.24 MB",
        "support_discard": "4096",
        "vendor": null,
        "virtual": 1
    },
    "loop8": {
        "holders": [],
        "host": "",
        "links": {
            "ids": [],
            "labels": [],
            "masters": [],
            "uuids": []
        },
        "model": null,
        "partitions": {},
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "none",
        "sectors": "0",
        "sectorsize": "512",
        "size": "0.00 Bytes",
        "support_discard": "4096",
        "vendor": null,
        "virtual": 1
    },
    "sda": {
        "holders": [],
        "host": "IDE interface: Intel Corporation 6 Series/C200 Series Chipset Family Desktop SATA Controller (IDE mode, ports 0-3) (rev 05)",
        "links": {
            "ids": [
                "ata-MB0500EBNCR_WMAYP3116422",
                "scsi-0ATA_MB0500EBNCR_WMAYP3116422",
                "scsi-1ATA_MB0500EBNCR_WMAYP3116422",
                "scsi-350014ee00320fd29",
                "scsi-SATA_MB0500EBNCR_WMAYP3116422",
                "wwn-0x50014ee00320fd29"
            ],
            "labels": [],
            "masters": [],
            "uuids": []
        },
        "model": "MB0500EBNCR",
        "partitions": {
            "sda1": {
                "holders": [],
                "links": {
                    "ids": [
                        "ata-MB0500EBNCR_WMAYP3116422-part1",
                        "scsi-0ATA_MB0500EBNCR_WMAYP3116422-part1",
                        "scsi-1ATA_MB0500EBNCR_WMAYP3116422-part1",
                        "scsi-350014ee00320fd29-part1",
                        "scsi-SATA_MB0500EBNCR_WMAYP3116422-part1",
                        "wwn-0x50014ee00320fd29-part1"
                    ],
                    "labels": [],
                    "masters": [],
                    "uuids": []
                },
                "sectors": "2048",
                "sectorsize": 512,
                "size": "1.00 MB",
                "start": "2048",
                "uuid": null
            },
            "sda2": {
                "holders": [],
                "links": {
                    "ids": [
                        "ata-MB0500EBNCR_WMAYP3116422-part2",
                        "scsi-0ATA_MB0500EBNCR_WMAYP3116422-part2",
                        "scsi-1ATA_MB0500EBNCR_WMAYP3116422-part2",
                        "scsi-350014ee00320fd29-part2",
                        "scsi-SATA_MB0500EBNCR_WMAYP3116422-part2",
                        "wwn-0x50014ee00320fd29-part2"
                    ],
                    "labels": [],
                    "masters": [],
                    "uuids": [
                        "b56a8fd9-dcb6-4861-be97-fbc0e2d35261"
                    ]
                },
                "sectors": "2097152",
                "sectorsize": 512,
                "size": "1.00 GB",
                "start": "4096",
                "uuid": "b56a8fd9-dcb6-4861-be97-fbc0e2d35261"
            },
            "sda3": {
                "holders": [
                    "ubuntu--vg-ubuntu--lv"
                ],
                "links": {
                    "ids": [
                        "ata-MB0500EBNCR_WMAYP3116422-part3",
                        "lvm-pv-uuid-zczj1C-x1rv-e5KW-a9MJ-F3cn-Uidi-jF2OZb",
                        "scsi-0ATA_MB0500EBNCR_WMAYP3116422-part3",
                        "scsi-1ATA_MB0500EBNCR_WMAYP3116422-part3",
                        "scsi-350014ee00320fd29-part3",
                        "scsi-SATA_MB0500EBNCR_WMAYP3116422-part3",
                        "wwn-0x50014ee00320fd29-part3"
                    ],
                    "labels": [],
                    "masters": [
                        "dm-0"
                    ],
                    "uuids": []
                },
                "sectors": "974669824",
                "sectorsize": 512,
                "size": "464.76 GB",
                "start": "2101248",
                "uuid": null
            }
        },
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "mq-deadline",
        "sectors": "976773168",
        "sectorsize": "512",
        "size": "465.76 GB",
        "support_discard": "0",
        "vendor": "ATA",
        "virtual": 1,
        "wwn": "0x50014ee00320fd29"
    },
    "sdb": {
        "holders": [],
        "host": "IDE interface: Intel Corporation 6 Series/C200 Series Chipset Family Desktop SATA Controller (IDE mode, ports 0-3) (rev 05)",
        "model": "ST4000VX013-2XG1",
        "partitions": {},
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "mq-deadline",
        "sectors": "7814037168",
        "sectorsize": "512",
        "size": "3.64 TB",
        "support_discard": "0",
        "vendor": "ATA",
        "virtual": 1,
        "wwn": "0x5000c500e04f7ede"
    },
    "sdc": {
        "holders": [],
        "host": "IDE interface: Intel Corporation 6 Series/C200 Series Chipset Family Desktop SATA Controller (IDE mode, ports 0-3) (rev 05)",
        "links": {
            "ids": [
                "ata-MB0500EBNCR_WMAYP3118198",
                "scsi-0ATA_MB0500EBNCR_WMAYP3118198",
                "scsi-1ATA_MB0500EBNCR_WMAYP3118198",
                "scsi-350014ee05876512e",
                "scsi-SATA_MB0500EBNCR_WMAYP3118198",
                "wwn-0x50014ee05876512e"
            ],
            "labels": [],
            "masters": [],
            "uuids": []
        },
        "model": "MB0500EBNCR",
        "partitions": {
            "sdc2": {
                "holders": [],
                "links": {
                    "ids": [
                        "ata-MB0500EBNCR_WMAYP3118198-part2",
                        "scsi-0ATA_MB0500EBNCR_WMAYP3118198-part2",
                        "scsi-1ATA_MB0500EBNCR_WMAYP3118198-part2",
                        "scsi-350014ee05876512e-part2",
                        "scsi-SATA_MB0500EBNCR_WMAYP3118198-part2",
                        "wwn-0x50014ee05876512e-part2"
                    ],
                    "labels": [],
                    "masters": [],
                    "uuids": []
                },
                "sectors": "5254202",
                "sectorsize": 512,
                "size": "2.51 GB",
                "start": "16255488",
                "uuid": null
            }
        },
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "mq-deadline",
        "sectors": "976773168",
        "sectorsize": "512",
        "size": "465.76 GB",
        "support_discard": "0",
        "vendor": "ATA",
        "virtual": 1,
        "wwn": "0x50014ee05876512e"
    },
    "sdd": {
        "holders": [],
        "host": "IDE interface: Intel Corporation 6 Series/C200 Series Chipset Family Desktop SATA Controller (IDE mode, ports 0-3) (rev 05)",
        "links": {
            "ids": [
                "ata-ST4000VX013-2XG104_WFN6EZDR",
                "scsi-0ATA_ST4000VX013-2XG1_WFN6EZDR",
                "scsi-1ATA_ST4000VX013-2XG104_WFN6EZDR",
                "scsi-35000c500e04f9132",
                "scsi-SATA_ST4000VX013-2XG1_WFN6EZDR",
                "wwn-0x5000c500e04f9132"
            ],
            "labels": [],
            "masters": [],
            "uuids": []
        },
        "model": "ST4000VX013-2XG1",
        "partitions": {},
        "removable": "0",
        "rotational": "1",
        "sas_address": null,
        "sas_device_handle": null,
        "scheduler_mode": "mq-deadline",
        "sectors": "7814037168",
        "sectorsize": "512",
        "size": "3.64 TB",
        "support_discard": "0",
        "vendor": "ATA",
        "virtual": 1,
        "wwn": "0x5000c500e04f9132"
    }
}
}
brc7rcf0

brc7rcf01#

我发现其他solution

- name: set facts
      set_fact:
       my_disks: "{{ ansible_devices | dict2items | selectattr('key', 'match', '^sd.*') | rejectattr('key', 'match', '^sda$') | selectattr('value.size', 'match', '^3.64 TB') | map(attribute='key') | list }}"
ok: [server] => {
    "ansible_facts": {
        "my_disks": [
            "sdd",
            "sdb"
        ]
    },
    "changed": false
}

谢谢!

相关问题