mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Better detection/categorization of ipv4 secondaries/virtual IPs
Fixes #3916 Fixes #3478
This commit is contained in:
parent
0e909640c5
commit
a2ac5d6df2
1 changed files with 71 additions and 63 deletions
|
@ -1440,10 +1440,9 @@ class LinuxNetwork(Network):
|
|||
path = os.path.join(path, 'bonding', 'all_slaves_active')
|
||||
if os.path.exists(path):
|
||||
interfaces[device]['all_slaves_active'] = open(path).read() == '1'
|
||||
ip_path = module.get_bin_path("ip")
|
||||
output = subprocess.Popen([ip_path, 'addr', 'show', device], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
|
||||
for line in output.split('\n'):
|
||||
|
||||
def parse_ip_output(output, secondary=False):
|
||||
for line in output.split('\n'):
|
||||
if not line:
|
||||
continue
|
||||
words = line.split()
|
||||
|
@ -1462,10 +1461,18 @@ class LinuxNetwork(Network):
|
|||
if iface != device:
|
||||
interfaces[iface] = {}
|
||||
interfaces[iface].update(interfaces[device])
|
||||
if "ipv4_secondaries" not in interfaces[iface]:
|
||||
interfaces[iface]["ipv4_secondaries"] = []
|
||||
if not secondary or "ipv4" not in interfaces[iface]:
|
||||
interfaces[iface]['ipv4'] = {'address': address,
|
||||
'netmask': netmask,
|
||||
'network': network}
|
||||
|
||||
else:
|
||||
interfaces[iface]["ipv4_secondaries"].append({
|
||||
'address': address,
|
||||
'netmask': netmask,
|
||||
'network': network,
|
||||
})
|
||||
# If this is the default address, update default_ipv4
|
||||
if 'address' in default_ipv4 and default_ipv4['address'] == address:
|
||||
default_ipv4['netmask'] = netmask
|
||||
|
@ -1474,36 +1481,37 @@ class LinuxNetwork(Network):
|
|||
default_ipv4['mtu'] = interfaces[device]['mtu']
|
||||
default_ipv4['type'] = interfaces[device].get("type", "unknown")
|
||||
default_ipv4['alias'] = words[-1]
|
||||
|
||||
if not address.startswith('127.'):
|
||||
ips['all_ipv4_addresses'].append(address)
|
||||
|
||||
elif words[0] == 'inet6':
|
||||
|
||||
address, prefix = words[1].split('/')
|
||||
scope = words[3]
|
||||
|
||||
if 'ipv6' not in interfaces[device]:
|
||||
interfaces[device]['ipv6'] = []
|
||||
|
||||
interfaces[device]['ipv6'].append({
|
||||
'address' : address,
|
||||
'prefix' : prefix,
|
||||
'scope' : scope
|
||||
})
|
||||
|
||||
# If this is the default address, update default_ipv6
|
||||
|
||||
if 'address' in default_ipv6 and default_ipv6['address'] == address:
|
||||
default_ipv6['prefix'] = prefix
|
||||
default_ipv6['scope'] = scope
|
||||
default_ipv6['macaddress'] = macaddress
|
||||
default_ipv6['mtu'] = interfaces[device]['mtu']
|
||||
default_ipv6['type'] = interfaces[device].get("type", "unknown")
|
||||
|
||||
if not address == '::1':
|
||||
ips['all_ipv6_addresses'].append(address)
|
||||
|
||||
ip_path = module.get_bin_path("ip")
|
||||
primary_data = subprocess.Popen(
|
||||
[ip_path, 'addr', 'show', 'primary', device],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
|
||||
secondary_data = subprocess.Popen(
|
||||
[ip_path, 'addr', 'show', 'secondary', device],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
|
||||
parse_ip_output(primary_data)
|
||||
parse_ip_output(secondary_data, secondary=True)
|
||||
return interfaces, ips
|
||||
|
||||
class GenericBsdIfconfigNetwork(Network):
|
||||
|
|
Loading…
Reference in a new issue