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

Ansible中如何正确向嵌套数组添加新键值对?

问题描述

我有如下结构的数组:

a1:
  - channelId: test1
  - channelId: test2

想要添加新键值对后得到:

a2:
  - channelId: test1
  - channelId: test2
  - channelId: test3

以下是我尝试的Ansible Playbook代码片段:

- name: Setting Channel id
  set_fact:
    channelarray: "{{ newrelic_alertpolicyresult | json_query('json.data.actor.account.aiWorkflows.workflows.entities[*].destinationConfigurations[*]')}}"
    workflowid: "{{ newrelic_alertpolicyresult | json_query('json.data.actor.account.aiWorkflows.workflows.entities[*].id') }}"
    
- name: PrintChannel ID
  debug:
    msg: "Channel ID - {{ channelarray }}  &  Workflow ID - {{ workflowid }}"
    
- name: PrintChannel ID in loop
  debug:
    msg: "{{ item }}"
  loop: "{{ channelarray }}"

- name: Convert JSON to dictionary
  set_fact: 
    updated_channel_array: "{{ channelarray + [{ 'channelId': 'test3' }] | json_query('[*]')  }}"

  
- name: Print updated channel array
  debug:
     msg: "{{ updated_channel_array }}"

执行后的输出:

TASK [Setting Channel id] 
ok: [localhost]

TASK [PrintChannel ID] 
ok: [localhost] => {
    "msg": "Channel ID - [[{'channelId': 'f35ead0f-8252-4975-9055-5d050161da01'}, {'channelId': 'be33c326-08d4-494c-b544-6c86beb0dd5e'}]]  &  Workflow ID - ['d7d1db30-918e-4202-a7ba-1613cf9600a1']"
}

TASK [PrintChannel ID in loop] 
ok: [localhost] => (item=[{'channelId': 'f35ead0f-8252-4975-9055-5d050161da01'}, {'channelId': 'be33c326-08d4-494c-b544-6c86beb0dd5e'}]) => {
    "msg": [
        {
            "channelId": "f35ead0f-8252-4975-9055-5d050161da01"
        },
        {
            "channelId": "be33c326-08d4-494c-b544-6c86beb0dd5e"
        }
    ]
}

TASK [Convert JSON to dictionary] *********************************************************************************************************************************************************************************
ok: [localhost]

ok: [localhost] => {
    "msg": [
        [
            {
                "channelId": "f35ead0f-8252-4975-9055-5d050161da01"
            },
            {
                "channelId": "be33c326-08d4-494c-b544-6c86beb0dd5e"
            }
        ],
        {
            "channelId": "test3"
        }
    ]
}

当前问题:新的键值对被添加到了外层数组,而我期望将其添加到destinationConfigurations数组中。


解决方案

从输出能看到,channelarray是二维数组(外层数组嵌套了存放channel的目标数组),直接用+拼接会把新元素加到外层数组里。要解决这个问题,需要先定位到内层的目标数组,再添加新元素。

单元素场景处理

如果channelarray里只有一组目标数组,直接取出内层数组拼接即可:

- name: Convert JSON to dictionary
  set_fact: 
    updated_channel_array: "{{ channelarray[0] + [{ 'channelId': 'test3' }] }}"

多元素场景处理

如果channelarray里有多个workflow的配置数组,需要遍历逐个更新:

- name: Update each destination configuration array
  set_fact:
    updated_channel_array: "{{ updated_channel_array | default([]) + [item + [{ 'channelId': 'test3' }]] }}"
  loop: "{{ channelarray }}"

这样处理后,新的channelId就会被添加到对应的内层数组中,符合预期结构。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 05:16:02