I am trying to configure a host and automatically select the relevant interface.
For that I am trying to run the Ansible task:
- name: Set the list of interfaces to be configured network_interfaces_to_configure: "{{ ansible_interfaces | difference([ ansible_default_ipv4.alias, 'lo', 'docker0' ]) }}"
For some reason it stores in network_interfaces_to_configure
an interface that is deactivated — it sets it to enp1s0f1
and it should be set to enp1s0f0
The host have the following interfaces:
- lo
- docker0 (virtual)
- enp1s0f0 (hardware connected and enabled)
- enp1s0f1 (hardware disabled and without any physical connection)
I also validated that the default gateway is set to enp1s0f0
.
Advertisement
Answer
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