interfaces_file: Add support for address_family

pull/47447/head
Jocelyn Jaubert 6 years ago committed by Michael Scherer
parent a4916e848d
commit 7ec49d7472

@ -30,6 +30,10 @@ options:
iface: iface:
description: description:
- Name of the interface, required for value changes or option remove - Name of the interface, required for value changes or option remove
address_family:
description:
- Address family of the interface, useful if same interface name is used for both inet and inet6
version_added: "2.8"
option: option:
description: description:
- Name of the option, required for value changes or option remove - Name of the option, required for value changes or option remove
@ -148,8 +152,8 @@ def lineDict(line):
return {'line': line, 'line_type': 'unknown'} return {'line': line, 'line_type': 'unknown'}
def optionDict(line, iface, option, value): def optionDict(line, iface, option, value, address_family):
return {'line': line, 'iface': iface, 'option': option, 'value': value, 'line_type': 'option'} return {'line': line, 'iface': iface, 'option': option, 'value': value, 'line_type': 'option', 'address_family': address_family}
def getValueFromLine(s): def getValueFromLine(s):
@ -208,13 +212,14 @@ def read_interfaces_lines(module, line_strings):
currif['address_family'] = words[2] currif['address_family'] = words[2]
except IndexError: except IndexError:
currif['address_family'] = None currif['address_family'] = None
address_family = currif['address_family']
try: try:
currif['method'] = words[3] currif['method'] = words[3]
except IndexError: except IndexError:
currif['method'] = None currif['method'] = None
ifaces[iface_name] = currif ifaces[iface_name] = currif
lines.append({'line': line, 'iface': iface_name, 'line_type': 'iface', 'params': currif}) lines.append({'line': line, 'iface': iface_name, 'line_type': 'iface', 'params': currif, 'address_family': address_family})
currently_processing = "IFACE" currently_processing = "IFACE"
elif words[0] == "auto": elif words[0] == "auto":
lines.append(lineDict(line)) lines.append(lineDict(line))
@ -233,7 +238,7 @@ def read_interfaces_lines(module, line_strings):
option_name = words[0] option_name = words[0]
# TODO: if option_name in currif.options # TODO: if option_name in currif.options
value = getValueFromLine(line) value = getValueFromLine(line)
lines.append(optionDict(line, iface_name, option_name, value)) lines.append(optionDict(line, iface_name, option_name, value, address_family))
if option_name in ["pre-up", "up", "down", "post-up"]: if option_name in ["pre-up", "up", "down", "post-up"]:
currif[option_name].append(value) currif[option_name].append(value)
else: else:
@ -248,11 +253,14 @@ def read_interfaces_lines(module, line_strings):
return lines, ifaces return lines, ifaces
def setInterfaceOption(module, lines, iface, option, raw_value, state): def setInterfaceOption(module, lines, iface, option, raw_value, state, address_family=None):
value = str(raw_value) value = str(raw_value)
changed = False changed = False
iface_lines = [item for item in lines if "iface" in item and item["iface"] == iface] iface_lines = [item for item in lines if "iface" in item and item["iface"] == iface]
if address_family is not None:
iface_lines = [item for item in iface_lines
if "address_family" in item and item["address_family"] == address_family]
if len(iface_lines) < 1: if len(iface_lines) < 1:
# interface not found # interface not found
@ -267,12 +275,12 @@ def setInterfaceOption(module, lines, iface, option, raw_value, state):
changed = True changed = True
# add new option # add new option
last_line_dict = iface_lines[-1] last_line_dict = iface_lines[-1]
lines = addOptionAfterLine(option, value, iface, lines, last_line_dict, iface_options) lines = addOptionAfterLine(option, value, iface, lines, last_line_dict, iface_options, address_family)
else: else:
if option in ["pre-up", "up", "down", "post-up"]: if option in ["pre-up", "up", "down", "post-up"]:
if len(list(filter(lambda i: i['value'] == value, target_options))) < 1: if len(list(filter(lambda i: i['value'] == value, target_options))) < 1:
changed = True changed = True
lines = addOptionAfterLine(option, value, iface, lines, target_options[-1], iface_options) lines = addOptionAfterLine(option, value, iface, lines, target_options[-1], iface_options, address_family)
else: else:
# if more than one option found edit the last one # if more than one option found edit the last one
if target_options[-1]['value'] != value: if target_options[-1]['value'] != value:
@ -280,6 +288,7 @@ def setInterfaceOption(module, lines, iface, option, raw_value, state):
target_option = target_options[-1] target_option = target_options[-1]
old_line = target_option['line'] old_line = target_option['line']
old_value = target_option['value'] old_value = target_option['value']
address_family = target_option['address_family']
prefix_start = old_line.find(option) prefix_start = old_line.find(option)
optionLen = len(option) optionLen = len(option)
old_value_position = re.search(r"\s+".join(old_value.split()), old_line[prefix_start + optionLen:]) old_value_position = re.search(r"\s+".join(old_value.split()), old_line[prefix_start + optionLen:])
@ -287,7 +296,7 @@ def setInterfaceOption(module, lines, iface, option, raw_value, state):
end = old_value_position.end() + prefix_start + optionLen end = old_value_position.end() + prefix_start + optionLen
line = old_line[:start] + value + old_line[end:] line = old_line[:start] + value + old_line[end:]
index = len(lines) - lines[::-1].index(target_option) - 1 index = len(lines) - lines[::-1].index(target_option) - 1
lines[index] = optionDict(line, iface, option, value) lines[index] = optionDict(line, iface, option, value, address_family)
elif state == "absent": elif state == "absent":
if len(target_options) >= 1: if len(target_options) >= 1:
if option in ["pre-up", "up", "down", "post-up"] and value is not None and value != "None": if option in ["pre-up", "up", "down", "post-up"] and value is not None and value != "None":
@ -304,7 +313,7 @@ def setInterfaceOption(module, lines, iface, option, raw_value, state):
return changed, lines return changed, lines
def addOptionAfterLine(option, value, iface, lines, last_line_dict, iface_options): def addOptionAfterLine(option, value, iface, lines, last_line_dict, iface_options, address_family):
# Changing method of interface is not an addition # Changing method of interface is not an addition
if option == 'method': if option == 'method':
for ln in lines: for ln in lines:
@ -323,7 +332,7 @@ def addOptionAfterLine(option, value, iface, lines, last_line_dict, iface_option
prefix += " " prefix += " "
line = prefix + "%s %s" % (option, value) + last_line[suffix_start:] line = prefix + "%s %s" % (option, value) + last_line[suffix_start:]
option_dict = optionDict(line, iface, option, value) option_dict = optionDict(line, iface, option, value, address_family)
index = len(lines) - lines[::-1].index(last_line_dict) index = len(lines) - lines[::-1].index(last_line_dict)
lines.insert(index, option_dict) lines.insert(index, option_dict)
return lines return lines
@ -343,6 +352,7 @@ def main():
argument_spec=dict( argument_spec=dict(
dest=dict(default='/etc/network/interfaces', required=False, type='path'), dest=dict(default='/etc/network/interfaces', required=False, type='path'),
iface=dict(required=False), iface=dict(required=False),
address_family=dict(required=False),
option=dict(required=False), option=dict(required=False),
value=dict(required=False), value=dict(required=False),
backup=dict(default='no', type='bool'), backup=dict(default='no', type='bool'),
@ -354,6 +364,7 @@ def main():
dest = module.params['dest'] dest = module.params['dest']
iface = module.params['iface'] iface = module.params['iface']
address_family = module.params['address_family']
option = module.params['option'] option = module.params['option']
value = module.params['value'] value = module.params['value']
backup = module.params['backup'] backup = module.params['backup']
@ -370,7 +381,7 @@ def main():
changed = False changed = False
if option is not None: if option is not None:
changed, lines = setInterfaceOption(module, lines, iface, option, value, state) changed, lines = setInterfaceOption(module, lines, iface, option, value, state, address_family)
if changed: if changed:
_, ifaces = read_interfaces_lines(module, [d['line'] for d in lines if 'line' in d]) _, ifaces = read_interfaces_lines(module, [d['line'] for d in lines if 'line' in d])

Loading…
Cancel
Save