Re: [ansible-project] List of dicts => dict of lists

2019-11-22 Thread Srinivasa Jasti
Here is another way.  Although I like vladimir Botka's solution.

---

- hosts: localhost

  gather_facts: no



  vars:

instances:

  - id: 1234

type: web

  - id: 1456

type: web

  - id: 1678

type: web

  - id: 3456

type: proxy

  - id: 2567

type: proxy

  - id: 2566

type: db

  - id: 4900

type: db


  tasks:

  - name: Set Fact for proxy

set_fact:

  new_proxy: "{{ new_proxy|default([]) + [ { 'id': item.id } ] }}"

loop: "{{ instances }}"

when: item.type == 'proxy'


  - name: set Fact for db

set_fact:

  new_db: "{{ new_proxy|default([]) + [ {'id': item.id } ] }}"

loop: "{{ instances }}"

when: item.type == 'db'


  - name: set Fact for web

set_fact:

  new_web: "{{ new_proxy|default([]) + [ {'id': item.id } ] }}"

loop: "{{ instances }}"

when: item.type == 'web'


  - name: Print facts

debug:

  msg: "Web - {{ new_web }} - DB - {{ new_db }} - Proxy - {{ new_proxy
}}"



  - name: Final set fact

set_fact:

  new_instances: "{{ new_instances|default([]) + [ {'db': new_db},
{'web': new_web}, {'proxy': new_proxy} ] }}"


  - name: Print final fact

debug:

  var: new_instances

On Fri, Nov 22, 2019 at 1:12 PM Vladimir Botka  wrote:

> On Fri, 22 Nov 2019 18:43:31 +0100
> Dick Visser  wrote:
>
> > instances:
> >   - id: i-0fe48c061f6a323a0
> > type: web
> >   - id: i-0403e39813defda31
> > type: web
> >   - id: i-0442f13b528e5e1c6
> > type: proxy
> >   - id: i-0ebea75d008becaa6
> > type: proxy
> >   - id: i-067c5d985c194e036
> > type: db
> >   - id: i-0679e87f06d0cf9a2
> > type: db
> >
> > Which I'd like to turn into this structure:
> >
> > instances:
> >   web:
> > - id: i-0fe48c061f6a323a0
> > - id: i-0403e39813defda31
> >   proxy:
> > - id: i-0442f13b528e5e1c6
> > - id: i-0ebea75d008becaa6
> >   db:
> > - id: i-067c5d985c194e036
> > - id: i-0679e87f06d0cf9a2
>
> The task below does the job
>
> - set_fact:
> instances2: "{{ instances2|
> default({})|
> combine({item.0: item.1|
>  json_query('[].{id: id}')}) }}"
>   loop: "{{ instances|groupby('type') }}"
>
> Cheers,
>
> -vlado
>
> --
> 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-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/20191122201219.5d637e3c%40gmail.com
> .
>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAB2Y44aogCGKcO-iNxmj7wuBujKd13JHUY1bAW7Jf6MGFZndGw%40mail.gmail.com.


Re: [ansible-project] List of dicts => dict of lists

2019-11-22 Thread Vladimir Botka
On Fri, 22 Nov 2019 18:43:31 +0100
Dick Visser  wrote:

> instances:
>   - id: i-0fe48c061f6a323a0
> type: web
>   - id: i-0403e39813defda31
> type: web
>   - id: i-0442f13b528e5e1c6
> type: proxy
>   - id: i-0ebea75d008becaa6
> type: proxy
>   - id: i-067c5d985c194e036
> type: db
>   - id: i-0679e87f06d0cf9a2
> type: db
> 
> Which I'd like to turn into this structure:
> 
> instances:
>   web:
> - id: i-0fe48c061f6a323a0
> - id: i-0403e39813defda31
>   proxy:
> - id: i-0442f13b528e5e1c6
> - id: i-0ebea75d008becaa6
>   db:
> - id: i-067c5d985c194e036
> - id: i-0679e87f06d0cf9a2

The task below does the job

- set_fact:
instances2: "{{ instances2|
default({})|
combine({item.0: item.1|
 json_query('[].{id: id}')}) }}"
  loop: "{{ instances|groupby('type') }}"

Cheers,

-vlado

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20191122201219.5d637e3c%40gmail.com.


pgplpewgiwX8e.pgp
Description: OpenPGP digital signature


Re: [ansible-project] List of dicts => dict of lists

2019-11-22 Thread Kai Stian Olstad

On 22.11.2019 18:43, Dick Visser wrote:

I'm struggling with something seemingly simple.
I have this list:

instances:
  - id: i-0fe48c061f6a323a0
type: web
  - id: i-0403e39813defda31
type: web
  - id: i-0442f13b528e5e1c6
type: proxy
  - id: i-0ebea75d008becaa6
type: proxy
  - id: i-067c5d985c194e036
type: db
  - id: i-0679e87f06d0cf9a2
type: db


Which I'd like to turn into this structure:


instances:
  web:
- id: i-0fe48c061f6a323a0
- id: i-0403e39813defda31
  proxy:
- id: i-0442f13b528e5e1c6
- id: i-0ebea75d008becaa6
  db:
- id: i-067c5d985c194e036
- id: i-0679e87f06d0cf9a2


But I can't figure out how to do this...


Are you sure you need the id in the list as it's redundant and without 
it is very simple with selectattr and map.


--
Kai Stian Olstad

--
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/e598f5d71fa93ea102e3f0e7e5178a00%40olstad.com.


[ansible-project] List of dicts => dict of lists

2019-11-22 Thread Dick Visser
Hi

I'm struggling with something seemingly simple.
I have this list:

instances:
  - id: i-0fe48c061f6a323a0
type: web
  - id: i-0403e39813defda31
type: web
  - id: i-0442f13b528e5e1c6
type: proxy
  - id: i-0ebea75d008becaa6
type: proxy
  - id: i-067c5d985c194e036
type: db
  - id: i-0679e87f06d0cf9a2
type: db


Which I'd like to turn into this structure:


instances:
  web:
- id: i-0fe48c061f6a323a0
- id: i-0403e39813defda31
  proxy:
- id: i-0442f13b528e5e1c6
- id: i-0ebea75d008becaa6
  db:
- id: i-067c5d985c194e036
- id: i-0679e87f06d0cf9a2


But I can't figure out how to do this...





-- 
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAL8fbwMY8Txx-OVkie-yxvxPNLigO8R5-W8etNM37ySE00U-Hw%40mail.gmail.com.