You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Ansible移除API返回字典中uuid字段的连字符

移除Ansible中API返回字典uuid字段的连字符

需要移除API返回数据中uuid字段值里的-,现有的Ansible代码未能实现该需求,以下是当前代码及API返回示例:

当前Ansible代码

block:
- name: 获取源卷列表
  uri:
    url: "https://{{ dms_ip }}/api/source/volumes"
    url_username: "{{ credential_dms.username }}"
    url_password: "{{ credential_dms.password }}"
    method: GET
    force_basic_auth: true
    validate_certs: false
  register: source_volume_data

- name: 注册主机完整详情
  set_fact:
    hostnamea: "{{ source_volume_data.json.volumes | json_query('[].{hostName: hostName, uuid: uuid, deviceName: deviceName, capacity: capacity}') }}"

API返回示例

{
    "capacity": "34361180160",
    "destinationInUse": false,
    "destinationPurpose": "PURPOSE_UNKNOWN",
    "deviceDisplayName": "Dev-S-02210",
    "deviceName": "Dev-S-02210",
    "firstPathLun": 41,
    "hostId": 7,
    "hostName": "Host005",
    "inquiry": "EMC_____SYMMETRIX_______5977",
    "pathCount": 2,
    "paths": [],
    "product": "SYMMETRIX       ",
    "rev": "5977",
    "sectorSize": 512,
    "sharedDevice": false,
    "status": "ONLINE",
    "storageArrayId": 11,
    "storageArrayName": "EMC-11",
    "type": "NEXUS",
    "uuid": "60000970-0002-9670-0118-533032343211",
    "vendor": "EMC     "
},
{
    "capacity": "34361180160",
    "destinationInUse": false,
    "destinationPurpose": "PURPOSE_UNKNOWN",
    "deviceDisplayName": "Dev-S-02211",
    "deviceName": "Dev-S-02211",
    "firstPathLun": 42,
    "hostId": 7,
    "hostName": "Host005",
    "inquiry": "EMC_____SYMMETRIX_______5977",
    "pathCount": 2,
    "paths": [],
    "product": "SYMMETRIX       ",
    "rev": "5977",
    "sectorSize": 512,
    "sharedDevice": false,
    "status": "ONLINE",
    "storageArrayId": 11,
    "storageArrayName": "EMC-11",
    "type": "NEXUS",
    "uuid": "60000970-0002-9670-0118-533032343711",
    "vendor": "EMC     "
}

解决方案

方法一:基于现有json_query结果处理

保留原json_query提取逻辑,通过列表推导式遍历结果,用combine和regex_replace替换uuid中的连字符:

- name: 处理uuid并注册主机详情
  set_fact:
    hostnamea: >-
      {% set raw_volumes = source_volume_data.json.volumes | json_query('[].{hostName: hostName, uuid: uuid, deviceName: deviceName, capacity: capacity}') %}
      {{ [ item | combine({'uuid': item.uuid | regex_replace('-', '')}) for item in raw_volumes ] }}

方法二:直接遍历原始数据提取并处理

跳过json_query,直接遍历API返回的卷列表,在构建字典时直接处理uuid:

- name: 提取并处理uuid后注册主机详情
  set_fact:
    hostnamea: >-
      {{ [ 
          {
            'hostName': item.hostName,
            'uuid': item.uuid | regex_replace('-', ''),
            'deviceName': item.deviceName,
            'capacity': item.capacity
          } 
          for item in source_volume_data.json.volumes 
        ] }}

说明

  • regex_replace('-', ''):将uuid字符串中的所有-替换为空,实现移除效果。
  • combine:用于合并原字典和修改后的uuid字段,保留其他字段不变。

内容的提问来源于stack exchange,提问作者kalyanyellapu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 09:47:02