# appending to nested dictionary

**URL:** <https://forum.ansible.com/t/appending-to-nested-dictionary/38608>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [November 30, 2023, 1:34pm UTC](https://forum.ansible.com/t/appending-to-nested-dictionary/38608 "2023-11-30T13:34:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![darmar](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@darmar](https://forum.ansible.com/u/darmar)\
**Post date:** [November 30, 2023, 1:34pm UTC](https://forum.ansible.com/t/appending-to-nested-dictionary/38608/1 "2023-11-30T13:34:00Z")

</div>

I am wondering how can i append the dictionary with subnet\_id as key into vpc\_result subnets element to look like below. Please find below all the tasks, vars and run output.

ok: [localhost] =\> {  
“vpc\_result”: [  
{  
“cidr\_block”: “10.10.8.0/21”,  
“igw\_id”: “igw-0e06eba08c68ebf2b”,  
“name”: “A”,  
“region”: “eu-central-1”,  
“subnets”: {  
“10.10.8.0/24”: {  
“az”: “a”,  
“map\_public”: true,  
“subnet\_id”: “subnet-08b6790e6e51b6b78” ←   
},  
“10.10.9.0/24”: {  
“az”: “b”,  
“subnet\_id”: “subnet-0e8512e1ace7ddc1f” ←   
}  
},  
“vpc\_id”: “vpc-000d7431c4a4f6e83”  
}  
]  
}

# roles/aws/defaults/main.yml

project: killerapp  
customer: loser  
stage: dev

resource\_tags:  
Stage: “{{ stage }}”  
Application: “{{ project }}”  
Created-By: “Ansible for {{ customer }}”

vpc:

- name: “A”  
cidr\_block: “10.10.8.0/21”  
region: “eu-central-1”  
subnets:  
“10.10.8.0/24”:  
az: “a”  
map\_public: true  
“10.10.9.0/24”:  
az: “b”
- name: “B”  
cidr\_block: “10.11.8.0/21”  
region: “eu-west-3”  
subnets:  
“10.11.8.0/24”:  
az: “a”  
map\_public: true  
“10.11.9.0/24”:  
az: “b”

# roles/aws/tasks/main.yml

- name: Create VPC for each region  
loop: “{{ vpc }}”  
include\_tasks: vpc.yml  
vars:  
region: “{{ item.region }}”  
name: “{{ item.name }}”  
cidr\_block: “{{ item.cidr\_block }}”  
subnets: “{{ item.subnets }}”

- name: “Report of generated networking”  
debug:  
var: vpc\_result

# roles/aws/tasks/vpc.yml

- name: Create virtual private network for VPC {{ name }}  
ec2\_vpc\_net:  
region: “{{ region }}”  
name: “{{ name }}”  
cidr\_block: “{{ cidr\_block }}”  
tags: “{{ resource\_tags }}”  
register: created\_vpc

- name: Create internet gateway for VPC {{ name }} in {{ region }}  
ec2\_vpc\_igw:  
vpc\_id: “{{ created\_vpc.vpc.id }}”  
region: “{{ region }}”  
tags: “{{ resource\_tags | combine({‘Name’: ‘VPC internet gateway’}) }}”  
register: created\_igw

- set\_fact:  
vpc\_result: “{{ vpc\_result | default() + [{  
‘name’: created\_vpc.vpc.name,  
‘igw\_id’: created\_igw.gateway\_id,  
‘vpc\_id’: created\_vpc.vpc.id,  
‘region’: region,  
‘subnets’: subnets,  
‘cidr\_block’: cidr\_block  
}] }}”

- name: Create subnets in VPC {{ name }} in {{ region }}  
loop: “{{ subnets | list }}”  
loop\_control:  
loop\_var: subnet  
include\_tasks: vpc\_subnet.yml

# roles/aws/tasks/vpc\_subnet.yml

- name: Create subnet {{ subnet }} for {{ name }} VPC in region {{ region }}  
ec2\_vpc\_subnet:  
vpc\_id: “{{ created\_vpc.vpc.id }}”  
region: “{{ region }}”  
cidr: “{{ subnet }}”  
tags: “{{ resource\_tags | combine({‘Name’: subnet, ‘Type’: subnets[subnet].map\_public|default(false) | ternary(‘public’, ‘private’)}) }}”  
az: “{{ region }}{{ subnets[subnet].az }}”  
map\_public: “{{ subnets[subnet].map\_public | default(false) }}”  
register: created\_subnet  
retries: 3  
delay: 5

- debug:  
var: created\_subnet.subnet.id

# output

❯ ansible-playbook aws.yml

PLAY [localhost]

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 30, 2023, 8:30pm UTC](https://forum.ansible.com/t/appending-to-nested-dictionary/38608/2 "2023-11-30T20:30:04Z")

</div>

This is untested, but you would end up doing effectively the same as this:

```auto
set_fact:
  vpc_result: '{{ [vpc_result[0] | combine({"subnets":
                                             {"10.10.8.0/24":
                                               {"subnet_id": "subnet-08b6790e6e51b6b78"}
                                             },
                                             {"10.10.9.0/24":
                                               {"subnet_id": "subnet-0e8512e1ace7ddc1f"}
                                             }
                                            })] }}'

```

Here’s a [link to the ansible.builtin.combine filter](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/combine_filter.html#ansible-builtin-combine-filter-combine-two-dictionaries) docs. Note that combine works on dictionaries, and vpc\_result is a single element list. That’s why the whole expression is enclosed in and we combine on vpc\_result[0] instead of vpc\_result.
