Repository: ansible/ansible

Issue:
[WIP] Make network plugins PY3 complaint
<!---

-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report


##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
asa
dellos6
dellos9
dellos10
eos
ios
iosxr
junos
nxos
sros
vyos

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
2.4
```

##### CONFIGURATION
<!---
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.
-->

##### SUMMARY
<!--- Explain the problem briefly -->
Issue to track py3 compliance for network modules
##### 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 -->
```

```



Discussion:
@ganeshrn Let me know if you need any help in this ? I would like to help here.

Resolving patch:
diff --git a/lib/ansible/module_utils/vyos.py b/lib/ansible/module_utils/vyos.py
--- a/lib/ansible/module_utils/vyos.py
+++ b/lib/ansible/module_utils/vyos.py
@@ -25,6 +25,7 @@
 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #
+from ansible.module_utils._text import to_text
 from ansible.module_utils.basic import env_fallback, return_values
 from ansible.module_utils.network_common import to_list
 from ansible.module_utils.connection import exec_command
@@ -43,18 +44,19 @@
     'provider': dict(type='dict'),
 }
 
+
 def check_args(module, warnings):
     provider = module.params['provider'] or {}
     for key in vyos_argument_spec:
         if key != 'provider' and module.params[key]:
-            warnings.append('argument %s has been deprecated and will be '
-                    'removed in a future version' % key)
+            warnings.append('argument %s has been deprecated and will be removed in a future version' % key)
 
     if provider:
         for param in ('password',):
             if provider.get(param):
                 module.no_log_values.update(return_values(provider[param]))
 
+
 def get_config(module, target='commands'):
     cmd = ' '.join(['show configuration', target])
 
@@ -63,24 +65,26 @@ def get_config(module, target='commands'):
     except KeyError:
         rc, out, err = exec_command(module, cmd)
         if rc != 0:
-            module.fail_json(msg='unable to retrieve current config', stderr=err)
-        cfg = str(out).strip()
+            module.fail_json(msg='unable to retrieve current config', stderr=to_text(err, errors='surrogate_or_strict'))
+        cfg = to_text(out, errors='surrogate_or_strict').strip()
         _DEVICE_CONFIGS[cmd] = cfg
         return cfg
 
+
 def run_commands(module, commands, check_rc=True):
     responses = list()
     for cmd in to_list(commands):
         rc, out, err = exec_command(module, cmd)
         if check_rc and rc != 0:
-            module.fail_json(msg=err, rc=rc)
-        responses.append(out)
+            module.fail_json(msg=to_text(err, errors='surrogate_or_strict'), rc=rc)
+        responses.append(to_text(out, errors='surrogate_or_strict'))
     return responses
 
+
 def load_config(module, commands, commit=False, comment=None):
     rc, out, err = exec_command(module, 'configure')
     if rc != 0:
-        module.fail_json(msg='unable to enter configuration mode', output=err)
+        module.fail_json(msg='unable to enter configuration mode', output=to_text(err, errors='surrogate_or_strict'))
 
     for cmd in to_list(commands):
         rc, out, err = exec_command(module, cmd)
@@ -92,9 +96,10 @@ def load_config(module, commands, commit=False, comment=None):
     diff = None
     if module._diff:
         rc, out, err = exec_command(module, 'compare')
+        out = to_text(out, errors='surrogate_or_strict')
         if not out.startswith('No changes'):
             rc, out, err = exec_command(module, 'show')
-            diff = str(out).strip()
+            diff = to_text(out, errors='surrogate_or_strict').strip()
 
     if commit:
         cmd = 'commit'
diff --git a/lib/ansible/plugins/terminal/vyos.py b/lib/ansible/plugins/terminal/vyos.py
--- a/lib/ansible/plugins/terminal/vyos.py
+++ b/lib/ansible/plugins/terminal/vyos.py
@@ -29,23 +29,22 @@
 class TerminalModule(TerminalBase):
 
     terminal_stdout_re = [
-        re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
-        re.compile(r"\@[\w\-\.]+:\S+?[>#\$] ?$")
+        re.compile(br"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
+        re.compile(br"\@[\w\-\.]+:\S+?[>#\$] ?$")
     ]
 
     terminal_stderr_re = [
-        re.compile(r"\n\s*Invalid command:"),
-        re.compile(r"\nCommit failed"),
-        re.compile(r"\n\s+Set failed"),
+        re.compile(br"\n\s*Invalid command:"),
+        re.compile(br"\nCommit failed"),
+        re.compile(br"\n\s+Set failed"),
     ]
 
     terminal_length = os.getenv('ANSIBLE_VYOS_TERMINAL_LENGTH', 10000)
 
     def on_open_shell(self):
         try:
-            for cmd in ['set terminal length 0', 'set terminal width 512']:
+            for cmd in (b'set terminal length 0', b'set terminal width 512'):
                 self._exec_cli_command(cmd)
-            self._exec_cli_command('set terminal length %s' % self.terminal_length)
+            self._exec_cli_command(b'set terminal length %s' % self.terminal_length)
         except AnsibleConnectionFailure:
             raise AnsibleConnectionFailure('unable to set terminal parameters')
-


Test patch:
diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt
--- a/test/sanity/pep8/legacy-files.txt
+++ b/test/sanity/pep8/legacy-files.txt
@@ -100,7 +100,6 @@ lib/ansible/module_utils/univention_umc.py
 lib/ansible/module_utils/urls.py
 lib/ansible/module_utils/vca.py
 lib/ansible/module_utils/vmware.py
-lib/ansible/module_utils/vyos.py
 lib/ansible/modules/cloud/amazon/_ec2_ami_search.py
 lib/ansible/modules/cloud/amazon/_ec2_vpc.py
 lib/ansible/modules/cloud/amazon/aws_kms.py
@@ -857,7 +856,6 @@ lib/ansible/plugins/terminal/ios.py
 lib/ansible/plugins/terminal/iosxr.py
 lib/ansible/plugins/terminal/junos.py
 lib/ansible/plugins/terminal/nxos.py
-lib/ansible/plugins/terminal/vyos.py
 lib/ansible/plugins/test/core.py
 lib/ansible/plugins/test/files.py
 lib/ansible/plugins/test/mathstuff.py