# Remove a dict in a nested dict

**URL:** <https://forum.ansible.com/t/remove-a-dict-in-a-nested-dict/38728>\
**Category:** Ansible Project\
**Created:** [March 25, 2024, 4:40pm UTC](https://forum.ansible.com/t/remove-a-dict-in-a-nested-dict/38728 "2024-03-25T16:40:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Baptiste\_Doublet](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@Baptiste\_Doublet](https://forum.ansible.com/u/Baptiste_Doublet)\
**Post date:** [March 25, 2024, 4:40pm UTC](https://forum.ansible.com/t/remove-a-dict-in-a-nested-dict/38728/1 "2024-03-25T16:40:00Z")

</div>

Hi,  
I block on manipuling data with ansible / jinja2 / python filter.

I have this yaml structure which is an ansible inventory in yaml

```auto
all:
children:
admin_administration:
hosts:
admin_administration1:
server_ip: 127.0.0.1
int:
children:
int_administration:
int_console:
int_swarmanager:
int_swarmworker:
children:
int_swarmvip:
int_swarminfra:
int_swarmload:
int_scw_swarmload:
int_swarmvip:
hosts:
int_swarmvip1:
server_hostname: swarmvip-001
server_ip: 127.0.0.1

```

and I have this list of dict:

```auto

remove_groups:
- name:
int_swarmworker:
children:
int_swarmvip:

```

I loop on it so the task gets group.name dict on each iteration

```auto
- name: Remove group from inventory file
include_tasks: remove_group.yml
loop: "{{ remove_groups }}"
loop_control:
loop_var: group

```

In the section:

```auto
int_swarmworker:
children:
int_swarmvip:
int_swarminfra:
int_swarmload:
int_scw_swarmload:

```

Only the int\_swarmvip must be removed but if there is no more subobject after removing int\_swarmvip below int\_swarmworker, the group int\_swarmworker must be removed too.

Also this section must be removed:

```auto
int_swarmvip:
hosts:
int_swarmvip1:
server_hostname: swarmvip-001
server_ip: 127.0.0.1

```

So the new yaml ansible inventory should be:

```auto
all:
children:
admin_administration:
hosts:
admin_administration1:
server_ip: 127.0.0.1
int:
children:
int_administration:
int_console:
int_swarmanager:
int_swarmworker:
children:
int_swarminfra:
int_swarmload:
int_scw_swarmload:

```

I did the add\_host with ansible dict combine and I try it to remove a dict → fail  
Also try a custom plugin fillter → fail  
jinja2 code → fail  
I debug too much but I’m sure the solution is easy …

Thanks for your help
