create a dict from a list (value of another dict)

39 views
Skip to first unread message

Hiero-nymo

unread,
Mar 15, 2022, 11:51:19 AM3/15/22
to Ansible Project
Hi Team,

My playbook role receive a dict coming from a lookup file, linux_file_list is the variable name of this lookup. This dict has only one key and some lists as value. Elements of these lists are dict too.

Here's my debug to see the content:

- Name: debugging
  debug:
    msg:"{{ linux_file_list | dict2items }}"

And here's the result (a part...):

TASK [reposync : debugging] *******************************************************************************************************************************************************

ok: [] => {

    "msg": [

        [

            {

                "key": "list_of_linux",

                "value": [

                    {

                        "description": "linux SLES",

                        "enabled": 1,

                        "version": 15,

                        "name": "sles"

                    },

                    {

                        "description": "linux RHEL",

                        "enabled": 1,

                        "version": 8.4,

                        "name": "rhel"

                    }

                ]

            }

        ]

    ]

}

Now, I want to define the name value as a dict to loop on this to have  at the end a loop to retrieve these variables, like so…

"name: {{ item['name'] }}"
"description: {{ item['description'] }}"
"version: {{ item['version'] }}"
etc…

Unfortunately I don't find how can i do that. I've try many things, like item.value[0][0] to have the first element of the first list but it doesn't run. 

With set_fact i cannot do a dict on the list too:

- set_fact:
     dict: “{{ dict(_keys|zip(linux_file_list)) }}"
  vars:
    _keys: "{{ item['value']|map(attribute='name')|list }}"
  loop: "{{ linux_file_list | dict2items }}"

As anyone an Idea how can I resolve this? I can't see my problem clearly anymore and I need your help

Thanks in advance for your help, H

Hamza Bouabdallah

unread,
Mar 16, 2022, 5:41:58 AM3/16/22
to Ansible Project
try the  subelement filter

Hiero-nymo

unread,
Mar 16, 2022, 12:46:24 PM3/16/22
to Ansible Project
I've try some subelements but cannot retrieve the right data.

Stefan Hornburg (Racke)

unread,
Mar 16, 2022, 4:45:36 PM3/16/22
to ansible...@googlegroups.com
On 16/03/2022 17:46, Hiero-nymo wrote:
> I've try some subelements but cannot retrieve the right data.
> ie

I don't get why you use the dict2items filter on a list of dicts. You can start producing output from the "linux_file_list" variable directly.

Regards
Racke


> On Wednesday, March 16, 2022 at 10:41:58 AM UTC+1 h...@startx.fr wrote:
>
> try the *subelement *filter
> Le mardi 15 mars 2022 à 16:51:19 UTC+1, Hiero-nymo a écrit :
>
> Hi Team,
>
> My playbook role receive a dict coming from a lookup file, *linux_file_list* is the variable name of this lookup. This dict has only one key and some lists as value. Elements of these lists are dict too.
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b172badc-0b9d-4d86-9437-37d07b387dddn%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/b172badc-0b9d-4d86-9437-37d07b387dddn%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Automation expert - Ansible and friends
Linux administrator & Debian maintainer
Perl Dancer & conference hopper

OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages