vyos_l3_interface fix (#36721)

* vyos_l3_interface fix

Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>

* update test and changelog

Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>
pull/36952/head
Trishna Guha 8 years ago committed by GitHub
parent 2d05ecb26c
commit 92cea82a4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -82,6 +82,8 @@ Ansible Changes By Release
https://github.com/ansible/ansible/pull/36541
* Fix vyos_config IndexError in sanitize_config
(https://github.com/ansible/ansible/issues/36351)
* Fix vyos_l3_interface multiple address assignment to interfaces
(https://github.com/ansible/ansible/pull/36721)
<a id="2.4.3"></a>

@ -87,6 +87,10 @@ commands:
sample:
- set interfaces ethernet eth0 address '192.168.0.1/24'
"""
import socket
import re
from copy import deepcopy
from ansible.module_utils.basic import AnsibleModule
@ -95,6 +99,41 @@ from ansible.module_utils.vyos import load_config, run_commands
from ansible.module_utils.vyos import vyos_argument_spec, check_args
def is_masklen(val):
try:
return 0 <= int(val) <= 32
except ValueError:
return False
def validate_ip_address(address):
try:
socket.inet_aton(address)
except socket.error:
return False
return address.count('.') == 3
def is_ipv4(value):
if value:
address = value.split('/')
if is_masklen(address[1]) and validate_ip_address(address[0]):
return True
return False
def is_ipv6(value):
if value:
address = value.split('/')
if 0 <= int(address[1]) <= 128:
try:
socket.inet_pton(socket.AF_INET6, address[0])
except socket.error:
return False
return True
return False
def search_obj_in_list(name, lst):
for o in lst:
if o['name'] == name:
@ -118,16 +157,16 @@ def map_obj_to_commands(updates, module):
if not ipv4 and not ipv6 and (obj_in_have['ipv4'] or obj_in_have['ipv6']):
commands.append('delete interfaces ethernet ' + name + ' address')
else:
if ipv4 and obj_in_have['ipv4']:
if ipv4 and ipv4 in obj_in_have['ipv4']:
commands.append('delete interfaces ethernet ' + name + ' address ' + ipv4)
if ipv6 and obj_in_have['ipv6']:
if ipv6 and ipv6 in obj_in_have['ipv6']:
commands.append('delete interfaces ethernet ' + name + ' address ' + ipv6)
elif (state == 'present' and obj_in_have):
if ipv4 and ipv4 != obj_in_have['ipv4']:
if ipv4 and ipv4 not in obj_in_have['ipv4']:
commands.append('set interfaces ethernet ' + name + ' address ' +
ipv4)
if ipv6 and ipv6 != obj_in_have['ipv6']:
if ipv6 and ipv6 not in obj_in_have['ipv6']:
commands.append('set interfaces ethernet ' + name + ' address ' +
ipv6)
@ -137,29 +176,30 @@ def map_obj_to_commands(updates, module):
def map_config_to_obj(module):
obj = []
output = run_commands(module, ['show interfaces ethernet'])
lines = output[0].splitlines()
lines = re.split(r'\ne', output[0])[1:]
if len(lines) > 3:
for line in lines[3:]:
if len(lines) > 0:
for line in lines:
splitted_line = line.split()
if len(splitted_line) > 1:
name = splitted_line[0]
address = splitted_line[1]
if address == '-':
address = None
if address is not None and ':' not in address:
obj.append({'name': name,
'ipv4': address,
'ipv6': None})
else:
obj.append({'name': name,
'ipv6': address,
'ipv4': None})
else:
obj[-1]['ipv6'] = splitted_line[0]
if len(splitted_line) > 0:
ipv4 = []
ipv6 = []
if splitted_line[0].lower().startswith('th'):
name = 'e' + splitted_line[0].lower()
for i in splitted_line[1:]:
if (('.' in i or ':' in i) and '/' in i):
value = i.split(r'\n')[0]
if is_ipv4(value):
ipv4.append(value)
elif is_ipv6(value):
ipv6.append(value)
obj.append({'name': name,
'ipv4': ipv4,
'ipv6': ipv6})
return obj

@ -147,6 +147,7 @@
aggregate:
- { name: eth1, ipv4: 192.168.2.10/24 }
- { name: eth2, ipv4: 192.168.3.10/24, ipv6: "fd5d:12c9:2201:1::1/64" }
- { name: eth2, ipv4: 192.168.4.10/24 }
register: result
- assert:
@ -155,12 +156,14 @@
- '"set interfaces ethernet eth1 address 192.168.2.10/24" in result.commands'
- '"set interfaces ethernet eth2 address 192.168.3.10/24" in result.commands'
- '"set interfaces ethernet eth2 address fd5d:12c9:2201:1::1/64" in result.commands'
- '"set interfaces ethernet eth2 address 192.168.4.10/24" in result.commands'
- name: Set IP addresses on aggregate (idempotent)
vyos_l3_interface:
aggregate:
- { name: eth1, ipv4: 192.168.2.10/24 }
- { name: eth2, ipv4: 192.168.3.10/24, ipv6: "fd5d:12c9:2201:1::1/64" }
- { name: eth2, ipv4: 192.168.4.10/24 }
register: result
- assert:
@ -172,6 +175,7 @@
aggregate:
- { name: eth1, ipv4: 192.168.2.10/24 }
- { name: eth2, ipv4: 192.168.3.10/24, ipv6: "fd5d:12c9:2201:1::1/64" }
- { name: eth2, ipv4: 192.168.4.10/24 }
state: absent
register: result
@ -181,12 +185,14 @@
- '"delete interfaces ethernet eth1 address 192.168.2.10/24" in result.commands'
- '"delete interfaces ethernet eth2 address 192.168.3.10/24" in result.commands'
- '"delete interfaces ethernet eth2 address fd5d:12c9:2201:1::1/64" in result.commands'
- '"delete interfaces ethernet eth2 address 192.168.4.10/24" in result.commands'
- name: Remove IP addresses on aggregate (idempotent)
vyos_l3_interface:
aggregate:
- { name: eth1, ipv4: 192.168.2.10/24 }
- { name: eth2, ipv4: 192.168.3.10/24, ipv6: "fd5d:12c9:2201:1::1/64" }
- { name: eth2, ipv4: 192.168.4.10/24 }
state: absent
register: result

Loading…
Cancel
Save