Repository: ansible/ansible

Issue:
nmcli modify_connection_vlan() function seems broken
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
<!--- Explain the problem briefly -->
The code in the above function generates an invalid nmcli command.

The code is still the same in devel, as below. As you can see, 'con-name' should not be specified for a connection modification, and also nowhere in this function is the name of the connection actually put in to the command (so it will never work).

Produced command (caught in strace, not complete):
```
8155  execve("/usr/bin/nmcli", ["/usr/bin/nmcli", "con", "mod", "con-name", "autoconnect", "yes", "vlan.id", "299", "ipv4.geteway", "vlan.parent", "eno1", "ipv4.address", "10.250.10.68/28", "ipv6.dns", "ipv6.address", "ipv4.dns", "ipv6.gateway"], [/* 30 vars */] <unfinished ...>
```

Code:
```
    def modify_connection_vlan(self):
        cmd = [self.nmcli_bin]
        cmd.append('con')
        cmd.append('mod')
        cmd.append('con-name')

        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,
                  'autoconnect': self.bool_to_string(self.autoconnect)
                  }

        for k, v in params.items():
            cmd.extend([k, v])

        return cmd
```

##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
nmcli

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.5
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->
CACHE_PLUGIN(/etc/ansible/ansible.cfg) = memory
DEFAULT_ACTION_PLUGIN_PATH(/etc/ansible/ansible.cfg) = [u'/usr/share/ansible_plugins/action_plugins']
DEFAULT_CALLBACK_PLUGIN_PATH(/etc/ansible/ansible.cfg) = [u'/usr/share/ansible_plugins/callback_plugins']
DEFAULT_CONNECTION_PLUGIN_PATH(/etc/ansible/ansible.cfg) = [u'/usr/share/ansible_plugins/connection_plugins']
DEFAULT_FILTER_PLUGIN_PATH(/etc/ansible/ansible.cfg) = [u'/usr/share/ansible_plugins/filter_plugins']
DEFAULT_FORKS(/etc/ansible/ansible.cfg) = 5
DEFAULT_GATHERING(/etc/ansible/ansible.cfg) = implicit
DEFAULT_HOST_LIST(/etc/ansible/ansible.cfg) = [u'/home/ansible/hosts']
DEFAULT_LOOKUP_PLUGIN_PATH(/etc/ansible/ansible.cfg) = [u'/usr/share/ansible_plugins/lookup_plugins']
DEFAULT_MANAGED_STR(/etc/ansible/ansible.cfg) = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S by {uid} on {host}
DEFAULT_MODULE_LANG(/etc/ansible/ansible.cfg) = C
DEFAULT_POLL_INTERVAL(/etc/ansible/ansible.cfg) = 15
DEFAULT_ROLES_PATH(/etc/ansible/ansible.cfg) = [u'/home/ansible/roles']
DEFAULT_SUDO_EXE(/etc/ansible/ansible.cfg) = sudo
DEFAULT_TIMEOUT(/etc/ansible/ansible.cfg) = 10
DEFAULT_TRANSPORT(/etc/ansible/ansible.cfg) = smart
DEFAULT_VARS_PLUGIN_PATH(/etc/ansible/ansible.cfg) = [u'/usr/share/ansible_plugins/vars_plugins']
HOST_KEY_CHECKING(/etc/ansible/ansible.cfg) = False
RETRY_FILES_ENABLED(/etc/ansible/ansible.cfg) = False
##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->

##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
        - name: create vlan interface
          nmcli:
            state: present
            vlandev: eno1
            vlanid: '{{ item.vlan }}'
            type: vlan
            ip4: '{{ item.the_ip }}/{{ item.prefix }}'
            conn_name: "vlan{{ item.vlan }}"
            ifname: "vlan{{ item.vlan }}"
            autoconnect: yes
          when: item.the_ip is defined
          with_items: "{{ interfaceList }}"
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
vlan interface that already exists to be untouched (or modified with new information)

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
error, due to the fact that the function creates an invalid command to nmcli

<!--- Paste verbatim command output between quotes below -->
```
failed: [zproxy00] (item={u'the_ip': u'10.253.1.229', u'prefix': 27, u'vlan': 539, u'hex_ip': u'10.253.1.228'}) => {"changed": false, "item": {"hex_ip": "10.253.1.228", "prefix": 27, "the_ip": "10.253.1.229", "vlan": 539}, "msg": "Error: unknown connection 'con-name'.\n", "name": "vlan539", "rc": 10}
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/net_tools/nmcli.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/net_tools/nmcli.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @alcamie101
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/net_tools/nmcli.py b/lib/ansible/modules/net_tools/nmcli.py
--- a/lib/ansible/modules/net_tools/nmcli.py
+++ b/lib/ansible/modules/net_tools/nmcli.py
@@ -1033,10 +1033,10 @@ def create_connection_vlan(self):
 
         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 @@ def modify_connection_vlan(self):
         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)
                   }
 


Test patch:
diff --git a/test/units/modules/net_tools/test_nmcli.py b/test/units/modules/net_tools/test_nmcli.py
--- 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]