# Create a list from a dictionary cycle

**URL:** <https://forum.ansible.com/t/create-a-list-from-a-dictionary-cycle/36833>\
**Category:** Ansible Project\
**Created:** [July 12, 2022, 9:18am UTC](https://forum.ansible.com/t/create-a-list-from-a-dictionary-cycle/36833 "2022-07-12T09:18:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Softmax23](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Softmax23](https://forum.ansible.com/u/Softmax23)\
**Post date:** [July 12, 2022, 9:18am UTC](https://forum.ansible.com/t/create-a-list-from-a-dictionary-cycle/36833/1 "2022-07-12T09:18:57Z")

</div>

Hi

I would like to create a list with this dict

{ “v3bf33-srv01”: “eu-west0-a”,

“v3bf33-srv02”: “eu-west0-a”,  
“v3bf33-srv03”: “eu-west0-a”,  
“v3bf33-srv04”: “eu-west0-b”,

“v3bf33-srv05”: “eu-west0-b”,

“v3bf33-srv06”: “eu-west0-b”,

“v3bf33-srv07”: “eu-west0-c”,  
“v3bf33-srv08”: “eu-west0-c”,

“v3bf33-srv09”: “eu-west0-c” }

if a user ask 5 servers ( in a pipeline), cycle through servers of each value:

eu-west0-a,

eu-west0-b,

eu-west0-c,

then again , eu-west0-a, eu-west0-b, eu-west0-c … until reach number of server need

The result should be a list containing only the server name.

E.g.:

- v3bf33-srv01 #eu-west0-a

- v3bf33-srv04 #eu-west0-b

- v3bf33-srv07 #eu-west0-c

- v3bf33-srv02 #eu-west0-a -  
v3bf33-srv05 #eu-west0-b

any help is welcome. ^^

---

<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:** [July 12, 2022, 9:59am UTC](https://forum.ansible.com/t/create-a-list-from-a-dictionary-cycle/36833/2 "2022-07-12T09:59:32Z")

</div>

For example, the playbook below

> cat pb.yml

- hosts: localhost  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;servers:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srv01: a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srv02: a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srv03: a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srv04: b  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srv05: b  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srv06: b  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srv07: c  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srv08: c  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;srv09: c  
&nbsp;&nbsp;&nbsp;&nbsp;arr: "{{ servers|dict2items|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groupby('value')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map(attribute=1)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('map', attribute='key')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;list\_of\_servers: "{{ tarr|flatten }}"  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tarr: "{{ tarr|d(arr.0)|zip(item)|map('flatten') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ arr[1:] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: list\_of\_servers[:5]|to\_yaml

1) Creates the matrix

&nbsp;&nbsp;arr:  
&nbsp;&nbsp;&nbsp;&nbsp;- [srv01, srv02, srv03]  
&nbsp;&nbsp;&nbsp;&nbsp;- [srv04, srv05, srv06]  
&nbsp;&nbsp;&nbsp;&nbsp;- [srv07, srv08, srv09]

2) Transpose the matrix

&nbsp;&nbsp;tarr:  
&nbsp;&nbsp;&nbsp;&nbsp;- [srv01, srv04, srv07]  
&nbsp;&nbsp;&nbsp;&nbsp;- [srv02, srv05, srv08]  
&nbsp;&nbsp;&nbsp;&nbsp;- [srv03, srv06, srv09]

3) Get the slice

&nbsp;&nbsp;list\_of\_servers[:5]: [srv01, srv04, srv07, srv02, srv05]

---

<div class="post-metadata">

**Author:** ![Softmax23](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Softmax23](https://forum.ansible.com/u/Softmax23)\
**Post date:** [July 12, 2022, 12:10pm UTC](https://forum.ansible.com/t/create-a-list-from-a-dictionary-cycle/36833/3 "2022-07-12T12:10:16Z")

</div>

it s not working for me :

fatal: [localhost]: FAILED! =\>  
{“msg”: "Invalid data passed to ‘loop’, it requires a list, got this instead: \<generator object do\_map at 0x7fe97cca3900\>,

\<generator object do\_map at 0x7fe97cca39e0\>, \<generator object do\_map at 0x7fe97cca3a50\>].

Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup."}

a co worker try too … .same result. i think i will go in BASH… i am sure it will work . ahahah

---

<div class="post-metadata">

**Author:** ![Softmax23](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Softmax23](https://forum.ansible.com/u/Softmax23)\
**Post date:** [July 12, 2022, 1:24pm UTC](https://forum.ansible.com/t/create-a-list-from-a-dictionary-cycle/36833/4 "2022-07-12T13:24:01Z")

</div>

When i do

arr: “{{ servers|dict2items|  
groupby(‘value’)|  
map(attribute=1)|  
map(‘map’, attribute=‘key’)|  
list }}”  
list\_of\_servers: “{{ tarr|flatten }}”  
tasks:

- debug:  
var: arr

the result , no data.

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“arr”: “[\<generator object do\_map at 0x7f1c51cac4a0\>, \<generator object do\_map at 0x7f1c51cac9e0\>,  
\<generator object do\_map at 0x7f1c51caca50\>]”  
}

---

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [July 12, 2022, 1:29pm UTC](https://forum.ansible.com/t/create-a-list-from-a-dictionary-cycle/36833/5 "2022-07-12T13:29:50Z")

</div>

I think there’s an typo here. There’s an extra ‘t’ in list\_of\_servers: “{{ tarr|flatten }}” should be  
list\_of\_servers: “{{ arr|flatten }}”

---

<div class="post-metadata">

**Author:** ![Softmax23](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Softmax23](https://forum.ansible.com/u/Softmax23)\
**Post date:** [July 12, 2022, 1:59pm UTC](https://forum.ansible.com/t/create-a-list-from-a-dictionary-cycle/36833/6 "2022-07-12T13:59:14Z")

</div>

thx for looking, but the error come from the task débug:

tasks:

- debug:  
var: arr

not tarr .
