Thank you, Vladimir. It works. Is there any trick to make this with one-liner?
On Tuesday, June 14, 2022 at 6:54:04 PM UTC+3 [email protected] wrote: > On Tue, 14 Jun 2022 08:03:22 -0700 (PDT) > "[email protected]" <[email protected]> wrote: > > > Hello, > > I've got a list variable that looks like this > > list: > > - name: n1 > > group: g1 > > - name: n2 > > group: g1 > > - name: c1 > > group: g2 > > etc.... > > > > > > in my config file, this should become: > > g1: n1,n2 > > g2: c1 > > > > etc.... > > > > how can this be done? > > > > * Given the list > > _list: > - {group: g1, name: n1} > - {group: g1, name: n2} > - {group: g2, name: c1} > > * Use *groupby* > > _arr: "{{ _list|groupby('group') }}" > > gives > > _arr: > - - g1 > - - {group: g1, name: n1} > - {group: g1, name: n2} > - - g2 > - - {group: g2, name: c1} > > * Create lists of keys and values > > _keys: "{{ _arr|map('first')|list }}" > _vals: "{{ _arr|map('last')|map('map', attribute='name')|list }}" > > gives > > _keys: > [g1, g2] > _vals: > - [n1, n2] > - [c1] > > * Create the dictionary > > _dict: "{{ dict(_keys|zip(_vals)) }}" > > gives > > _dict: > g1: [n1, n2] > g2: [c1] > > > -- > Vladimir Botka > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/121e8417-e8de-4bbc-a2ac-6cf01e35a2a6n%40googlegroups.com.
