I am trying to get data from json data (below) using a loop with a variable (instead of hard coding a value). In the json data (below), 'cluster' can change therefore I cannot simply use: loop: "{{ drs_rule_jsondata.drs_rule_info.cluster }}" -- which works as I expect it would and produces the expected results.
However, when I use the play below (using vars in the loop and when {{ cluster_info.name }} = cluster), I get "msg": "'dict object' has no attribute 'drs_rule_jsondata.drs_rule_info.cluster'". The confusing part is, it produces the same syntax as the one that works.... should using vars this way in the loop not work? Is there another way to get the expected results given the 'cluster' can change?
Play:
- name: Set drs rule info
set_fact:
drs_rule: "{{ drs_rule|default([]) + [ {
'rule_name': item | json_query('rule_name'),
'rule_affinity': item | json_query('rule_affinity'),
'rule_vms': item | json_query('rule_vms[*]')
} ] }}"
vars:
path: drs_rule_jsondata.drs_rule_info
cluster_name: "{{ cluster_info.name }}"
loop: "{{ vars[path + '.' + cluster_name] }}"
json data:
{
"ansible_facts": {
"drs_rule_jsondata": {
"changed": false,
"drs_rule_info": {
"cluster": [
{
"rule_affinity": true,
"rule_enabled": true,
"rule_key": 1,
"rule_mandatory": null,
"rule_name": "DEMO_REP_DRS_1",
"rule_type": "vm_vm_rule",
"rule_uuid": "522d41eb-4acb-afbf-9f37-15a1651ccf45",
"rule_vms": [
"VM1",
"VM2",
"VM3",
"VM4"
]
]
},
"failed": false
}
},
"_ansible_no_log": false,
"changed": false
}
Expected results:
{
"drs_rule": [
{
"rule_name": "DEMO_REP_DRS_1",
"rule_affinity": true,
"rule_vms": [
"VM1",
"VM2",
"VM3",
"VM4"
]
}
]
}