Repository: ansible/ansible
Pull request: properly convert inputs to handle bytes/unicode
  fixes #52186

##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
- Bugfix Pull Request

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below -->
inventory/nmap

Problem statement:
nmap inventory plugin python3 cannot use a string pattern on a bytes-like object
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
When trying to use nmap dynamic inventory plugin, it fails with "cannot use a string pattern on a bytes-like object".

The stdout returned from `stdout, stderr = p.communicate()` needs to be decoded to string so regex matching can be applied.  

Quick workaround on my system was adding `stdout = stdout.decode("utf-8")`.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
nmap

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
ansible 2.7.7
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.7.2 (default, Jan 10 2019, 23:51:51) [GCC 8.2.1 20181127]

```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
Relevant changes to configuration
```paste below
[inventory]
enable_plugins = yaml, nmap
```
##### STEPS TO REPRODUCE
nmap inventory file
```yaml
plugin: nmap
address: "192.168.10.0/24"
```
Command: `ansible-inventory -i nmap_inventory.yaml --list -vvv`

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->
Inventory list

##### ACTUAL RESULTS
```paste below
  File "/usr/lib/python3.7/site-packages/ansible/plugins/inventory/nmap.py", line 130, in parse
    hits = self.find_host.match(line)
```

Hints from issue discussion:
Files identified in the description:
* [lib/ansible/plugins/inventory/nmap.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/inventory/nmap.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 --->

Commit message: properly convert inputs to handle bytes/unicode

  fixes #52186

Patch to review:
diff --git a/changelogs/fragments/nmap_bytes_fix.yml b/changelogs/fragments/nmap_bytes_fix.yml
new file mode 100644
index 00000000000000..591cf24c703a8a
--- /dev/null
+++ b/changelogs/fragments/nmap_bytes_fix.yml
@@ -0,0 +1,2 @@
+bugfixes:
+    - convert input into text to ensure valid comparissons in nmap inventory plugin
diff --git a/lib/ansible/plugins/inventory/nmap.py b/lib/ansible/plugins/inventory/nmap.py
index f9e709ee64498e..72f2a140b47da2 100644
--- a/lib/ansible/plugins/inventory/nmap.py
+++ b/lib/ansible/plugins/inventory/nmap.py
@@ -57,7 +57,7 @@
 
 from ansible import constants as C
 from ansible.errors import AnsibleParserError
-from ansible.module_utils._text import to_native
+from ansible.module_utils._text import to_native, to_text
 from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
 
 
@@ -127,6 +127,7 @@ def parse(self, inventory, loader, path, cache=False):
             ip = None
             ports = []
             for line in stdout.splitlines():
+                line = to_text(line)
                 hits = self.find_host.match(line)
                 if hits:
                     if host is not None:


Code review comments:
diff_hunk: @@ -0,0 +1,2 @@
+bugfixes:
+    - convert input into text to ensure valid comparissons in nmap inventory plugin
path: changelogs/fragments/nmap_bytes_fix.yml
text: @user1:
```suggestion
    - convert input into text to ensure valid comparisons in nmap inventory plugin
```