ansible list in dict (need to understand how it works)

2,446 views
Skip to first unread message

Hiero-nymo

unread,
Mar 8, 2022, 2:22:04 PM3/8/22
to Ansible Project
Hi everyone,

I try to understand ansible dictionaries and for my need I use lists in a dictionary which makes the code not simple and I am struggling to understand that:

Below there're part of my playbook and result:

user_list:
  - name: "name1"
    city: "New York"
    phone: "03-45-67"
  - name: "name2"
    city: "London"
    phone: "04-45-67"
  - name: "name3"
    city: "Berlin"
    phone: "05-45-67"

I need to retrieve information like each phone number for each name and I found this solution:

- name: debugging dict
  debug:
    msg:
      - "type of user_list: {{ user_list | type_debug }}"
      - " this {{ item['name'] }} is relation to this phone : {{ item['phone'] }}"
  loop: "{{ user_list }}"

and here's the result:

TASK [ansible_dict_test : debugging dict] *************************************************************************************************

ok: [localhost] => (item={'name': 'name1', 'city': 'New York', 'phone': '03-45-67'}) => {

    "msg": [

        "type of user_list: list",

        " this name1 is relation to this phone : 03-45-67"

    ]

}

ok: [localhost] => (item={'name': 'name2', 'city': 'London', 'phone': '04-45-67'}) => {

    "msg": [

        "type of user_list: list",

        " this name2 is relation to this phone : 04-45-67"

    ]

}

ok: [localhost] => (item={'name': 'name3', 'city': 'Berlin', 'phone': '05-45-67'}) => {

    "msg": [

        "type of user_list: list",

        " this name3 is relation to this phone : 05-45-67"

    ]

}

 I've some question about that:

1) type_debug read from user_list and it is a dict then why it give a list as result?

2) I has understood, user_list ist the key of dict and the rest is the value.  In that case why  item['name'] is like a key?

3) Is my playbook good or is a better solution?

Thank you in advance for your help because I am really trying to understand in detail how dictionaries work and I need your advice.

Best regards, H


Vladimir Botka

unread,
Mar 8, 2022, 6:28:04 PM3/8/22
to Hiero-nymo, ansible...@googlegroups.com
On Tue, 8 Mar 2022 11:22:04 -0800 (PST)
Hiero-nymo <jer.m...@gmail.com> wrote:

> 1) type_debug read from user_list and it is a dict then why it give a list
> as result?

The value of the variable *user_list* is a list (you wouldn't be able
to iterate it if otherwise)

user_list:
- city: New York
name: name1
phone: 03-45-67
- city: London
name: name2
phone: 04-45-67
- city: Berlin
name: name3
phone: 05-45-67

> 2) I has understood, user_list ist the key of dict and the rest is the
> value. In that case why item['name'] is like a key?

Yes. *user_list* is dictionary (aka hash, or mapping) with one key
*user_list*. The value of this key is the list. Each item of this
list is a dictionary therefore, in iteration, you can use *name* as a
key in the dictionary *item*.

> 3) Is my playbook good or is a better solution?

The dictionaries can be searched faster compared to the lists

- set_fact:
user_dict: "{{ dict(_keys|zip(user_list)) }}"
vars:
_keys: "{{ user_list|map(attribute='name')|list }}"

transforms the list to a dictionary

phone_book:
name1:
city: New York
name: name1
phone: 03-45-67
name2:
city: London
name: name2
phone: 04-45-67
name3:
city: Berlin
name: name3
phone: 05-45-67

Now you can easily search by names. If the names alone are not unique
create the keys as a combination of attributes

- set_fact:
phone_book: "{{ dict(_keys|zip(_vals)) }}"
vars:
_names: "{{ user_list|map(attribute='name')|list }}"
_addrs: "{{ user_list|map(attribute='city')|list }}"
_keys: "{{ _names|zip(_addrs)|map('join', ',')|list }}"
_vals: "{{ user_list|map(attribute='phone')|list }}"

create keys by joining the name and the city, and values by selecting
the phone only

phone_book:
name1,New York: 03-45-67
name2,London: 04-45-67
name3,Berlin: 05-45-67

You need the bracket notation to reference such keys

phone_book['name2,London']: 04-45-67

You'll have to stick with the lists if you are not able to create
unique keys.

--
Vladimir Botka

Hiero-nymo

unread,
Mar 9, 2022, 10:16:55 AM3/9/22
to Ansible Project
Thanks a lot for your detailed instructions. 
I understand dictionnaires much better now but I still have to practice.
I know we learn by doing but how do we know what tips or functions we need for a specific case. It is not easy to use something that we don't know about, by example I didn't know about zip. 
So I need to learn the other functions and how to involve them together.

Best regards, H
Reply all
Reply to author
Forward
0 new messages