如何使用Ansible获取物理网络接口列表

如何使用Ansible获取物理网络接口列表,ansible,Ansible,默认情况下,Ansible 2.7列出了收集的事实中的所有网络接口。这个列表可能相当长,特别是如果使用Docker和Kubernetes(使用类似CNI的编织网) 对于一些防火墙规则,我只对实际的物理NIC感兴趣。例如,ansible\u default\u ipv4.interface为我列出了其中一个,但在某些服务器(例如DMZ/LAN)中可能会有更多 如何在Ansible 2.7剧本中获得物理网络适配器列表?这种机制应该适用于基于Debian的Linux发行版以及RHEL 除此之外,ser

默认情况下,Ansible 2.7列出了收集的事实中的所有网络接口。这个列表可能相当长,特别是如果使用Docker和Kubernetes(使用类似CNI的编织网)

对于一些防火墙规则,我只对实际的物理NIC感兴趣。例如,
ansible\u default\u ipv4.interface
为我列出了其中一个,但在某些服务器(例如DMZ/LAN)中可能会有更多


如何在Ansible 2.7剧本中获得物理网络适配器列表?这种机制应该适用于基于Debian的Linux发行版以及RHEL

除此之外,serverfault.com上还有一个关于同一主题的问题。我相信给定的命令应该在debian/ubuntu和Centos/RHEL上返回一致的结果

find/sys/class/net-type l-not-lname'*virtual*'-printf“%f\n”
从我的测试中:它返回了我当前家用ubuntu机器上的单一物理接口(安装了其他几个veth、bridges、docker接口…和centos:7 docker容器中的一个空字符串)

我将使用该命令并将其输出注册到变量中。以下是我刚才尝试的内容:

---
- 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 }}"
结果

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