data conversion question

40 views
Skip to first unread message

gregory....@gmail.com

unread,
Jun 14, 2022, 11:03:22 AM6/14/22
to Ansible Project
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?

Vladimir Botka

unread,
Jun 14, 2022, 11:54:04 AM6/14/22
to gregory....@gmail.com, ansible...@googlegroups.com
* 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

gregory....@gmail.com

unread,
Jun 14, 2022, 3:17:53 PM6/14/22
to Ansible Project

Thank you, Vladimir. 
It works. Is there any trick to make this with one-liner?

Vladimir Botka

unread,
Jun 14, 2022, 4:25:32 PM6/14/22
to gregory....@gmail.com, ansible...@googlegroups.com
On Tue, 14 Jun 2022 12:17:53 -0700 (PDT)
"gregory....@gmail.com" <gregory....@gmail.com> wrote:

> Thank you, Vladimir.
> It works. Is there any trick to make this with one-liner?

Sure. The one-liner below does the job as well. You're welcome

_dict: "{{ dict(_list|groupby('group')|map('first')|list|
zip(_list|groupby('group')|map('last')|
map('map',
attribute='name')|list))
}}"

--
Vladimir Botka

Hugo F. Gonzalez

unread,
Jun 15, 2022, 2:14:53 AM6/15/22
to ansible...@googlegroups.com
On Tue, Jun 14, 2022 at 10:03 AM gregory....@gmail.com <gregory....@gmail.com> 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


I'm assuming a *config file* here, so can you render it? The example format he docs on the groupby module comes in handy. https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.groupby

{% for group, values in (list | groupby('group') %} 
{{ group }}:{% for item in value %}{{ item }},{% endfor %}
{% endfor %}

Hugo F. Gonzalez

Automation Architect, Solutions & Technology Practices LATAM

Red Hat Services 

hgon...@redhat.com   
M: +52-5531490291    






Vladimir Botka

unread,
Jun 15, 2022, 4:02:32 AM6/15/22
to ansible...@googlegroups.com, Hugo F. Gonzalez
On Wed, 15 Jun 2022 01:13:17 -0500
"Hugo F. Gonzalez" <hgon...@redhat.com> wrote:

> {% for group, values in (list | groupby('group') %}
> {{ group }}:{% for item in value %}{{ item }},{% endfor %}
> {% endfor %}

The template can be fixed

{% for group, values in _list|groupby('group') %}
{{ group }}: {{ values|map(attribute='name')|join(',') }}
{% endfor %}

to get the expected result

g1: n1,n2
Reply all
Reply to author
Forward
0 new messages