Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append JSON array with specific data

Tags:

json

ansible

I am trying to run :

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

below is the data generated.

    {
        "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
            }
        ]
    }

Now I want to append the accountIds above with any new account say 1234567 which should give me result such as

{
    "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
        }
    ]
}

I am trying to do is:

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

but this doesn't work, please let me know the right syntax.

like image 584
AniK Avatar asked Oct 14 '25 07:10

AniK


1 Answers

Basically you require bit of JSON manipulation to achieve your task.

Steps :

  1. Store output of first command in some json file. In your case you can keep that as registered variable of ansible.

  2. Get existing account_ids in some variable.

  3. Create a list of new accounts as variables in ansible.

  4. Iterate over new account_ids and add to existing account_ids.

  5. Update the aws config command.

Sample Code :

- 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"]}]\""'

Sample Output :

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
like image 183
mahbuhs_redoc Avatar answered Oct 18 '25 04:10

mahbuhs_redoc