I have the following defined in vars or defaults:
fusemq_authentication:
- { 'username':'admin', 'password’:'blah', 'groups':'admin' }
- { 'username':'LOGSTASH', 'password’:'bing', 'groups':'LOGSTASH' }
I want to know the password for LOGSTASH in a template. Today I loop until I find it. Is there a simpler way?
{% for value in fusemq_authentication %}
{% if value.username=="LOGSTASH" %}
fuseMQ_password={{ value.password }}
{% endif %}
{% endfor %}
--
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/72470009-3f65-4b70-85f1-e31c83b468bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
The problem is, sometimes it's nice to have a list to loop over especially if the client code doesn't know the keys.Other times it would be nice to have a dict if you have the key since writing loops can be messy.I think it would be very nice in these cases to have a dict_to_list adapter and list_to_dict adapter as jinja2 plugins, so the original datastructure can be whichever.
{{ fusemq_auth|selectattr('username','equalto','LOGSTASH')|map(attribute='password')|first }}
selectattr and map are new to version 2.7. The 'equalto' test is the problem you'll run in to.
So the map function with an optional selectattr provides the dict_to_list function you are looking for. (Your gist wouldn't load.) I don't see how a general purpose list_to_dict would be possible without some specific logic be written.
<tim/>