如何追加特定数据到JSON数组

3

我正在尝试运行:

- name: Describe config aggregator
  shell: >
    aws configservice describe-configuration-aggregators --configuration-aggregator-name test-config
  register: config_ouput
    

以下是生成的数据。
    {
        "ConfigurationAggregators": [
            {
                "ConfigurationAggregatorName": "test-config",
                "ConfigurationAggregatorArn": "arn:aws:config:us-east-1:4567:config-aggregator/config-aggregator-uw2o9pzf",
                "AccountAggregationSources": [
                    {
                        "AccountIds": [
                            "895677"
                        ],
                        "AllAwsRegions": true
                    }
                ],
                "CreationTime": 1624454176.124,
                "LastUpdatedTime": 1626426755.504
            }
        ]
    }

现在我想要将上面的账户ID与任何新的账户(例如1234567)连接起来,得到以下结果:

{
    "ConfigurationAggregators": [
        {
            "ConfigurationAggregatorName": "test-config",
            "ConfigurationAggregatorArn": "arn:aws:config:us-east-1:8778:config-aggregator/test-config-pzf",
            "AccountAggregationSources": [
                {
                    "AccountIds": [
                        "895677,1234567"
                    ],
                    "AllAwsRegions": true
                }
            ],
            "CreationTime": 1624454176.124,
            "LastUpdatedTime": 1626426755.504
        }
    ]
}

我想要做的是:

- name: Export results to JSON
  set_fact:
    config_ouput_json: "{{ config_ouput + [{"AccountIds": "1234567","AllAwsRegions": true}]}}"

但这个不起作用,请告诉我正确的语法。

1个回答

2
基本上,您需要进行一些JSON操作来完成您的任务。
步骤:
1. 将第一个命令的输出存储在某个json文件中。在您的情况下,您可以将其保留为ansible的注册变量。 2. 获取现有的account_ids并存储在某个变量中。 3. 在ansible中创建一个新帐户的列表作为变量。 4. 遍历新的account_ids并添加到现有的account_ids中。 5. 更新aws config命令。
示例代码:
- name: initial validation
  hosts: localhost
  connection: local
  vars:
    newAccountIds:
      - "123456"
      - "566544"
      - "555445"

  tasks:
  - name: register json file
    include_vars:
      file: 'abc.json'
      name: bundle

  - name: set value
    set_fact:
      values: "{{ bundle['ConfigurationAggregators'][0]['AccountAggregationSources'][0]['AccountIds'] }}"

  - set_fact:
      values: "{{ (values | default([])) + [item] }}"
    with_items: "{{ newAccountIds }}"

  - debug:
      msg: "{{ values }}"

  - debug:
      msg: '"aws configservice put-configuration-aggregator --configuration-aggregator-name test-config --account-aggregation-sources "[{"AccountIds": {{ values | to_json }},"AwsRegions": ["us-east-1"]}]\""'

示例输出:

PLAY [initial validation] ********************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [localhost]

TASK [register json file] ********************************************************************************************
ok: [localhost]

TASK [set value] *****************************************************************************************************
ok: [localhost]

TASK [set_fact] ******************************************************************************************************
ok: [localhost] => (item=123456)
ok: [localhost] => (item=566544)
ok: [localhost] => (item=555445)

TASK [debug] *********************************************************************************************************
ok: [localhost] => {
    "msg": [
        "895677",
        "123456",
        "566544",
        "555445"
    ]
}

TASK [debug] *********************************************************************************************************
ok: [localhost] => {
"msg": "\"aws configservice put-configuration-aggregator --configuration-aggregator-name test-config --account-aggregation-sources \"[{\"AccountIds\": [\"895677\", \"123456\", \"566544\", \"555445\"],\"AwsRegions\": [\"us-east-1\"]}]\\\"\""}

PLAY RECAP ***********************************************************************************************************
localhost                  : ok=6    changed=0    unreachable=0    failed=0

你好Shubh,在调试时一切都很好,但是当我将其实施到服务器上时,它会抛出错误。 有什么想法吗?当前的语法是什么?它说找不到命令。
  • name: 配置配置聚合器 shell: > 'aws configservice put-configuration-aggregator --configuration-aggregator-name test-config --account-aggregation-sources "[{"AccountIds": {{ values | to_json }},"AllAwsRegions": true}]"'
- AniK
/bin/sh: aws configservice put-configuration-aggregator --configuration-aggregator-name RDS-details-test --account-aggregation-sources "[{"AccountIds": ["895677", "123456", "555445"],"AllAwsRegions": true}]": 命令未找到 - AniK
由于缺少“\”,我感觉它会引发错误。正确的语法应该是: aws configservice put-configuration-aggregator --configuration-aggregator-name MyAggregator --account-aggregation-sources "[{"AccountIds": ["AccountID1","AccountID2","AccountID3"],"AllAwsRegions": true}]" - AniK
1
这个能帮到你吗?https://dev59.com/jVkS5IYBdhLWcg3wp4Qi - SRJ
这对我不起作用,我甚至在想如果我们只在聚合器源中附加AccountIDs,然后将文件保存为json格式并在远程上使用它进行部署,会怎样。我甚至在https://stackoverflow.com/questions/68812442/trying-to-append-a-json/68813287?noredirect=1#comment121617338_68813287 上提出了问题,你有什么线索吗?我不能使用ansible 2.10。 - AniK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接