Matt's solution is fine (and elegant!) for simple cases like this
particular one. However, I found myself needing to filter items on some
more complex conditions a few times and started using the Jinja do
extension to effectively turn variable assignments into arbitrary
computations:
my_list: |
{%- set names = [] -%}
{%- for item in my_dict if item.item_en -%}
{%- do names.append(item.name) -%}
{%- endfor -%}
{{ names }}
It's important to do proper whitespace-control (the dashes in {%- ... -%})
because in the end the string assigned to my_list must be "['name1',
'name3']" and not something like " ['name1', 'name3']". In the former
case, Ansible converts it to a proper list (because the value starts with
'['), in the latter my_list would be assigned the actual string " [...]".
And you need to enable the Jinja extension in your ansible.cfg:
[defaults]
jinja2_extensions = jinja2.ext.do
As mentioned in the beginning, this isn't really necessary in this case and
certainly isn't very much in Ansible's spirit of simplicity, but it has
saved me from duplicating information across multiple variables several
times.
On Wednesday, May 20, 2015 at 3:20:17 PM UTC+2, Matt Martz wrote:
>
> I think you want something like:
>
> my_list: "{{ my_dict|selectattr('item_en')|map(attribute='name')|list }}"
>
> On Tuesday, May 19, 2015, Javeria Khan <[email protected] <javascript:>>
> wrote:
>
>> Hi,
>>
>> I've tried searching everything but can't find a possible solution. I
>> have a yml that defines all my variables for a playbook and I need to do
>> something like the following inside of it:
>> ----------------
>> my_dict:
>> - { item_en: True, name: name1, type: type1, key: value1 }
>> - { item_en: False, name: name2, type: type2, key: value2 }
>> - { item_en: True, name: name3, type: type3, key: value3 }
>>
>> my_list:
>> {% for item in my_dict %}
>> {% if my_dict[item].item_en == True %}
>> {{ my_dict[item].name }}
>> {% endfor %}
>> ---------------
>>
>> So basically I need 'my_list' to be a list of 'names' from every dict
>> object that has item_en = True. Based on the data structure above, it would
>> look like this:
>>
>> my_list:
>> - name1
>> - name3
>>
>> I know this is possible using the jinja loop syntax in a template but
>> this isn't a template file.
>>
>> Thanks
>>
>> --
>> 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 [email protected].
>> To post to this group, send email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/ansible-project/5e6a8d23-4835-44c2-9902-5ec3a49feebb%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/ansible-project/5e6a8d23-4835-44c2-9902-5ec3a49feebb%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
> --
> Matt Martz
> @sivel
> sivel.net
>
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/71f13d7f-50ee-48ab-bfa4-1f622e92179c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.