AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79556368
Accepted
zagpoint
zagpoint
Asked: 2025-04-05 07:25:48 +0800 CST2025-04-05 07:25:48 +0800 CST 2025-04-05 07:25:48 +0800 CST

您可以动态更新 ansible 字典吗?

  • 772

我从 REST API 调用的输出中捕获了一个 dict/json 类型的对象,假设它位于一个名为ouput的变量/寄存器中。数据如下所示。为了完整起见,我分享了整个对象并做了一些基本的清理。请只关注我接下来要讨论的那一行 ;-)

            {
            "_create_time": 1724823837185,
            "_create_user": "system",
            "_last_modified_time": 1736218001919,
            "_last_modified_user": "nsx_policy",
            "_protection": "NOT_PROTECTED",
            "_revision": 1,
            "_system_owned": false,
            "display_name": "w1-vc1-c4n17.example.com",
            "host_switch_spec": {
                "host_switches": [
                    {
                        "cpu_config": [],
                        "ecmp_mode": "L3",
                        "host_switch_id": "50 01 cb 21 be 19 cb 38-78 bd 67 bc b3 0e 91 f1",
                        "host_switch_mode": "STANDARD",
                        "host_switch_name": "w1-vc1-c4-dvs",
                        "host_switch_profile_ids": [
                            {
                                "key": "UplinkHostSwitchProfile",
                                "value": "/infra/host-switch-profiles/6d7a0ed8-c86a-4190-9121-ce6f863226ae"
                            },
                            {
                                "key": "VtepHAHostSwitchProfile",
                                "value": "/infra/host-switch-profiles/0de8282e-7385-4e8e-a905-0c11960db728"
                            }
                        ],
                        "host_switch_type": "VDS",
                        "ip_assignment_spec": {
                            "ip_pool_id": "/infra/ip-pools/w1-nsx1-esxi-teps",
                            "resource_type": "StaticIpPoolSpec"
                        },
                        "is_migrate_pnics": false,
                        "not_ready": false,
                        "portgroup_transport_zone_id": "/infra/sites/default/enforcement-points/default/transport-zones/77166711-1f83-407f-8c27-a4e1b8b4e1e5",
                        "transport_zone_endpoints": [
                            {
                                "transport_zone_id": "/infra/sites/default/enforcement-points/default/transport-zones/4b367ddf-d4ac-460b-a884-24846ff4edf6",
                                "transport_zone_profile_ids": [
                                    {
                                        "profile_id": "/infra/transport-zone-profiles/52035bb3-ab02-4a08-9884-18631312e50a",
                                        "resource_type": "BfdHealthMonitoringProfile"
                                    }
                                ]
                            },
                            {
                                "transport_zone_id": "/infra/sites/default/enforcement-points/default/transport-zones/bc19b340-f0b5-46d8-9454-6d94d3288821",
                                "transport_zone_profile_ids": [
                                    {
                                        "profile_id": "/infra/transport-zone-profiles/52035bb3-ab02-4a08-9884-18631312e50a",
                                        "resource_type": "BfdHealthMonitoringProfile"
                                    }
                                ]
                            }
                        ],
                        "uplinks": [
                            {
                                "uplink_name": "uplink-2",
                                "vds_uplink_name": "uplink2"
                            },
                            {
                                "uplink_name": "uplink-1",
                                "vds_uplink_name": "uplink1"
                            }
                        ]
                    }
                ],
                "resource_type": "StandardHostSwitchSpec"
            },
            "id": "w1-vc1-c4n17-6adb838f-669c-48ca-b76f-e1238984ef93host-2171",
            "is_overridden": false,
            "maintenance_mode": "DISABLED",
            "marked_for_delete": false,
            "node_deployment_info": {
                "compute_collection_id": "6adb838f-669c-48ca-b76f-e1238984ef93:domain-c2147",
                "discovered_ip_addresses": [
                    "x.x.x.x",
                    "x.x.x.x"
                ],
                "discovered_node_id": "6adb838f-669c-48ca-b76f-e1238984ef93:host-2171",
                "fqdn": "w1-vc1-c4n17.example.com",
                "ip_addresses": [
                    "10.x.x.x"
                ],
                "managed_by_server": "10.x.x.x",
                "os_type": "ESXI",
                "os_version": "x.x.x"
            },
            "overridden": false,
            "owner_id": "1ae23909-d587-4683-a515-4c2001940803",
            "parent_path": "/infra/sites/default/enforcement-points/default",
            "path": "/infra/sites/default/enforcement-points/default/host-transport-nodes/w1-vc1-c4n17-6adb838f-669c-48ca-b76f-e1238984ef93host-2171",
            "realization_id": "c2c69a21-698a-4586-869a-7c0b0a651601",
            "relative_path": "w1-vc1-c4n17-6adb838f-669c-48ca-b76f-e1238984ef93host-2171",
            "remote_path": "",
            "resource_type": "HostTransportNode",
            "unique_id": "c2c69a21-698a-4586-869a-7c0b0a651601"
        }

我想通过更改以下行来更新该对象:

"host_switch_mode": "STANDARD",

到:

"host_switch_mode": "LEGACY",

我会将结果 JSON 作为消息体传递给 POST API 调用。我尝试过 anislbe 的组合过滤器,但没有得到预期的结果。例如:

- Name: example post/put API call
  uri:
    url: "https://{{ nsx }}/policy/api/v1/infra/host-transport-node-profiles/{{ id }}"
        force_basic_auth: yes
        validate_certs: no
        headers:
          Accept: "application/json"
          Content-Type: "application/json"
        user: "{{ username }}"
        password: "{{ password }}"
        method: PUT
        body: "{{ ouput | combine( {'host_switch_spec': {'host_switches': [ {'host_switch_mode': "LEGACY"},],}, }, recursive=true) }}"
        status_code: "200"
        body_format: json

使用组合过滤器,我看到的是,整个 host_switch_spec 部分被替换(因此丢失了其他属性,如 host_switch_name、host_switch_id 等),而不是仅仅合并相关行,这不是我想要的:

            "host_switch_spec": {
            "host_switches": [
                {
                    "host_switch_mode": "STANDARD"
                }
            ],
            "resource_type": "StandardHostSwitchSpec"
        },

我做错了什么?我查看了组合过滤器的手册页,并尝试了所有list_merge选项,但都无济于事。


更新:

为了简单起见,我在上一篇文章中没有提到的一个复杂问题是,我有一个包含这些“输出”数据对象的列表,我需要像下面这样循环处理它们。假设这个列表在变量“tn”中:

  # Modify the transport node json data on the fly and feed it to the API as message body
  - debug: 
      msg: 
        - "{{ item }}"
        - "{{ item | combine( {'host_switch_spec': {'host_switches': [ {'host_switch_mode': mode}, ], }, },) }}"
    with_items: "{{ tn }}"
    loop_control:
      label: "{{ item.display_name }}"

概括:

感谢大家的帮助和指导。我还没有完成端到端测试,但以下是我根据 Vladimir Botka 提供的精彩示例得出的结论:

      # how to modify the transport node json data on the fly and feed it to the API as message body
  - name: Testing switch mode update on the fly
    debug: 
      msg:
        #- "hs: {{ (item.host_switch_spec.host_switches + [ hs_update ]) | combine }}"
        #- "update: {{ {'host_switch_spec': {'host_switches': [ (item.host_switch_spec.host_switches + [ hs_update ]) | combine ]}} }}"
        #- "result: {{ item | combine({'host_switch_spec': {'host_switches': [ (item.host_switch_spec.host_switches + [ hs_update ]) | combine ]}}) }}"
        - "{{ item | combine({'host_switch_spec': {'host_switches': [ (item.host_switch_spec.host_switches + [ hs_update ]) | combine ]}}) }}"
    loop: "{{ tn }}"
    loop_control:
      label: "{{ item.display_name }}"
      index_var: counter
    vars:
      hs_update:
        host_switch_mode: "LEGACY"

结果看起来很有希望(显示最后一个-#24,并且仅显示部分以避免混乱):

ok: [localhost] => (item=w11-vc1-c1n24.example.com) => {
"msg": [
    "24':' STANDARD",
    {
        "_create_time": 1743198123451,
        "_create_user": "system",
        "_last_modified_time": 1743445520238,
        "_last_modified_user": "system",
        "_protection": "NOT_PROTECTED",
        "_revision": 2,
        "_system_owned": false,
        "display_name": "w11-vc1-c1n24.example.com",
        "host_switch_spec": {
            "host_switches": [
                {
                    "cpu_config": [],
                    "ecmp_mode": "L3",
                    "host_switch_id": "50 1e 66 c9 c5 fb da b3-5e 1f 45 fc 2a 42 99 50",
                    "host_switch_mode": "LEGACY",
                    "host_switch_name": "w11-vc1-dvs",
                    "host_switch_profile_ids": [
                        {
                            "key": "UplinkHostSwitchProfile",
                            "value": "/infra/host-switch-profiles/ace9be64-9728-4340-968e-a62dae6a3d00"
                        },
                        {
                            "key": "VtepHAHostSwitchProfile",
                            "value": "/infra/host-switch-profiles/0de8282e-7385-4e8e-a905-0c11960db728"
                        }
                    ],
                    "host_switch_type": "VDS",
json
  • 1 1 个回答
  • 75 Views

1 个回答

  • Voted
  1. Best Answer
    Vladimir Botka
    2025-04-05T23:43:42+08:002025-04-05T23:43:42+08:00

    output.host_switch_spec.host_switches是包含一个项目的列表

        output.host_switch_spec.host_switches:
        -   cpu_config: []
            ecmp_mode: L3
            host_switch_id: 50 01 cb 21 be 19 cb 38-78 bd 67 bc b3 0e 91 f1
            host_switch_mode: STANDARD
            host_switch_name: w1-vc1-c4-dvs
        ...
    

    将要更新的内容输入字典中。例如:

        hs_update:
          host_switch_mode: LEGACY
    

    合并词典

        hs: "{{ (output.host_switch_spec.host_switches + [hs_update]) | combine }}"
    

    给出

        hs:
            cpu_config: []
            ecmp_mode: L3
            host_switch_id: 50 01 cb 21 be 19 cb 38-78 bd 67 bc b3 0e 91 f1
            host_switch_mode: LEGACY
            host_switch_name: w1-vc1-c4-dvs
        ...
    

    注意:如果有更多项目,您必须创建一个产品并映射组合功能。


    创建字典更新

        update:
          host_switch_spec:
            host_switches: "{{ [hs] }}"
    

    并合并结果

        result: "{{ output | combine(update) }}"
    

    给出

        result:
            _create_time: 1724823837185
            _create_user: system
            _last_modified_time: 1736218001919
            _last_modified_user: nsx_policy
            _protection: NOT_PROTECTED
            _revision: 1
            _system_owned: false
            display_name: w1-vc1-c4n17.example.com
            host_switch_spec:
                host_switches:
                -   cpu_config: []
                    ecmp_mode: L3
                    host_switch_id: 50 01 cb 21 be 19 cb 38-78 bd 67 bc b3 0e 91 f1
                    host_switch_mode: LEGACY
                    host_switch_name: w1-vc1-c4-dvs
        ...
    

    完整测试剧本示例

    - hosts: localhost
    
      vars:
    
        hs_update:
          host_switch_mode: LEGACY
        hs: "{{ (output.host_switch_spec.host_switches + [hs_update]) | combine }}"
    
        update:
          host_switch_spec:
            host_switches: "{{ [hs] }}"
        result: "{{ output | combine(update) }}"
    
      tasks:
    
        - include_vars:
            file: data.json
            name: output
    
        - debug:
            var: output.host_switch_spec.host_switches
    
        - debug:
            var: hs
    
        - debug:
            var: result
    

    问:** “‘host_switch_spec’ 同时具有‘host_switches’ 和‘resource_type’ ”

    A:递归地合并字典

        result: "{{ output | combine(update, recursive=True) }}"
    

    给出

        result:
            _create_time: 1724823837185
            _create_user: system
            _last_modified_time: 1736218001919
            _last_modified_user: nsx_policy
            _protection: NOT_PROTECTED
            _revision: 1
            _system_owned: false
            display_name: w1-vc1-c4n17.example.com
            host_switch_spec:
                host_switches:
                -   cpu_config: []
                    ecmp_mode: L3
                    host_switch_id: 50 01 cb 21 be 19 cb 38-78 bd 67 bc b3 0e 91 f1
                    host_switch_mode: LEGACY
                    host_switch_name: w1-vc1-c4-dvs
                    ...
                    uplinks:
                    -   uplink_name: uplink-2
                        vds_uplink_name: uplink2
                    -   uplink_name: uplink-1
                        vds_uplink_name: uplink1
                resource_type: StandardHostSwitchSpec
            id: w1-vc1-c4n17-6adb838f-669c-48ca-b76f-e1238984ef93host-2171
            is_overridden: false
            maintenance_mode: DISABLED
        ...
    

    问:“这些‘输出’数据对象的列表。”

    A:如果项目较多,例如:

    output:
      bar: a
      foo: b
      hs_spec:
        hs:
          - {em: L3, hm: S3}
          - {em: L4, hm: S4}
          - {em: L5, hm: S5}
        rt: c
      mm: DISABLED
    

    更新所有项目

    hs_update:
      hm: LEGACY
    hs: "{{ output.hs_spec.hs | product([hs_update])
                              | map('flatten')
                              | map('combine') }}"
    

    给出

    hs:
      - {em: L3, hm: LEGACY}
      - {em: L4, hm: LEGACY}
      - {em: L5, hm: LEGACY}
    

    然后,合并词典

    update:
      hs_spec:
        hs: "{{ hs }}"
    result: "{{ output | combine(update, recursive=True) }}"
    

    给出

    result:
      bar: a
      foo: b
      hs_spec:
        hs:
          - {em: L3, hm: LEGACY}
          - {em: L4, hm: LEGACY}
          - {em: L5, hm: LEGACY}
        rt: c
      mm: DISABLED
    
    • 3

相关问题

  • PostgreSQL:如何验证 JSON 字段?

  • jq 对特定键进行过滤并将值整理到单个 csv 单元格中

  • Elasticsearch:在 2 节点集群中创建副本时出错

  • 未进行任何更改的行上的 Sales Apex 错误消息

  • 如何使用XSLT 3.0关键功能

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve