Repository: ansible/ansible

Issue:
get_prompt fails for network platforms using cliconf plugin
<!---
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
<!---
mlnxos_command
vyos_command
-->

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
2.5
```

##### 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.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
   "changed": false,
    "module_stderr": "Traceback (most recent call last):\n  File \"/Users/gnalawad/targets/ansible_ChpCiX/ansible_module_vyos_command.py\", line 233, in <module>\n    main()\n  File \"/Users/gnalawad/targets/ansible_ChpCiX/ansible_module_vyos_command.py\", line 203, in main\n    responses = run_commands(module, commands)\n  File \"/Users/gnalawad/targets/ansible_ChpCiX/ansible_modlib.zip/ansible/module_utils/network/vyos/vyos.py\", line 104, in run_commands\n  File \"/Users/gnalawad/targets/ansible_ChpCiX/ansible_modlib.zip/ansible/module_utils/network/vyos/vyos.py\", line 70, in get_connection\n  File \"/Users/gnalawad/targets/ansible_ChpCiX/ansible_modlib.zip/ansible/module_utils/network/vyos/vyos.py\", line 84, in get_capabilities\n  File \"/Users/gnalawad/targets/ansible_ChpCiX/ansible_modlib.zip/ansible/module_utils/connection.py\", line 146, in __rpc__\nansible.module_utils.connection.ConnectionError: operation requires privilege escalation\n",
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/plugins/cliconf/__init__.py b/lib/ansible/plugins/cliconf/__init__.py
--- a/lib/ansible/plugins/cliconf/__init__.py
+++ b/lib/ansible/plugins/cliconf/__init__.py
@@ -25,7 +25,7 @@
 from functools import wraps
 
 from ansible.errors import AnsibleError, AnsibleConnectionFailure
-from ansible.module_utils._text import to_bytes
+from ansible.module_utils._text import to_bytes, to_text
 from ansible.module_utils.six import with_metaclass
 
 try:
@@ -45,8 +45,8 @@
 def enable_mode(func):
     @wraps(func)
     def wrapped(self, *args, **kwargs):
-        prompt = self.get_prompt()
-        if not str(prompt).strip().endswith('#'):
+        prompt = self._connection.get_prompt()
+        if not to_text(prompt, errors='surrogate_or_strict').strip().endswith('#'):
             raise AnsibleError('operation requires privilege escalation')
         return func(self, *args, **kwargs)
     return wrapped


Test patch: