Testing a group_vars list and setting fact based on results...how?

I have a couple of lists defined in group_vars along with a lookup to determine the defined IP for the ilom:

…ansbile/inventory/group_vars/.yml

ilom_gateway:

  • 10.224.64.1
  • 10.85.0.1
  • 10.234.50.161

ilom_mask:

  • 255.255.248.0
  • 255.255.254.0
  • 255.255.255.224

ilom_ip: “{{ lookup(‘dig’, ‘mgmt-’ + ansible_local.system_facts.host_serial + ‘.example.com’) }}”

I need a way, in the playbook, to test and compare the ilom_gateway values to the ilom_ip – if it’s in the same subnet use that gateway and mask to configure the ilom. My question is, how? Would it be better to combine the lists (gw:mask) and split then test?

I'd make the information a different shape if i was you, rather than
trying to loop over 2 data structures. Something like

iloms:
  - gateway: 10.224.64.1
    mask: 255.255.248.0
  - gateway: 10.85.0.1
    mask: 255.255.254.0
  - gateway: 10.234.50.161
    mask: 255.255.255.224

and use with_items: iloms, accessing item.gateway and item.mask.

Ah yes, that makes better sense. But I’m still at a loss how I could determine the correct gw/mask to use after comparing the gateway to the ilom_ip … depending on location, there could be any from from one gateway/mask to 4 and I need to make sure the correct one is selected…