By default Ansible 2.7 lists all network interfaces in the gathered facts. This list can be quite long, especially if Docker and Kubernetes (with an appropriate CNI like Weave Net) are used.
For some firewall rules I am only interested in the actual physical NICs. While e.g. ansible_default_ipv4.interface
lists me one of them, there might be more in some servers (e.g. DMZ / LAN).
How do I get a list of physical network adapters in an Ansible 2.7 playbook? This mechanism should work for Debian-based Linux distributions as well as RHEL.
Ansible apart, there is a question on the same subject on serverfault.com with an interesting answer. I believe the given command should return a consistent result on both debian/ubuntu and Centos/RHEL.
find /sys/class/net -type l -not -lname '*virtual*' -printf '%f\n'
From my tests: it returned my single physical interface on my current home ubuntu machine (with several other veth, bridges, docker interfaces... installed) and an empty string in a centos:7 docker container.
I would use that command and register its output in a var. Here is what I just tried:
---
- name: details for physical interfaces
hosts: localhost
become: true
tasks:
- name: Get physical interfaces names
command: find /sys/class/net -type l -not -lname '*virtual*' -printf '%f\n'
register: physical_interfaces_cmd
changed_when: false
check_mode: false
- name: Show interfaces details
debug:
msg: "{{ lookup('vars', 'ansible_' + item) }}"
loop: "{{ physical_interfaces_cmd.stdout_lines }}"
Result
PLAY [details for physical interfaces] *****************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [Get physical interfaces names] *******************************************
changed: [localhost]
TASK [Show interfaces details] *************************************************
ok: [localhost] => (item=enp2s0) => {
"msg": {
"active": true,
"device": "enp2s0",
"features": {
"esp_hw_offload": "off [fixed]",
"esp_tx_csum_hw_offload": "off [fixed]",
"fcoe_mtu": "off [fixed]",
"generic_receive_offload": "on",
"generic_segmentation_offload": "off [requested on]",
"highdma": "on [fixed]",
"hw_tc_offload": "off [fixed]",
"l2_fwd_offload": "off [fixed]",
"large_receive_offload": "off [fixed]",
"loopback": "off [fixed]",
"netns_local": "off [fixed]",
"ntuple_filters": "off [fixed]",
"receive_hashing": "off [fixed]",
"rx_all": "off",
"rx_checksumming": "on",
"rx_fcs": "off",
"rx_udp_tunnel_port_offload": "off [fixed]",
"rx_vlan_filter": "off [fixed]",
"rx_vlan_offload": "on",
"rx_vlan_stag_filter": "off [fixed]",
"rx_vlan_stag_hw_parse": "off [fixed]",
"scatter_gather": "off",
"tcp_segmentation_offload": "off",
"tx_checksum_fcoe_crc": "off [fixed]",
"tx_checksum_ip_generic": "off [fixed]",
"tx_checksum_ipv4": "off",
"tx_checksum_ipv6": "off",
"tx_checksum_sctp": "off [fixed]",
"tx_checksumming": "off",
"tx_esp_segmentation": "off [fixed]",
"tx_fcoe_segmentation": "off [fixed]",
"tx_gre_csum_segmentation": "off [fixed]",
"tx_gre_segmentation": "off [fixed]",
"tx_gso_partial": "off [fixed]",
"tx_gso_robust": "off [fixed]",
"tx_ipxip4_segmentation": "off [fixed]",
"tx_ipxip6_segmentation": "off [fixed]",
"tx_lockless": "off [fixed]",
"tx_nocache_copy": "off",
"tx_scatter_gather": "off",
"tx_scatter_gather_fraglist": "off [fixed]",
"tx_sctp_segmentation": "off [fixed]",
"tx_tcp6_segmentation": "off",
"tx_tcp_ecn_segmentation": "off [fixed]",
"tx_tcp_mangleid_segmentation": "off",
"tx_tcp_segmentation": "off",
"tx_udp_tnl_csum_segmentation": "off [fixed]",
"tx_udp_tnl_segmentation": "off [fixed]",
"tx_vlan_offload": "on",
"tx_vlan_stag_hw_insert": "off [fixed]",
"udp_fragmentation_offload": "off",
"vlan_challenged": "off [fixed]"
},
"hw_timestamp_filters": [],
"ipv4": {
"address": "W.X.Y.Z",
"broadcast": "W.X.Y.255",
"netmask": "A.B.C.0",
"network": "W.X.Y.0"
},
"ipv6": [
{
"address": "aaaa:bbbb:cccc:dddd::zzzz",
"prefix": "128",
"scope": "global"
}
],
"macaddress": "aa:bb:cc:dd:ee:ff",
"module": "r8169",
"mtu": 1500,
"pciid": "0000:02:00.0",
"promisc": false,
"speed": 100,
"timestamping": [
"tx_software",
"rx_software",
"software"
],
"type": "ether"
}
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0