Skip to content
Advertisement

ansible ansible_default_ipv4 set wrong interface

I am trying to configure a host and automatically select the relevant interface.
For that I am trying to run the Ansible task:

JavaScript

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.

JavaScript

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:

JavaScript

So, maybe, a better idea would be to create a fact and exclude interfaces based on their active and type properties.

Something like:

JavaScript

Then, use that variable in network_interfaces_to_configure:

JavaScript

Here would be an example of what this gives on my own setup, given the playbook:

JavaScript

This yields:

JavaScript
Advertisement