# How to iterate over nested list and split it into two groups using add\_host?

**URL:** <https://forum.ansible.com/t/how-to-iterate-over-nested-list-and-split-it-into-two-groups-using-add-host/31227>\
**Category:** Ansible Project\
**Created:** [September 9, 2019, 7:05am UTC](https://forum.ansible.com/t/how-to-iterate-over-nested-list-and-split-it-into-two-groups-using-add-host/31227 "2019-09-09T07:05:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![autogun](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@autogun](https://forum.ansible.com/u/autogun)\
**Post date:** [September 9, 2019, 7:05am UTC](https://forum.ansible.com/t/how-to-iterate-over-nested-list-and-split-it-into-two-groups-using-add-host/31227/1 "2019-09-09T07:05:26Z")

</div>

Hey,

Given the following playbook -

- hosts: localhost  
gather\_facts: false  
vars:  
my\_hosts:

- [‘host1’, ‘host2’]

- [‘host3’]

- name: Add host  
add\_host:  
name: ‘{{ item }}’  
groups: “group{{ index }}”  
loop: “{{ my\_hosts }}”  
loop\_control:  
index\_var: index

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 9, 2019, 7:25am UTC](https://forum.ansible.com/t/how-to-iterate-over-nested-list-and-split-it-into-two-groups-using-add-host/31227/2 "2019-09-09T07:25:43Z")

</div>

Flatten the lists.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ my\_hosts|flatten }}"

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![autogun](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@autogun](https://forum.ansible.com/u/autogun)\
**Post date:** [September 9, 2019, 7:54am UTC](https://forum.ansible.com/t/how-to-iterate-over-nested-list-and-split-it-into-two-groups-using-add-host/31227/3 "2019-09-09T07:54:32Z")

</div>

Thanks for the hint,

Flatting the list results with

`

TASK [print] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: {  
“all”: [  
“host1”,  
“host2”,  
“host3”  
],  
“group0”: [  
“host1”  
],  
“group1”: [  
“host2”  
],  
“group2”: [  
“host3”  
],  
“ungrouped”:   
}  
}

`

What i’m trying to achieve is -

`

TASK [print] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: {  
“all”: [  
“host1”,  
“host2”,  
“host3”  
],  
“group0”: [  
“host1”,  
“host2”  
],  
“group1”: [  
“host3”  
],  
“ungrouped”:   
}  
}

`

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 9, 2019, 10:52am UTC](https://forum.ansible.com/t/how-to-iterate-over-nested-list-and-split-it-into-two-groups-using-add-host/31227/4 "2019-09-09T10:52:37Z")

</div>

You've said "I want this to be dynamic as much as possible". Where does this  
"dynamic" assignment of the hosts to the groups come from? Any structure? If  
not it might be hard-coded i.e not dynamic at all. Make your choice.

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 9, 2019, 12:42pm UTC](https://forum.ansible.com/t/how-to-iterate-over-nested-list-and-split-it-into-two-groups-using-add-host/31227/5 "2019-09-09T12:42:52Z")

</div>

If the hosts from 1st element of the list come to group0, from 2nd to  
group1 and so on ... The play below

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;my\_hosts:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ['host1', 'host2']  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- ['host3']  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- include\_tasks: loop.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: '{{ my\_hosts }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_control:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_var: outer\_item  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;index\_var: outer\_idx  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: groups

with this included task below gives the groups.

&nbsp;&nbsp;$ cat loop.yml  
&nbsp;&nbsp;- add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: "{{ 'group' ~ outer\_idx }}"  
&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ outer\_item }}"

Cheers,

&nbsp;&nbsp;-vlado
