如何使用`json_query`过滤和重命名hasmap列表中的元素

问题描述 投票:0回答:2

我正在尝试使用

json_query

从哈希映射列表中过滤和重命名一些元素

我的 ansible 事实中有以下示例数据:

"ansible_mounts": [
    {
        "block_used": 136260,
        "uuid": "fa28207c-c4cf-4afd-a2e8-7090c3e4263e",
        "size_total": 2136997888,
        "block_total": 521728,
        "mount": "/disk1",
        "block_available": 385468,
        "size_available": 1578876928,
        "fstype": "xfs",
        "inode_total": 1048576,
        "inode_available": 1048572,
        "device": "/dev/sdb1",
        "inode_used": 4,
        "block_size": 4096,
        "options": "rw,seclabel,relatime,attr2,inode64,noquota"
    },
    {
        "block_used": 443460,
        "uuid": "f2a3d15f-f63a-4b07-bfe6-aa1fa3366c58",
        "size_total": 3210739712,
        "block_total": 783872,
        "mount": "/mnt/disk2",
        "block_available": 340412,
        "size_available": 1394327552,
        "fstype": "xfs",
        "inode_total": 1572864,
        "inode_available": 1572860,
        "device": "/dev/sdb2",
        "inode_used": 4,
        "block_size": 4096,
        "options": "rw,seclabel,relatime,attr2,inode64,noquota"
    },
    {
        "block_used": 482382,
        "uuid": "17764e52-0259-4918-b8c6-d4b99041db75",
        "size_total": 10725883904,
        "block_total": 2618624,
        "mount": "/",
        "block_available": 2136242,
        "size_available": 8750047232,
        "fstype": "xfs",
        "inode_total": 5242368,
        "inode_available": 5184047,
        "device": "/dev/sda1",
        "inode_used": 58321,
        "block_size": 4096,
        "options": "rw,seclabel,relatime,attr2,inode64,noquota"
    }
]

目前我正在使用以下查询:

- set_fact:
    mountlist: "{{ ansible_mounts | json_query('{device: [*].device, name: [*].mount, size: [*].size_available}') }}"

- debug:
    msg: "{{ mountlist }}"

我得到以下结果:

"msg": {
  "device": [
    "/dev/sdb1", 
    "/dev/sdb2", 
    "/dev/sda1"
  ], 
  "name": [
    "/disk1", 
    "/mnt/disk2", 
    "/"
  ], 
  "size": [
    1578876928, 
    1394327552, 
    8736772096
  ]
}

然而,我期待的是:

"msg": [
   {
      "name": "/disk1",
      "device": "/dev/sdb1"
      "size": 1578876928
   },
   {
      "name": "/mnt/disk2",
      "device": "/dev/sdb2"
      "size": 1394327552
   },
   {
      "name": "/",
      "device": "/dev/sda1"
      "size": 8736772096
   }
]

我无法弄清楚我的查询出了什么问题以及如何更改它以获得正确的结果。

ansible json-query
2个回答
1
投票

json_query
使用
jmespath
。您应该查看教程示例规范

在您的具体情况下,您得到的结果与您编写的过滤器严格一致。以下任务是如何修改它以获得所需结果的示例:

- name: Filter + Rename elements from my source list
  debug:
    msg: "{{ ansible_mounts | json_query('[].{device: device, name: mount, size: size_available}') }}"

使用您的示例数据给出:

TASK [Filter + Rename elements from my source list] ***********************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "device": "/dev/sdb1",
            "name": "/disk1",
            "size": 1578876928
        },
        {
            "device": "/dev/sdb2",
            "name": "/mnt/disk2",
            "size": 1394327552
        },
        {
            "device": "/dev/sda1",
            "name": "/",
            "size": 8750047232
        }
    ]
}

0
投票
下面的

json_query完成了这项工作

- set_fact:
    mountlist: "{{ ansible_mounts|
                   json_query('[*].{mount: mount,
                                   device: device,
                                   size: size_available}')
                                   }}"
© www.soinside.com 2019 - 2024. All rights reserved.