gpt4 book ai didi

ansible - 如何使用 Ansible 获取物理网络接口(interface)列表

转载 作者:行者123 更新时间:2023-12-02 17:10:41 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

根据我的测试:它返回了我当前家用 ubuntu 机器上的单个物理接口(interface)(安装了其他几个 veth、网桥、docker 接口(interface)...)以及 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: 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

关于ansible - 如何使用 Ansible 获取物理网络接口(interface)列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55880443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com