selectattr with ansible

1,995 views
Skip to first unread message

Vishal Chainani

unread,
Jul 4, 2015, 9:18:50 AM7/4/15
to ansible...@googlegroups.com
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

Igor Cicimov

unread,
Jul 5, 2015, 4:17:19 AM7/5/15
to ansible...@googlegroups.com
I think the 'equalto' test is only available in the 2.8+ version of jinja2

Vishal Chainani

unread,
Jul 5, 2015, 10:03:08 AM7/5/15
to ansible...@googlegroups.com
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

Nico K.

unread,
Jul 6, 2015, 12:55:03 AM7/6/15
to ansible...@googlegroups.com
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 %}

Igor Cicimov

unread,
Jul 6, 2015, 1:28:51 AM7/6/15
to ansible...@googlegroups.com
This works for me:

---
- hosts: localhost
  connection: local
  gather_facts: false
  vars:

   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
  tasks:
   - set_fact:
      myip: |
       {%- set ips = [] -%}

       {% for interface in interfaces if interface.name == "lo0" %}
         {%- do ips.append(interface.unit0.ip_primary) -%}
       {%- endfor -%}
       {{ ips }}
   - debug: var=myip

$ ansible-playbook -i local test101.yml

PLAY [localhost] **************************************************************

TASK: [set_fact ] *************************************************************
ok: [localhost]

TASK: [debug var=myip] ********************************************************
ok: [localhost] => {
    "var": {
        "myip": [
            "1.1.1.1"
        ]
    }
}

PLAY RECAP ********************************************************************
set_fact  --------------------------------------------------------------- 0.04s
debug var=myip ---------------------------------------------------------- 0.00s
localhost                  : ok=2    changed=0    unreachable=0    failed=0
Reply all
Reply to author
Forward
0 new messages