# selectattr with ansible

**URL:** <https://forum.ansible.com/t/selectattr-with-ansible/18675>\
**Category:** Ansible Project\
**Created:** [July 4, 2015, 7:11am UTC](https://forum.ansible.com/t/selectattr-with-ansible/18675 "2015-07-04T07:11:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vishal\_Chainani](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@Vishal\_Chainani](https://forum.ansible.com/u/Vishal_Chainani)\
**Post date:** [July 4, 2015, 7:11am UTC](https://forum.ansible.com/t/selectattr-with-ansible/18675/1 "2015-07-04T07:11:58Z")

</div>

Hi,

I have a list like below, and I need to select the sequence which has “name” attribute equal to “lo0”. I use this selectattr statement **{% set lo0 = interfaces|selectattr(“name”, “equalto”, lo0) | first%}**

But while running the playbook throws me an error {‘msg’: “TemplateRuntimeError: no test named ‘equalto’”, ‘failed’: True}

interfaces:

- name: lo0  
unit0:  
ip\_primary: 1.1.1.1  
ip\_secondary: 2.2.2.2  
unit1:  
ip\_primary: 3.3.3.3  
ip\_secondary: 4.4.4.4
- name: xyz  
unit0:  
ip\_primary: 9.9.9.9

Jinja2 version 2.7.3.

Any pointer what am I missing?

Vishal

---

<div class="post-metadata">

**Author:** ![igorc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/igorc/32/5491_2.png) [@igorc](https://forum.ansible.com/u/igorc)\
**Post date:** [July 5, 2015, 8:17am UTC](https://forum.ansible.com/t/selectattr-with-ansible/18675/2 "2015-07-05T08:17:19Z")

</div>

I think the 'equalto' test is only available in the 2.8+ version of jinja2

---

<div class="post-metadata">

**Author:** ![Vishal\_Chainani](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@Vishal\_Chainani](https://forum.ansible.com/u/Vishal_Chainani)\
**Post date:** [July 5, 2015, 2:03pm UTC](https://forum.ansible.com/t/selectattr-with-ansible/18675/3 "2015-07-05T14:03:08Z")

</div>

Hmmm…

so if have to select the list based on the value one of its key, is it possible?

I am trying something like this:

**{% for interfaces in interfaces if interfaces.name == “lo0” %}**  
**{{ interfaces.unit0.ip\_primary }}**  
**{% endfor %}**

But throws an error {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘list object’ has no attribute ‘lo0’”, ‘failed’: True}

Any ideas?

Vishal

---

<div class="post-metadata">

**Author:** ![Nico\_K](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@Nico\_K](https://forum.ansible.com/u/Nico_K)\
**Post date:** [July 6, 2015, 4:55am UTC](https://forum.ansible.com/t/selectattr-with-ansible/18675/4 "2015-07-06T04:55:03Z")

</div>

In your example, the loop variable is named identical to your list variable.

Try doing:

{% for interface in interfaces if interface.name == “lo0” %}  
{{ interface.unit0.ip\_primary }}  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![igorc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/igorc/32/5491_2.png) [@igorc](https://forum.ansible.com/u/igorc)\
**Post date:** [July 6, 2015, 5:28am UTC](https://forum.ansible.com/t/selectattr-with-ansible/18675/5 "2015-07-06T05:28:51Z")

</div>

This works for me:
