默认情况下,Ansible 2.7会在收集的事实中列出所有网络接口。该列表可能会很长,尤其是如果使用Docker和Kubernetes(具有适当的CNI,如Weave Net)。
对于某些防火墙规则,我只对实际的物理网卡感兴趣。虽然ansible_default_ipv4.interface
列出了我其中的一台,某些服务器(例如DMZ / LAN)中可能还会有更多服务器。
如何在Ansible 2.7剧本中获取物理网络适配器的列表?该机制应适用于基于Debian的Linux发行版以及RHEL。
答案 0 :(得分:4)
分开来看,关于服务器故障的同一主题还有一个问题,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