found a solution myself

 - name: find interface without an ip
    set_fact:
      desired_interface_name: "{{ item }}"
    when:
      hostvars[inventory_hostname]['ansible_'+item].ipv4|length == 0 
      # ansible_{{ item }}.ipv4 is defined and (ansible_{{ item }}|length>0)
    with_items: 
      "{{ ansible_interfaces }}"

it works. although it seems a bit counterintuitive...
why not the first way???
On Friday, June 25, 2021 at 11:38:59 AM UTC+3 [email protected] wrote:

> Hello
>
> here's my playbook, I am looking for interface without IP:
>
> ---
> - hosts: all
>   vars:
>     interface_name: ""
>     ipaddress: "{{ ansible_default_ipv4.address }}"
>   tasks:
>   - name: enumerate interfaces
>     debug: 
>       msg: "{{ item }}"
>     with_items:
>       "{{ ansible_interfaces }}"
>
>   - debug:
>       var:  ansible_{{ item }} 
>     with_items:
>       "{{ ansible_interfaces }}"
>  
>   - name: find interface without an ip
>     set_fact:
>       desired_interface_name: "{{ item }}"
>     when:
>       ansible_{{ item }}.ipv4 is defined and (ansible_{{ item }}|length>0)
>     with_items: 
>       "{{ ansible_interfaces }}"
>
>   - name: print result
>     debug:
>       var: desired_interface_name
>
> it works, but yells me a warn:
> [WARNING]: conditional statements should not include jinja2 templating 
> delimiters such as {{ }} or {% %}. Found: ansible_{{item }}.ipv4 is defined 
> and (ansible_{{ item }}|length>0)
>
> just out for curiosity, what's the proper way?
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/b242f85c-aef1-417b-8a90-e977949abbe2n%40googlegroups.com.

Reply via email to