AniK
AniK

Reputation: 65

Trying to append a json

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.

Upvotes: 0

Views: 505

Answers (1)

Zeitounator
Zeitounator

Reputation: 44809

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

Upvotes: 1

Related Questions