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
On Saturday, July 4, 2015 at 11:18:50 PM UTC+10, Vishal Chainani wrote:
>
> 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
>
--
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/862068e6-1c55-4e2e-a671-b28c3cc03d82%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.