Repository: ansible/ansible

Issue:
ansible_devices not populating partitions{} structure for NVMe devices
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and devel branch are affected too.
Always add information AFTER of these html comments.
-->

##### 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
-->
setup.py

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/dblack/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.14 (default, Mar 14 2018, 13:36:31) [GCC 7.3.1 20180303 (Red Hat 7.3.1-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.
-->
Fedora 27 & RHEL 7.4

##### SUMMARY
<!--- Explain the problem briefly -->
When running 'ansible host -m setup' or when using gather_facts in a playbook, the partitions{} substructure for NVMe block devices under ansible_devices is not populated.

##### 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.
-->
1. Have an NVMe device with at least one partition configured
2. run 'ansible localhost -m setup'
3. Review the data structure for the NVMe device under ansible_devices

<!--- Paste example playbooks or commands between quotes below -->
```yaml
ansible localhost -m setup
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
The partitions{} substructure should be populated with the partition information for the NVMe device

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
The partitions{} substructure for the NVMe device is empty

<!--- Paste verbatim command output between quotes below -->
```
$ ls /dev/nvme0n1*
/dev/nvme0n1  /dev/nvme0n1p1  /dev/nvme0n1p2  /dev/nvme0n1p3

$ lsblk -i /dev/nvme0n1
NAME                        MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
nvme0n1                     259:0    0 238.5G  0 disk  
|-nvme0n1p1                 259:1    0   200M  0 part  /boot/efi
|-nvme0n1p2                 259:2    0     1G  0 part  /boot
`-nvme0n1p3                 259:3    0 237.3G  0 part  
...

$ ansible localhost -m setup
...
        "ansible_devices": {
...
            "nvme0n1": {
                "holders": [], 
                "host": "Non-Volatile memory controller: Samsung Electronics Co Ltd NVMe SSD Controller SM961/PM961", 
                "links": {
                    "ids": [
                        "nvme-SAMSUNG_MZVLW256HEHP-000L7_S35ENX0J556726", 
                        "nvme-eui.002538b571b4b81a"
                    ], 
                    "labels": [], 
                    "masters": [], 
                    "uuids": []
                }, 
                "model": "SAMSUNG MZVLW256HEHP-000L7", 
                "partitions": {}, 
                "removable": "0", 
                "rotational": "0", 
                "sas_address": null, 
                "sas_device_handle": null, 
                "scheduler_mode": "none", 
                "sectors": "500118192", 
                "sectorsize": "512", 
                "size": "238.47 GB", 
                "support_discard": "512", 
                "vendor": null, 
                "virtual": 1
            },
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/system/setup.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/system/setup.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 @alikins 
I hit the same issue and can reproduce on master.
Let me check the code and offer a patch.

Resolving patch:
diff --git a/lib/ansible/module_utils/facts/hardware/linux.py b/lib/ansible/module_utils/facts/hardware/linux.py
--- a/lib/ansible/module_utils/facts/hardware/linux.py
+++ b/lib/ansible/module_utils/facts/hardware/linux.py
@@ -615,7 +615,7 @@ def get_device_facts(self):
 
             d['partitions'] = {}
             for folder in os.listdir(sysdir):
-                m = re.search("(" + diskname + r"\d+)", folder)
+                m = re.search("(" + diskname + r"[p]*\d+)", folder)
                 if m:
                     part = {}
                     partname = m.group(1)


Test patch: