Create a list from a dictionary cycle

163 views
Skip to first unread message

Softmax23

unread,
Jul 12, 2022, 5:18:57 AM7/12/22
to Ansible Project

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. ^^

Vladimir Botka

unread,
Jul 12, 2022, 5:59:52 AM7/12/22
to Softmax23, ansible...@googlegroups.com
On Tue, 12 Jul 2022 02:18:57 -0700 (PDT)
Softmax23 <maximef...@gmail.com> wrote:

For example, the playbook below

shell> cat pb.yml
- hosts: localhost
vars:
servers:
srv01: a
srv02: a
srv03: a
srv04: b
srv05: b
srv06: b
srv07: c
srv08: c
srv09: c
arr: "{{ servers|dict2items|
groupby('value')|
map(attribute=1)|
map('map', attribute='key')|
list }}"
list_of_servers: "{{ tarr|flatten }}"
tasks:
- set_fact:
tarr: "{{ tarr|d(arr.0)|zip(item)|map('flatten') }}"
loop: "{{ arr[1:] }}"
- debug:
var: list_of_servers[:5]|to_yaml

1) Creates the matrix

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

2) Transpose the matrix

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

3) Get the slice

list_of_servers[:5]: [srv01, srv04, srv07, srv02, srv05]

--
Vladimir Botka
Message has been deleted
Message has been deleted
Message has been deleted

Softmax23

unread,
Jul 12, 2022, 8:10:16 AM7/12/22
to Ansible Project
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

Softmax23

unread,
Jul 12, 2022, 9:24:01 AM7/12/22
to Ansible Project
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>]"
}



On Tuesday, July 12, 2022 at 11:59:52 AM UTC+2 vbo...@gmail.com wrote:

Jorge Rúa

unread,
Jul 12, 2022, 9:30:16 AM7/12/22
to ansible...@googlegroups.com
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 }}"

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a38eebfa-d815-45fb-a2f3-0c57529bac66n%40googlegroups.com.

Softmax23

unread,
Jul 12, 2022, 9:59:15 AM7/12/22
to Ansible Project
thx for looking, but the error come from the task débug:
tasks:
    - debug:
        var: arr

not tarr .
Reply all
Reply to author
Forward
0 new messages