The reason why you end up with the wrong interface there is because you are excluding the correct one in your difference filter.
difference([ ansible_default_ipv4.alias, 'lo', 'docker0' ])
## ^--- it is excluded because of this addition
## of `ansible_default_ipv4.alias` in the list
Now, as you stated that enp1s0f1 should be excluded because it is an inactive interface, then you will have to dig further in the properties of those interfaces.
In order to do this, you have to know that the list inside ansible_interfaces, corresponds to ansible_* facts that list more information for those interfaces.
For example, in your case, you will have those facts:
ansible_lo
ansible_docker0
ansible_enp1s0f0
ansible_enp1s0f1
With information like:
ansible_lo:
active: true
device: lo
ipv4:
address: 127.0.0.1
broadcast: ''
netmask: 255.0.0.0
network: 127.0.0.0
mtu: 65536
promisc: false
type: loopback
So, maybe, a better idea would be to create a fact and exclude interfaces based on their active and type properties.
Something like:
- set_fact:
active_interfaces: "{{ active_interfaces | default([]) + [item] }}"
loop: "{{ ansible_interfaces }}"
when:
## only include active interfaces
- lookup('vars', 'ansible_' ~ item).active
## exclude local loopback interfaces (`lo`)
- lookup('vars', 'ansible_' ~ item).type != 'loopback'
## exclude bridge interfaces (`dockerO`)
- lookup('vars', 'ansible_' ~ item).type != 'bridge'
Then, use that variable in network_interfaces_to_configure:
- network_interfaces_to_configure: "{{ active_interfaces }}"
Here would be an example of what this gives on my own setup, given the playbook:
- hosts: localhost
gather_facts: yes
tasks:
- set_fact:
active_interfaces: "{{ active_interfaces | default([]) + [item] }}"
loop: "{{ ansible_interfaces }}"
when:
- lookup('vars', 'ansible_' ~ item).active
- lookup('vars', 'ansible_' ~ item).type != 'loopback'
- lookup('vars', 'ansible_' ~ item).type != 'bridge'
- debug:
var: active_interfaces
This yields:
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [set_fact] ****************************************************************
ok: [localhost] => (item=eth0)
skipping: [localhost] => (item=tunl0)
skipping: [localhost] => (item=docker0)
skipping: [localhost] => (item=lo)
ok: [localhost] => (item=services1)
skipping: [localhost] => (item=ip6tnl0)
TASK [debug] *******************************************************************
ok: [localhost] =>
active_interfaces:
- eth0
- services1