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

455 views
Skip to first unread message

Roopendra Vishwakarma

unread,
Jun 23, 2014, 4:52:21 AM6/23/14
to ansible...@googlegroups.com

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 ?

Michael DeHaan

unread,
Jun 23, 2014, 8:11:21 AM6/23/14
to ansible...@googlegroups.com
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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8a3184ab-fcc4-462b-aa28-4409f7a2264a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Roopendra Vishwakarma

unread,
Jun 24, 2014, 3:22:50 AM6/24/14
to ansible...@googlegroups.com
Thanks for your help.  Nice Trick. Its working for me.
Reply all
Reply to author
Forward
0 new messages