4

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.

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Shubh, Till debug it was ok, but when i putting this to implement on the server, it throws me error. any idea what will be the current syntax then? it says command not found. - name: Configure config aggregator shell: > 'aws configservice put-configuration-aggregator --configuration-aggregator-name test-config --account-aggregation-sources "[{"AccountIds": {{ values | to_json }},"AllAwsRegions": true}]"'
/bin/sh: aws configservice put-configuration-aggregator --configuration-aggregator-name RDS-details-test --account-aggregation-sources "[{"AccountIds": ["895677", "123456", "555445"],"AllAwsRegions": true}]": command not found
correct syntax should be aws configservice put-configuration-aggregator --configuration-aggregator-name MyAggregator --account-aggregation-sources "[{\"AccountIds\": [\"AccountID1\",\"AccountID2\",\"AccountID3\"],\"AllAwsRegions\": true}]" I feel cz of "\" missing it is throwing this error.
this doesn't works for me, i was even thinking what if we append only AccountIDs in aggregatorsource and then save the file as json on remote, use it for deploying it then. I have even asked the question on stackoverflow.com/questions/68812442/trying-to-append-a-json/… you have any clue to this? I cannot use ansible 2.10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.