如何循环 ansible_devices[elements]

hfr*_*nco 0 ansible

我可以从主机收集事实,并且可以查看我的驱动器。如何使用 ansible_devices 变量仅获取驱动器(sda、sdb 等)?

我正在尝试做类似的事情:

tasks:
- debug: msg={{item.mount}}
  with_items: ansible_mounts
Run Code Online (Sandbox Code Playgroud)

我尝试了以下调试消息,但我能看到的唯一变量只是一个驱动器:

debug: msg={{ hostvars[inventory_hostname]["ansible_devices"] }}
Run Code Online (Sandbox Code Playgroud)

 "ansible_devices": {
        "sda": {
            "holders": [],
            "host": "Serial Attached SCSI controller: LSI Logic / Symbios Logic SAS2008 PCI-Express Fusion-MPT SAS-2 [Falcon] (rev 03)",
            "model": "Crucial_CT480M50",
            "partitions": {
                "sda1": {
                    "sectors": "2097152",
                    "sectorsize": 512,
                    "size": "1.00 GB",
                    "start": "2048"
                },
                "sda2": {
                    "sectors": "8388608",
                    "sectorsize": 512,
                    "size": "4.00 GB",
                    "start": "2099200"
                },
                "sda3": {
                    "sectors": "927213568",
                    "sectorsize": 512,
                    "size": "442.13 GB",
                    "start": "10487808"
                }
            },
            "removable": "0",
            "rotational": "0",
            "scheduler_mode": "cfq",
            "sectors": "937703088",
            "sectorsize": "4096",
            "size": "3.49 TB",
            "support_discard": "33553920",
            "vendor": "ATA"
        },
        "sdb": {
            "holders": [],
            "host": "Serial Attached SCSI controller: LSI Logic / Symbios Logic SAS2008 PCI-Express Fusion-MPT SAS-2 [Falcon] (rev 03)",
            "model": "Micron_M500_MTFD",
            "partitions": {
                "sdb1": {
                    "sectors": "1875380224",
                    "sectorsize": 512,
                    "size": "894.25 GB",
                    "start": "4096"
                }
            },
            "removable": "0",
            "rotational": "0",
            "scheduler_mode": "cfq",
            "sectors": "1875385008",
            "sectorsize": "4096",
            "size": "6.99 TB",
            "support_discard": "33553920",
            "vendor": "ATA"
        },
Run Code Online (Sandbox Code Playgroud)

yae*_*shi 5

您在模块参数解析中遇到问题。尝试像这样引用论证:

- debug: msg="{{ hostvars[inventory_hostname]["ansible_devices"] }}"
Run Code Online (Sandbox Code Playgroud)

最好使用而var不是msgmodule debug

- debug: var=hostvars[inventory_hostname]["ansible_devices"]
Run Code Online (Sandbox Code Playgroud)

我总是建议将模块参数作为字典传递,以降低引用的复杂性:

- debug:
    var: hostvars[inventory_hostname]["ansible_devices"]
Run Code Online (Sandbox Code Playgroud)

keys()您可以使用 @ydaetskcoR 的答案获取设备名称数组:

- debug:
    var: hostvars[inventory_hostname]["ansible_devices"].keys()
Run Code Online (Sandbox Code Playgroud)