EDIT: Ok may be I couldn't explained it well so edit is below.
I have a list element that is filled with dict. items and looks like this:
"switch_vars": [
{
"leaf101": {
"node_id": "101",
"node_name": "leaf101",
"pod_id": "1"
},
"leaf102": {
"node_id": "102",
"node_name": "leaf102",
"pod_id": "1"
},
"spine103": {
"node_id": "103",
"node_name": "spine103",
"pod_id": "1"
}
}
]
When I try to access a value in a loop ansible says "'dict object' has no attribute 'why'".
- name: separate the pair leafs
set_fact:
first_leaf: "{{ item.split('-')[-2] }}"
second_leaf: "leaf{{ item.split('-')[-1] }}"
#This task is ok and gives:
#first_leaf: "leaf101",
#second_leaf: "leaf102"
- name: a test
set_fact:
test_var: "{{ switch_vars[0].why.node_id }}"
loop:
- first_leaf
- second_leaf
loop_control:
loop_var: why
I expect to get 101 for the first loop and 102 for the second loop.
EDIT: I changed the list to the following
- name: get a dict.
set_fact:
test_var: "{{ switch_vars | items2dict }}"
which gave me:
"test_var": {
"leaf101": {
"node_id": "101",
"node_name": "leaf101",
"pod_id": "1"
},
"leaf102": {
"node_id": "102",
"node_name": "leaf102",
"pod_id": "1"
},
"spine201": {
"node_id": "201",
"node_name": "spine201",
"pod_id": "1"
}
}
when I try to access the attribute of an element I try the following:
- debug:
msg:
- "{{ first_leaf }} #-->this is ok and gives as expected leaf101
- debug:
msg:
- "{{ test_var.first_leaf.node_id }}"
This task gives the error that the dict object has no attribute "first_leaf"
So my problem here is a templating problem but I don't know how to overcome it.