Repository: ansible/ansible

Issue:
Vyos interface throws a traceback in check mode
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
 - Bug Report

##### COMPONENT NAME
<!---
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
-->
- vyos_interface

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.0 (ansible-test-check-mode 9909474852) last updated 2018/02/01 13:28:27 (GMT -400)
  config file = None
  configured module search path = ['/Users/david/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/david/code/ansible/lib/ansible
  executable location = /Users/david/code/ansible/bin/ansible
  python version = 3.6.4 (default, Jan  6 2018, 11:51:59) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste 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).
-->

##### OS / ENVIRONMENT
<!---
Mention 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.
-->

##### SUMMARY
<!--- Explain the problem briefly -->

##### 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.
-->

Run the following playbook with the --check flag.
<!--- Paste example playbooks or commands between quotes below -->
```yaml
- hosts: vyos
  gather_facts: no
  connection: network_cli

  vars:
    limit_to: "*"
    debug: false

  tasks:
  - name: Setup (interface is up)
    vyos_interface:
      name: eth1
      enabled: True
      state: present
    register: result

  - name: Check intent arguments
    vyos_interface:
      name: eth1
      state: up
    register: result

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Playbook should run without errors.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
TASK [Check intent arguments] ************************************************************************************************************************
task path: /Users/david/code/ansible/#david_stuff/vyos.yaml:20
<vyos02.example.net> attempting to start connection
<vyos02.example.net> using connection plugin network_cli
<vyos02.example.net> found existing local domain socket, using it!
<vyos02.example.net> updating play_context for connection
<vyos02.example.net>
<vyos02.example.net> local domain socket path is /Users/david/.ansible/pc/af7ff55793
Using module file /Users/david/code/ansible/lib/ansible/modules/network/vyos/vyos_interface.py
<vyos02.example.net> ESTABLISH LOCAL CONNECTION FOR USER: david
<vyos02.example.net> EXEC /bin/sh -c 'echo ~ && sleep 0'
<vyos02.example.net> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/david/.ansible/tmp/ansible-tmp-1517516417.19518-22304825954046 `" && echo ansible-tmp-1517516417.19518-22304825954046="` echo /Users/david/.ansible/tmp/ansible-tmp-1517516417.19518-22304825954046 `" ) && sleep 0'
<vyos02.example.net> PUT /Users/david/.ansible/tmp/ansible-local-210906dn2dlh1/tmpu8uvlnd0 TO /Users/david/.ansible/tmp/ansible-tmp-1517516417.19518-22304825954046/vyos_interface.py
<vyos02.example.net> EXEC /bin/sh -c 'chmod u+x /Users/david/.ansible/tmp/ansible-tmp-1517516417.19518-22304825954046/ /Users/david/.ansible/tmp/ansible-tmp-1517516417.19518-22304825954046/vyos_interface.py && sleep 0'
<vyos02.example.net> EXEC /bin/sh -c '/usr/bin/python /Users/david/.ansible/tmp/ansible-tmp-1517516417.19518-22304825954046/vyos_interface.py && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_Mxw7Xt/ansible_module_vyos_interface.py", line 434, in <module>
    main()
  File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_Mxw7Xt/ansible_module_vyos_interface.py", line 420, in main
    diff = load_config(module, commands, commit=commit)
  File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_Mxw7Xt/ansible_modlib.zip/ansible/module_utils/network/vyos/vyos.py", line 132, in load_config
  File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_Mxw7Xt/ansible_modlib.zip/ansible/module_utils/connection.py", line 146, in __rpc__
ansible.module_utils.connection.ConnectionError: configure

  Invalid command: [configure]

[edit]
ansible@vyos02#

fatal: [vyos02.example.net]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_Mxw7Xt/ansible_module_vyos_interface.py\", line 434, in <module>\n    main()\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_Mxw7Xt/ansible_module_vyos_interface.py\", line 420, in main\n    diff = load_config(module, commands, commit=commit)\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_Mxw7Xt/ansible_modlib.zip/ansible/module_utils/network/vyos/vyos.py\", line 132, in load_config\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_Mxw7Xt/ansible_modlib.zip/ansible/module_utils/connection.py\", line 146, in __rpc__\nansible.module_utils.connection.ConnectionError: configure\r\n\r\n  Invalid command: [configure]\r\n\r\n[edit]\r\r\nansible@vyos02# \n",
    "module_stdout": "",
    "msg": "MODULE FAILURE",
    "rc": 1
}
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/network/vyos/vyos_interface.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/network/vyos/vyos_interface.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 @Qalthos @ganeshrn
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Once fixed https://github.com/ansible/ansible/pull/35788 needs reverting

Resolving patch:
diff --git a/lib/ansible/module_utils/network/vyos/vyos.py b/lib/ansible/module_utils/network/vyos/vyos.py
--- a/lib/ansible/module_utils/network/vyos/vyos.py
+++ b/lib/ansible/module_utils/network/vyos/vyos.py
@@ -29,7 +29,7 @@
 from ansible.module_utils._text import to_text
 from ansible.module_utils.basic import env_fallback, return_values
 from ansible.module_utils.network.common.utils import to_list
-from ansible.module_utils.connection import Connection
+from ansible.module_utils.connection import Connection, ConnectionError
 
 _DEVICE_CONFIGS = {}
 
@@ -143,14 +143,13 @@ def load_config(module, commands, commit=False, comment=None):
     if commit:
         try:
             out = connection.commit(comment)
-        except:
+        except ConnectionError:
             connection.discard_changes()
             module.fail_json(msg='commit failed: %s' % out)
-
-    if not commit:
-        connection.discard_changes()
+        else:
+            connection.get('exit')
     else:
-        connection.get('exit')
+        connection.discard_changes()
 
     if diff:
         return diff
diff --git a/lib/ansible/plugins/cliconf/vyos.py b/lib/ansible/plugins/cliconf/vyos.py
--- a/lib/ansible/plugins/cliconf/vyos.py
+++ b/lib/ansible/plugins/cliconf/vyos.py
@@ -26,7 +26,7 @@
 
 from ansible.module_utils._text import to_bytes, to_text
 from ansible.module_utils.network.common.utils import to_list
-from ansible.plugins.cliconf import CliconfBase, enable_mode
+from ansible.plugins.cliconf import CliconfBase
 
 
 class Cliconf(CliconfBase):
@@ -69,7 +69,7 @@ def commit(self, comment=None):
         self.send_command(to_bytes(command))
 
     def discard_changes(self, *args, **kwargs):
-        self.send_command(b'discard')
+        self.send_command(b'exit discard')
 
     def get_capabilities(self):
         result = {}


Test patch:
diff --git a/test/integration/targets/vyos_smoke/tests/cli/misc_tests.yaml b/test/integration/targets/vyos_smoke/tests/cli/misc_tests.yaml
--- a/test/integration/targets/vyos_smoke/tests/cli/misc_tests.yaml
+++ b/test/integration/targets/vyos_smoke/tests/cli/misc_tests.yaml
@@ -1,13 +1,13 @@
 # hit check conditional in module_utils.network.vyos -> load_config()
-# - name: configure simple config command
-#   vyos_config:
-#     lines: set system host-name check-test
-#   check_mode: yes
-#
-# - name: get host name
-#   vyos_command:
-#     commands: show host name
-#   register: result
-#
-# - assert:
-#     that: '"check-test" not in result.stdout'
+- name: configure simple config command
+  vyos_config:
+    lines: set system host-name check-test
+  check_mode: yes
+
+- name: get host name
+  vyos_command:
+    commands: show host name
+  register: result
+
+- assert:
+    that: '"check-test" not in result.stdout'