Unable to fetch value from ansible_devices variable if special charcter in key

I am using gather fact variable to get size from ansible_devices variable. for some host I am getting variable "ansible_devices": { "sda" and for few server getting "ansible_devices": { "cciss!c0d0".

Problem:- When I am using variable {{** ansible_devices.**__sda.size__ }} its working perfectly. But when I am trying to access value from {{ ansible_devices.**cciss!c0d0**.size }} in my playbook then it is not returning any value whereas in json ansible_devices variable there is value exist for {{ ansible_devices.**cciss!c0d0**.size }}.

``

`
{
“ansible_devices”: {
“cciss!c0d0”: {
“holders”: ,
“host”: “RAID bus controller: Hewlett-Packard Company Smart Array Controller (rev 03)”,
“model”: “LOGICAL VOLUME”,
“partitions”: {},
“removable”: “0”,
“rotational”: “1”,
“scheduler_mode”: “cfq”,
“sectors”: “143299800”,
“sectorsize”: “512”,
“size”: “68.33 GB”,
“support_discard”: “0”,
“vendor”: “HP”
}
},
“item”: “”
}

`

It might be the case special character in a json key that’s why I am unable to fetch its value. can anyone suggest me how can I access {{ ansible_devices.**cciss!c0d0**.size }} value here ?

That’s not a valid variable name, so you’ll have to attack it edgewise:

{{ hostvars[inventory_hostname]["ansible_devices"]["**cciss!c0d0"]["**size"] }}

Usage of set_fact would allow assigning it to a cleaner variable name.

Thanks for your help. Nice Trick. Its working for me.