Get item from list

28 views
Skip to first unread message

Trond Hindenes

unread,
Jun 7, 2016, 6:39:57 AM6/7/16
to Ansible Project
Hi have the following inside a variable:
[{u'maxDataDiskCount': 2,          
  u'memoryInMB': 3584,             
  u'name': u'Standard_DS1',        
  u'numberOfCores': 1,             
  u'osDiskSizeInMB': 1047552,      
  u'resourceDiskSizeInMB': 7168},  
 {u'maxDataDiskCount': 4,          
  u'memoryInMB': 7168,             
  u'name': u'Standard_DS2',        
  u'numberOfCores': 2,             
  u'osDiskSizeInMB': 1047552,      
  u'resourceDiskSizeInMB': 14336}, 
 {u'maxDataDiskCount': 8,          
  u'memoryInMB': 14336,            
  u'name': u'Standard_DS3',        
  u'numberOfCores': 4,             
  u'osDiskSizeInMB': 1047552,      
  u'resourceDiskSizeInMB': 28672}, 
 {u'maxDataDiskCount': 16,         
  u'memoryInMB': 28672,            
  u'name': u'Standard_DS4',        
  u'numberOfCores': 8,             
  u'osDiskSizeInMB': 1047552,      
  u'resourceDiskSizeInMB': 57344}, 
 {u'maxDataDiskCount': 4,          
  u'memoryInMB': 14336,            
  u'name': u'Standard_DS11',       
  u'numberOfCores': 2,             
  u'osDiskSizeInMB': 1047552,      
  u'resourceDiskSizeInMB': 28672}]

I need to get to a given object based on 'name' 

Sorry, this is probably super-easy but I'm strugglig like **** with advanced jinja stuff.

Trond Hindenes

unread,
Jun 7, 2016, 7:59:54 AM6/7/16
to Ansible Project
btw, I'm able to do this if I use a separate template file:
{% for vm in arm_vm_details %}
{% if vm.name == vm_size %}
print 'hello {{ vm.maxDataDiskCount }}'
{% endif %}
{% endfor %}

However, that seems like an uneccessary number of steps, as I simply want to get to the correct values for my given vm size (name). If anyone 

Matt Martz

unread,
Jun 7, 2016, 8:23:26 AM6/7/16
to ansible...@googlegroups.com
I think the following is roughly equivalent. I haven't tested it, so there may be some tweaks needed.

{{ (arm_vm_details|selectattr('name', 'equalto', vm_size)|first).maxDataDiskCount }}
--
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/8844ebd1-39e7-4db2-9d5f-6bfb24713c79%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Reply all
Reply to author
Forward
0 new messages