From fb72a5424cab097d28670d00cf107fd515cee3f0 Mon Sep 17 00:00:00 2001 From: dangoscomb Date: Thu, 11 Oct 2018 12:52:18 +0100 Subject: [PATCH] nmcli: fix vlan connection modification Fixes #42322 (#42415) * ensure optional items are set to empty strings rather than not presented fix syntax of vlan modification command * extended tests for nmcli --- lib/ansible/modules/net_tools/nmcli.py | 28 +++++++++++++--------- test/units/modules/net_tools/test_nmcli.py | 17 +++++++++++-- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/ansible/modules/net_tools/nmcli.py b/lib/ansible/modules/net_tools/nmcli.py index d828e1b6873..5e953a54a35 100644 --- a/lib/ansible/modules/net_tools/nmcli.py +++ b/lib/ansible/modules/net_tools/nmcli.py @@ -1033,10 +1033,10 @@ class Nmcli(object): params = {'dev': self.vlandev, 'id': self.vlanid, - 'ip4': self.ip4, - 'gw4': self.gw4, - 'ip6': self.ip6, - 'gw6': self.gw6, + 'ip4': self.ip4 or '', + 'gw4': self.gw4 or '', + 'ip6': self.ip6 or '', + 'gw6': self.gw6 or '', 'autoconnect': self.bool_to_string(self.autoconnect) } for k, v in params.items(): @@ -1048,16 +1048,22 @@ class Nmcli(object): cmd = [self.nmcli_bin] cmd.append('con') cmd.append('mod') - cmd.append('con-name') + + if self.conn_name is not None: + cmd.append(self.conn_name) + elif self.ifname is not None: + cmd.append(self.ifname) + else: + cmd.append('vlan%s' % self.vlanid) params = {'vlan.parent': self.vlandev, 'vlan.id': self.vlanid, - 'ipv4.address': self.ip4, - 'ipv4.gateway': self.gw4, - 'ipv4.dns': self.dns4, - 'ipv6.address': self.ip6, - 'ipv6.gateway': self.gw6, - 'ipv6.dns': self.dns6, + 'ipv4.address': self.ip4 or '', + 'ipv4.gateway': self.gw4 or '', + 'ipv4.dns': self.dns4 or '', + 'ipv6.address': self.ip6 or '', + 'ipv6.gateway': self.gw6 or '', + 'ipv6.dns': self.dns6 or '', 'autoconnect': self.bool_to_string(self.autoconnect) } diff --git a/test/units/modules/net_tools/test_nmcli.py b/test/units/modules/net_tools/test_nmcli.py index 42492d88a7f..86713226d14 100644 --- a/test/units/modules/net_tools/test_nmcli.py +++ b/test/units/modules/net_tools/test_nmcli.py @@ -402,7 +402,15 @@ def test_create_vlan_con(mocked_generic_connection_create): arg_list = nmcli.Nmcli.execute_command.call_args_list args, kwargs = arg_list[0] - for param in ['vlan']: + assert args[0][0] == '/usr/bin/nmcli' + assert args[0][1] == 'con' + assert args[0][2] == 'add' + assert args[0][3] == 'type' + assert args[0][4] == 'vlan' + assert args[0][5] == 'con-name' + assert args[0][6] == 'non_existent_nw_device' + + for param in ['ip4', '10.10.10.10', 'gw4', '10.10.10.1']: assert param in args[0] @@ -419,7 +427,12 @@ def test_mod_vlan_conn(mocked_generic_connection_modify): arg_list = nmcli.Nmcli.execute_command.call_args_list args, kwargs = arg_list[0] - for param in ['vlan.id']: + assert args[0][0] == '/usr/bin/nmcli' + assert args[0][1] == 'con' + assert args[0][2] == 'mod' + assert args[0][3] == 'non_existent_nw_device' + + for param in ['ipv4.address', '10.10.10.10', 'ipv4.gateway', '10.10.10.1']: assert param in args[0]