试图附加一个json [英] Trying to append a json

查看:22
本文介绍了试图附加一个json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当添加新帐户时,应按以下结构添加

- name:描述配置聚合器外壳:>aws configservice 描述配置聚合器 --configuration-聚合器名称测试配置注册:config_output- 调试:味精={{ config_output.stdout |from_json }}"

输出:

 ConfigurationAggregators":[{ConfigurationAggregatorArn":arn:aws:config:us-east-1:4424:config-aggregator/config-aggregator-uw",创建时间":1624454176.124,ConfigurationAggregatorName":test-config",AccountAggregationSources":[{AllAwsRegions":真,帐户 ID":[80570",17622"]}],上次更新时间":1629115098.532}]

我只在变量中存储了首选数据:

- name:将结果导出为 JSON设置事实:包:"{{ config_output.stdout |from_json }}"- 名称:设置值设置事实:值:{{ bundle['ConfigurationAggregators'][0]['AccountAggregationSources'] }}";

输出:

<预><代码>[{帐户 ID":[80570",17622"],AllAwsRegions":真}]

我想在 AccountIds 中添加78956",为此我使用以下语法.

- 调试:msg: "{{ 值 |combine(to_append, recursive=True, list_merge='append') }}";

但这给了我错误msg":'recursive'是唯一有效的关键字参数"

我已经将 to_append 存储为 var ,就像下面评论中提到的那样.

解决方案

使用 ansible >= 2.10,您可以定义一个与原始元素具有相同结构的元素,并使用缺失的列表元素并使用 combine filter 与其recursivelist_merge 选项.

注意:这有点快,如果您的输入结构发生变化(例如顶级列表中的几个元素......),可能会产生一些副作用.为了获得更好/更准确的答案,您应该描述您的真实用例以及您计划如何使用这种转换后的结构(循环、模板、写入文件...).

以下示例剧本:

---- 主机:本地主机收集事实:假变量:# 你的示例变量在一行上以提高可读性变量:[{字符串":[abc",xyz"],输入":true}]# 这是我们将附加到上述结构的内容to_append:- 字符串:- efg任务:- 调试:msg: "{{ 变量 |combine(to_append, recursive=true, list_merge='append') }}";

给出:

PLAY [localhost] ******************************************************************************************************************************************************************************************************************任务 [调试] ********************************************************************************************************************************************************************************************************************好的:[本地主机] =>{味精":{输入":真,字符串":[abc",xyz",鸡蛋"]}}播放回顾 ************************************************************************************************************************************************************************************************************************本地主机:ok=1已更改=0无法访问=0失败=0已跳过=0已获救=0已忽略=0

When a new account is added, it should be added in the below structure

- name: Describe config aggregator
  shell: >
    aws configservice describe-configuration-aggregators --configuration-aggregator-name test-config
   register: config_output
- debug: msg="{{ config_output.stdout | from_json }}"

output:

        "ConfigurationAggregators": [
            {
                "ConfigurationAggregatorArn": "arn:aws:config:us-east-1:4424:config-aggregator/config-aggregator-uw",
                "CreationTime": 1624454176.124,
                "ConfigurationAggregatorName": "test-config",
                "AccountAggregationSources": [
                    {
                        "AllAwsRegions": true,
                        "AccountIds": [
                            "80570",
                            "17622"
                        ]
                    }
                ],
                "LastUpdatedTime": 1629115098.532
            }
        ]

I have stored only preferred data in a variable:

- name: Export results to JSON
  set_fact:
    bundle: "{{ config_output.stdout | from_json }}"
- name: set value
  set_fact:
    values: "{{ bundle['ConfigurationAggregators'][0]['AccountAggregationSources'] }}"

Output:

[
    {
        "AccountIds": [
            "80570",
            "17622"
        ],
        "AllAwsRegions": true
    }
]

I want to add '78956' in AccountIds, for this I am using below syntax.

- debug:
    msg: "{{ values | combine(to_append, recursive=True, list_merge='append') }}"

but this give me error "msg": "'recursive' is the only valid keyword argument"

I have stored the to_append as a var already just like mentioned in below comments.

解决方案

Using ansible >= 2.10, you can define an element having the same structure as your original one with the missing list element and use the combine filter with its recursive and list_merge options.

Note: this is a bit quickNdirty and might have some side effects if your input structure changes (e.g. several element in the top level list....). For a better / more accurate answer, you should describe your real use case and how you plan to use this transformed structure (loop, template, write to file...).

The following example playbook:

---
- hosts: localhost
  gather_facts: false

  vars:
    # Your example var on a single line for legibility
    Variables: [{"Strings":["abc","xyz"],"Inputs":true}]

    # This what we will append to the above structure
    to_append:
      - Strings:
          - efg

  tasks:
    - debug:
        msg: "{{ Variables | combine(to_append, recursive=true, list_merge='append') }}"

Gives:

PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": {
        "Inputs": true,
        "Strings": [
            "abc",
            "xyz",
            "efg"
        ]
    }
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

这篇关于试图附加一个json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆