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:
-
type_debug read from user_list and it is a dict then why it give a list as result?
-
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?
-
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