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

问题描述 投票:2回答:1

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

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

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

ansible
1个回答
4
投票

除了Ansible之外,在an interesting answer的服务器故障上有一个关于同一主题的问题。我相信给定的命令应该在debian / ubuntu和Centos / RHEL上返回一致的结果。

find /sys/class/net -type l -not -lname '*virtual*' -printf '%f\n'

从我的测试:它返回我当前的家庭ubuntu机器上的单个物理接口(安装了几个其他veth,桥接器,docker接口...)和centos:7 docker容器中的空字符串。

我会使用该命令并在var中注册其输出。这是我刚尝试过的:

---
- 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: phyintcmd
      changed_when: false
      check_mode: false

    - name: Show interfaces details
      debug:
        msg: "{{ lookup('vars', 'ansible_' + item) }}"
      loop: "{{ phyintcmd.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  
© www.soinside.com 2019 - 2024. All rights reserved.