how to rewrite this the proper way?

17 views
Skip to first unread message

gregory....@gmail.com

unread,
Jun 25, 2021, 4:38:59 AM6/25/21
to Ansible Project
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?

gregory....@gmail.com

unread,
Jun 25, 2021, 5:15:03 AM6/25/21
to Ansible Project
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???
Reply all
Reply to author
Forward
0 new messages