diff --git a/changelogs/fragments/4062-nmcli-ipv6-routes-support.yml b/changelogs/fragments/4062-nmcli-ipv6-routes-support.yml new file mode 100644 index 0000000000..38378d56f8 --- /dev/null +++ b/changelogs/fragments/4062-nmcli-ipv6-routes-support.yml @@ -0,0 +1,2 @@ +minor_changes: + - nmcli - adds ``routes6`` and ``route_metric6`` parameters for supporting IPv6 routes (https://github.com/ansible-collections/community.general/issues/4059). diff --git a/plugins/modules/net_tools/nmcli.py b/plugins/modules/net_tools/nmcli.py index de00a1ea10..a9c08b4691 100644 --- a/plugins/modules/net_tools/nmcli.py +++ b/plugins/modules/net_tools/nmcli.py @@ -161,6 +161,18 @@ options: type: bool default: false version_added: 3.2.0 + routes6: + description: + - The list of IPv6 routes. + - Use the format C(fd12:3456:789a:1::/64 2001:dead:beef::1). + type: list + elements: str + version_added: 4.4.0 + route_metric6: + description: + - Set metric level of IPv6 routes configured on interface. + type: int + version_added: 4.4.0 dns6: description: - A list of up to 3 dns servers. @@ -1259,6 +1271,8 @@ class Nmcli(object): self.ip6 = module.params['ip6'] self.gw6 = module.params['gw6'] self.gw6_ignore_auto = module.params['gw6_ignore_auto'] + self.routes6 = module.params['routes6'] + self.route_metric6 = module.params['route_metric6'] self.dns6 = module.params['dns6'] self.dns6_search = module.params['dns6_search'] self.dns6_ignore_auto = module.params['dns6_ignore_auto'] @@ -1369,6 +1383,8 @@ class Nmcli(object): 'ipv6.ignore-auto-dns': self.dns6_ignore_auto, 'ipv6.gateway': self.gw6, 'ipv6.ignore-auto-routes': self.gw6_ignore_auto, + 'ipv6.routes': self.routes6, + 'ipv6.route-metric': self.route_metric6, 'ipv6.method': self.ipv6_method, 'ipv6.ip6-privacy': self.ip_privacy6, 'ipv6.addr-gen-mode': self.addr_gen_mode6 @@ -1628,6 +1644,7 @@ class Nmcli(object): 'ipv4.routing-rules', 'ipv6.dns', 'ipv6.dns-search', + 'ipv6.routes', '802-11-wireless-security.group', '802-11-wireless-security.leap-password-flags', '802-11-wireless-security.pairwise', @@ -1753,7 +1770,7 @@ class Nmcli(object): alias_key = alias_pair[0] alias_value = alias_pair[1] conn_info[alias_key] = alias_value - elif key == 'ipv4.routes': + elif key in ('ipv4.routes', 'ipv6.routes'): conn_info[key] = [s.strip() for s in raw_value.split(';')] elif key_type == list: conn_info[key] = [s.strip() for s in raw_value.split(',')] @@ -1832,8 +1849,8 @@ class Nmcli(object): if key in conn_info: current_value = conn_info[key] - if key == 'ipv4.routes' and current_value is not None: - # ipv4.routes do not have same options and show_connection() format + if key in ('ipv4.routes', 'ipv6.routes') and current_value is not None: + # ipv4.routes and ipv6.routes do not have same options and show_connection() format # options: ['10.11.0.0/24 10.10.0.2', '10.12.0.0/24 10.10.0.2 200'] # show_connection(): ['{ ip = 10.11.0.0/24, nh = 10.10.0.2 }', '{ ip = 10.12.0.0/24, nh = 10.10.0.2, mt = 200 }'] # Need to convert in order to compare both @@ -1932,6 +1949,8 @@ def main(): dns6=dict(type='list', elements='str'), dns6_search=dict(type='list', elements='str'), dns6_ignore_auto=dict(type='bool', default=False), + routes6=dict(type='list', elements='str'), + route_metric6=dict(type='int'), method6=dict(type='str', choices=['ignore', 'auto', 'dhcp', 'link-local', 'manual', 'shared', 'disabled']), ip_privacy6=dict(type='str', choices=['disabled', 'prefer-public-addr', 'prefer-temp-addr', 'unknown']), addr_gen_mode6=dict(type='str', choices=['eui64', 'stable-privacy']), diff --git a/tests/unit/plugins/modules/net_tools/test_nmcli.py b/tests/unit/plugins/modules/net_tools/test_nmcli.py index ead1551729..73b828fad4 100644 --- a/tests/unit/plugins/modules/net_tools/test_nmcli.py +++ b/tests/unit/plugins/modules/net_tools/test_nmcli.py @@ -158,6 +158,129 @@ ipv6.ignore-auto-dns: no ipv6.ignore-auto-routes: no """ +TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE = [ + { + 'type': 'ethernet', + 'conn_name': 'non_existent_nw_device', + 'ifname': 'ethernet_non_existant', + 'ip6': '2001:beef:cafe:10::1/64', + 'routes6': ['fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2'], + 'method6': 'manual', + 'state': 'present', + '_ansible_check_mode': False, + }, +] + +TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE_SHOW_OUTPUT = """\ +connection.id: non_existent_nw_device +connection.interface-name: ethernet_non_existant +connection.autoconnect: yes +ipv4.method: auto +ipv4.ignore-auto-dns: no +ipv4.ignore-auto-routes: no +ipv4.never-default: no +ipv4.may-fail: yes +ipv6.method: manual +ipv6.addresses: 2001:beef:cafe:10::1/64 +ipv6.routes: { ip = fd2e:446f:d85d:5::/64, nh = 2001:beef:cafe:10::2 } +ipv6.route-metric: -1 +ipv6.ignore-auto-dns: no +ipv6.ignore-auto-routes: no +""" + +TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES = [ + { + 'type': 'ethernet', + 'conn_name': 'non_existent_nw_device', + 'ifname': 'ethernet_non_existant', + 'ip6': '2001:beef:cafe:10::1/64', + 'routes6': ['fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2', 'fd2e:8890:abcd:25::/64 2001:beef:cafe:10::5'], + 'method6': 'manual', + 'state': 'present', + '_ansible_check_mode': False, + }, +] + +TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES_SHOW_OUTPUT = """\ +connection.id: non_existent_nw_device +connection.interface-name: ethernet_non_existant +connection.autoconnect: yes +ipv4.method: auto +ipv4.ignore-auto-dns: no +ipv4.ignore-auto-routes: no +ipv4.never-default: no +ipv4.may-fail: yes +ipv6.method: manual +ipv6.addresses: 2001:beef:cafe:10::1/64 +ipv6.routes: { ip = fd2e:446f:d85d:5::/64, nh = 2001:beef:cafe:10::2 }; { ip = fd2e:8890:abcd:25::/64, nh = 2001:beef:cafe:10::5 } +ipv6.route-metric: -1 +ipv6.ignore-auto-dns: no +ipv6.ignore-auto-routes: no +""" + +TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE_AND_METRIC = [ + { + 'type': 'ethernet', + 'conn_name': 'non_existent_nw_device', + 'ifname': 'ethernet_non_existant', + 'method4': 'disabled', + 'ip6': '2001:beef:cafe:10::1/64', + 'routes6': ['fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2'], + 'route_metric6': 5, + 'method6': 'manual', + 'state': 'present', + '_ansible_check_mode': False, + }, +] + +TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE_AND_METRIC_SHOW_OUTPUT = """\ +connection.id: non_existent_nw_device +connection.interface-name: ethernet_non_existant +connection.autoconnect: yes +ipv4.method: auto +ipv4.ignore-auto-dns: no +ipv4.ignore-auto-routes: no +ipv4.never-default: no +ipv4.may-fail: yes +ipv6.method: manual +ipv6.addresses: 2001:beef:cafe:10::1/64 +ipv6.routes: { ip = fd2e:446f:d85d:5::/64, nh = 2001:beef:cafe:10::2 } +ipv6.route-metric: 5 +ipv6.ignore-auto-dns: no +ipv6.ignore-auto-routes: no +""" + +TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES_AND_METRIC = [ + { + 'type': 'ethernet', + 'conn_name': 'non_existent_nw_device', + 'ifname': 'ethernet_non_existant', + 'method4': 'disabled', + 'ip6': '2001:beef:cafe:10::1/64', + 'routes6': ['fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2', 'fd2e:8890:abcd:25::/64 2001:beef:cafe:10::5'], + 'route_metric6': 5, + 'method6': 'manual', + 'state': 'present', + '_ansible_check_mode': False, + }, +] + +TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES_AND_METRIC_SHOW_OUTPUT = """\ +connection.id: non_existent_nw_device +connection.interface-name: ethernet_non_existant +connection.autoconnect: yes +ipv4.method: auto +ipv4.ignore-auto-dns: no +ipv4.ignore-auto-routes: no +ipv4.never-default: no +ipv4.may-fail: yes +ipv6.method: manual +ipv6.addresses: 2001:beef:cafe:10::1/64 +ipv6.routes: { ip = fd2e:446f:d85d:5::/64, nh = 2001:beef:cafe:10::2 }; { ip = fd2e:8890:abcd:25::/64, nh = 2001:beef:cafe:10::5 } +ipv6.route-metric: 5 +ipv6.ignore-auto-dns: no +ipv6.ignore-auto-routes: no +""" TESTCASE_GENERIC_DNS4_SEARCH = [ { @@ -1140,6 +1263,46 @@ def mocked_ethernet_connection_static_modify(mocker): )) +@pytest.fixture +def mocked_ethernet_connection_with_ipv6_static_address_static_route_create(mocker): + mocker_set(mocker, + execute_return=None, + execute_side_effect=( + (0, TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE_SHOW_OUTPUT, ""), + (0, "", ""), + )) + + +@pytest.fixture +def mocked_ethernet_connection_with_ipv6_static_address_multiple_static_routes_create(mocker): + mocker_set(mocker, + execute_return=None, + execute_side_effect=( + (0, TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES_SHOW_OUTPUT, ""), + (0, "", ""), + )) + + +@pytest.fixture +def mocked_ethernet_connection_with_ipv6_static_address_static_route_with_metric_create(mocker): + mocker_set(mocker, + execute_return=None, + execute_side_effect=( + (0, TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE_AND_METRIC_SHOW_OUTPUT, ""), + (0, "", ""), + )) + + +@pytest.fixture +def mocked_ethernet_connection_with_ipv6_static_address_multiple_static_routes_with_metric_create(mocker): + mocker_set(mocker, + execute_return=None, + execute_side_effect=( + (0, TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES_AND_METRIC_SHOW_OUTPUT, ""), + (0, "", ""), + )) + + @pytest.fixture def mocked_ethernet_connection_with_ipv6_address_static_modify(mocker): mocker_set(mocker, @@ -2261,6 +2424,150 @@ def test_ethernet_connection_static_unchanged(mocked_ethernet_connection_static_ assert not results['changed'] +@pytest.mark.parametrize('patch_ansible_module', TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE, indirect=['patch_ansible_module']) +def test_ethernet_connection_static_ipv6_address_static_route_create(mocked_ethernet_connection_with_ipv6_static_address_static_route_create, capfd): + """ + Test : Create ethernet connection with static IPv6 address and static route + """ + with pytest.raises(SystemExit): + nmcli.main() + + assert nmcli.Nmcli.execute_command.call_count == 1 + arg_list = nmcli.Nmcli.execute_command.call_args_list + add_args, add_kw = arg_list[0] + + assert add_args[0][0] == '/usr/bin/nmcli' + assert add_args[0][1] == 'con' + assert add_args[0][2] == 'add' + assert add_args[0][3] == 'type' + assert add_args[0][4] == 'ethernet' + assert add_args[0][5] == 'con-name' + assert add_args[0][6] == 'non_existent_nw_device' + + add_args_text = list(map(to_text, add_args[0])) + + for param in ['connection.interface-name', 'ethernet_non_existant', + 'con-name', 'non_existent_nw_device', + 'ipv6.addresses', '2001:beef:cafe:10::1/64', + 'ipv6.method', 'manual', + 'ipv6.routes', 'fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2']: + assert param in add_args_text + + out, err = capfd.readouterr() + results = json.loads(out) + assert not results.get('failed') + assert results['changed'] + + +@pytest.mark.parametrize('patch_ansible_module', TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES, indirect=['patch_ansible_module']) +def test_ethernet_connection_static_ipv6_address_multiple_static_routes_with_metric_create( + mocked_ethernet_connection_with_ipv6_static_address_multiple_static_routes_with_metric_create, capfd): + """ + Test : Create ethernet connection with static IPv6 address and multiple static routes + """ + with pytest.raises(SystemExit): + nmcli.main() + + assert nmcli.Nmcli.execute_command.call_count == 1 + arg_list = nmcli.Nmcli.execute_command.call_args_list + add_args, add_kw = arg_list[0] + + assert add_args[0][0] == '/usr/bin/nmcli' + assert add_args[0][1] == 'con' + assert add_args[0][2] == 'add' + assert add_args[0][3] == 'type' + assert add_args[0][4] == 'ethernet' + assert add_args[0][5] == 'con-name' + assert add_args[0][6] == 'non_existent_nw_device' + + add_args_text = list(map(to_text, add_args[0])) + + for param in ['connection.interface-name', 'ethernet_non_existant', + 'con-name', 'non_existent_nw_device', + 'ipv6.addresses', '2001:beef:cafe:10::1/64', + 'ipv6.method', 'manual', + 'ipv6.routes', 'fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2,fd2e:8890:abcd:25::/64 2001:beef:cafe:10::5']: + assert param in add_args_text + + out, err = capfd.readouterr() + results = json.loads(out) + assert not results.get('failed') + assert results['changed'] + + +@pytest.mark.parametrize('patch_ansible_module', TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_ROUTE_AND_METRIC, indirect=['patch_ansible_module']) +def test_ethernet_connection_static_ipv6_address_static_route_with_metric_create( + mocked_ethernet_connection_with_ipv6_static_address_static_route_with_metric_create, capfd): + """ + Test : Create ethernet connection with static IPv6 address and static route with metric + """ + with pytest.raises(SystemExit): + nmcli.main() + + assert nmcli.Nmcli.execute_command.call_count == 1 + arg_list = nmcli.Nmcli.execute_command.call_args_list + add_args, add_kw = arg_list[0] + + assert add_args[0][0] == '/usr/bin/nmcli' + assert add_args[0][1] == 'con' + assert add_args[0][2] == 'add' + assert add_args[0][3] == 'type' + assert add_args[0][4] == 'ethernet' + assert add_args[0][5] == 'con-name' + assert add_args[0][6] == 'non_existent_nw_device' + + add_args_text = list(map(to_text, add_args[0])) + + for param in ['connection.interface-name', 'ethernet_non_existant', + 'con-name', 'non_existent_nw_device', + 'ipv6.addresses', '2001:beef:cafe:10::1/64', + 'ipv6.method', 'manual', + 'ipv6.routes', 'fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2', + 'ipv6.route-metric', '5']: + assert param in add_args_text + + out, err = capfd.readouterr() + results = json.loads(out) + assert not results.get('failed') + assert results['changed'] + + +@pytest.mark.parametrize('patch_ansible_module', TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES_AND_METRIC, indirect=['patch_ansible_module']) +def test_ethernet_connection_static_ipv6_address_static_route_create(mocked_ethernet_connection_with_ipv6_static_address_static_route_create, capfd): + """ + Test : Create ethernet connection with static IPv6 address and multiple static routes with metric + """ + with pytest.raises(SystemExit): + nmcli.main() + + assert nmcli.Nmcli.execute_command.call_count == 1 + arg_list = nmcli.Nmcli.execute_command.call_args_list + add_args, add_kw = arg_list[0] + + assert add_args[0][0] == '/usr/bin/nmcli' + assert add_args[0][1] == 'con' + assert add_args[0][2] == 'add' + assert add_args[0][3] == 'type' + assert add_args[0][4] == 'ethernet' + assert add_args[0][5] == 'con-name' + assert add_args[0][6] == 'non_existent_nw_device' + + add_args_text = list(map(to_text, add_args[0])) + + for param in ['connection.interface-name', 'ethernet_non_existant', + 'con-name', 'non_existent_nw_device', + 'ipv6.addresses', '2001:beef:cafe:10::1/64', + 'ipv6.method', 'manual', + 'ipv6.routes', 'fd2e:446f:d85d:5::/64 2001:beef:cafe:10::2,fd2e:8890:abcd:25::/64 2001:beef:cafe:10::5', + 'ipv6.route-metric', '5']: + assert param in add_args_text + + out, err = capfd.readouterr() + results = json.loads(out) + assert not results.get('failed') + assert results['changed'] + + @pytest.mark.parametrize('patch_ansible_module', TESTCASE_WIRELESS, indirect=['patch_ansible_module']) def test_create_wireless(mocked_wireless_create, capfd): """