在Debian和FreeBSD之间,如何选择给定ip地址的网络接口?

分享于 

7分钟阅读

互联网

  繁體

问题:

我正在寻找一个表达式,以在Linux和FreeBSD上获取分配给该iface的IP地址的接口名称。


iface_for_ip: >-


 {{ ansible_facts


 | dict2items


 | selectattr('value.ipv4', 'defined')


 | selectattr('value.ipv4.address', 'equalto', given_ip)


 | first }}



但是,这在FreeBSD上不能工作,因为ipv4结构是一个数组,而不是一个对象。

如果仅运行这个代码段:


iface_for_ip: >-


 {{ ansible_facts


 | dict2items


 | selectattr('value.ipv4', 'defined') }}



得到如下所示的输出:

在Debian上

 - key: eth0


 value:


 active: true


 device: eth0


 ipv4:


 address: 10.8.20.206


 broadcast: 10.8.20.255


 netmask: 255.255.255.0


 network: 10.8.20.0


 ipv6:


 - address: fe80::84ee:35ff:fed4:a23c


 prefix: '64'


 scope: link


 macaddress: 00:ee:35:00:00:00


 mtu: 1500


 promisc: false


 speed: 10000


 type: ether



在FreeBSD上

 - key: epair0b


 value:


 device: epair0b


 flags:


 - UP


 - BROADCAST


 - RUNNING


 - SIMPLEX


 - MULTICAST


 ipv4:


 - address: 10.8.20.207


 broadcast: 10.8.20.255


 netmask: 255.255.255.0


 network: 10.8.20.0


 ipv6: []


 macaddress: 00:ee:23:00:00:00


 media: Ethernet


 media_options:


 - full-duplex


 media_select: 10Gbase-T


 media_type: 10Gbase-T


 metric: '0'


 mtu: '1500'


 options:


 - PERFORMNUD


 status: active


 type: ether




答案1:

在Debian和FreeBSD上,收集的数据有所不同。

在Ubuntu (Debian衍生产品)中,属性IPv4是一个字典,辅助地址存储在列表ipv4_secondaries中,第一步创建设备和IPv4地址的列表。例如


 - debug:


 var: ansible_facts.distribution


 - set_fact:


 ifc_list: "{{ ansible_facts|


 dict2items|


 json_query(query)|


 selectattr('ipv4')|list }}"


 vars:


 query: "[?value.type == 'ether'].{device: value.device,


 ipv4: value.ipv4.address}"


 - debug:


 var: ifc_list




 ansible_facts.distribution: Ubuntu



 ifc_list:


 - device: eth0


 ipv4: 10.1.0.27



然后选择给定ip地址的网络接口"


 - set_fact:


 iface_for_ip: "{{ ifc_list|


 selectattr('ipv4', 'eq', ip_address)|


 map(attribute='device')|list }}"


 vars:


 ip_address: "10.1.0.27"


 - debug:


 var: iface_for_ip




 iface_for_ip:


 - eth0



在FreeBSD中,属性IPv4是一个列表。创建设备和IPv4的列表


 - debug:


 var: ansible_facts.distribution


 - set_fact:


 ifc_list: "{{ ansible_facts|


 dict2items|


 json_query(query)|


 selectattr('ipv4')|list }}"


 vars:


 query: "[?value.type == 'ether'].{device: value.device,


 ipv4: value.ipv4[].address}"


 - debug:


 var: ifc_list




 ansible_facts.distribution: FreeBSD



 ifc_list:


 - device: wlan0


 ipv4:


 - 10.1.0.51



然后选择给定ip地址的网络接口"


 - set_fact:


 iface_for_ip: "{{ iface_for_ip|default([]) + [item.device] }}"


 loop: "{{ ifc_list }}"


 when: ip_address in item.ipv4


 vars:


 ip_address: "10.1.0.51"


 - debug:


 var: iface_for_ip




 iface_for_ip:


 - wlan0



您将必须编写自己的测试。例如


shell> cat test_plugins/contains.py 



def contains(l, value):


 return value in l



class TestModule:


 """Main test class from Ansible."""



 def tests(self):


 """Add these tests to the list of tests available to Ansible."""


 return {


 'contains': contains


 }



然后set_fact给出相同的结果。该表达式也可以在vars中使用


 - set_fact:


 iface_for_ip: "{{ ifc_list|


 selectattr('ipv4', 'contains', ip_address)|


 map(attribute='device')|list }}"




答案2:


vars:


 freebsd_query: "[*].{device: device, active: active, ipv6: ipv6, ipv4: ipv4[? address == '{{ ip_find_iface }}']}[?ipv4])" # string must be in ' # sorry, only partial interface info, did not find out how to return all info directly


 linux_query: "[?ipv4.address == '{{ ip_find_iface }}']" # string must be in '


 ipv6_query: "[*].{device: device, active: active, ipv4: ipv4, ipv6: ipv6[? address == '{{ ip_find_iface }}']}[?ipv6]" # string must be in ' # sorry, only partial info, did not find how to return the full one


 ip_query: "{{ ip_find_iface | ipv6 | ternary(


 ipv6_query,


 ansible_facts[ansible_facts.interfaces | first].ipv4 is mapping | ternary(linux_query, freebsd_query)


 ) }}.device"


 all_interfaces: "{{ ansible_facts.interfaces | map('extract', ansible_facts) }}"


 iface_for_ip: "{{ all_interfaces | json_query(ip_query) }}"



ip_query首先检查ip是否为IPv6格式,如果不是,它检查{anyNetwork}.ipv4是否为dict,json_query()的查询根据以下情况被选中。

https://gist.github.com/Ramblurr/5d8324e0154ea6be52407618222fcaf7)



INT  添加  接口  network  Select  DEB  
相关文章