setting facts and nesting vars

First post …

In my playbook, I have a variable called mgmt_network. I want to iterate through ansible_interfaces and set a fact when ansible_[item].ipv4.network = mgmt_network. Then later, use that fact to define a route

vars:
mgmt_network = 10.10.10.0

  • set_fact:
    mgmt_interface: "{{ item }}’
    when: “{{ [‘ansible_’ + item + ‘.ipv4.network’] }}” == “{{ mgmt_network }}”
    with_items: "{{ ansible_interfaces }}

then later …
lineinfile: dest=/etc/sysconfig/network-scripts/route-{{mgmt_interface}} line=“12.10.10.0/24 via dev {{mgmt_interface}}”

but I’m having trouble with that when statement, so I’m wondering if somebody can lend me a hand in order to understand why this isn’t working.

Thanks for reading.

Arthur

try

when: hostvars[inventory_hostname]['ansible_' +
item]['ipv4']['network'] == mgmt_network

Brilliant. Exactly what I needed.

Thank you for your time.