ansible-collections / community.windows

Windows community collection for Ansible
https://galaxy.ansible.com/community/windows
GNU General Public License v3.0
206 stars 160 forks source link

win_disk_facts fails when multiple physicaldisks with the same ID exist #165

Open jdkang opened 4 years ago

jdkang commented 4 years ago
SUMMARY

module win_disk_facts throws an error when multiple physical disks exists for a given DeviceID

Specifically, line 73 calling $vdisk = Get-VirtualDisk -PhysicalDisk $pdisk -ErrorAction SilentlyContinue wherein $pdisk is an object array rather than a single CIM instance.

Note that I can reproduce this by hand by logging into the server and executing the relevant commands/logic.

ISSUE TYPE
COMPONENT NAME

win_disk_facts.ps1

ANSIBLE VERSION

2.9.2

CONFIGURATION
INVENTORY_IGNORE_EXTS(env: ANSIBLE_INVENTORY_IGNORE) = ['.yml', '.yaml', '.retry']
OS / ENVIRONMENT

Windows 6.3.9600.0 (Server 2012)

STEPS TO REPRODUCE

Given the following setup:

PS C:\Users\xxx> Get-Disk | select number,friendlyname,size | Sort-Object numbernumber friendlyname                                  size
------ ------------                                  ----
     0 VMware Virtual disk SCSI Disk Device   64424509440
     1 VMware Virtual disk SCSI Disk Device   21474836480
     2 PURE FlashArray SCSI Disk Device     6839735418880
PS C:\Users\xxx> Get-PhysicalDisk | select deviceid,friendlyname,size | Sort-Object deviceiddeviceid friendlyname            size
-------- ------------            ----
0        PhysicalDisk0    64424509440
1        PhysicalDisk1    21474836480
1        PhysicalDisk1    21474836480
1        PhysicalDisk1    21474836480
1        PhysicalDisk1    21474836480
1        PhysicalDisk1  7939247046656
2        PhysicalDisk2  6839735418880
2        PhysicalDisk2  5905580032000
2        PhysicalDisk2 14512694493184
2        PhysicalDisk2  8796093022208
2        PhysicalDisk2  8796093022208
3        PhysicalDisk3  7881264988160
4        PhysicalDisk4    21474836480

The problem is specifically that the filter logic picks up multiple physical disks with the same DeviceID

EXPECTED RESULTS

Not to error out.

ACTUAL RESULTS

Throws an exception

fatal: [HOSTNAME]: FAILED! => {"changed": false, "msg": "Unhandled exception while executing module: Cannot process argument transformation on parameter 'PhysicalDisk'. Cannot convert the \\"System.Object[]\\" value of type \\"System.Object[]\\" to type \\"Microsoft.Management.Infrastructure.CimInstance\\"."}
janhonenj commented 3 years ago

The code:

41    $pdisk = Get-PhysicalDisk -ErrorAction SilentlyContinue | Where-Object {
42        $_.DeviceId -eq $disk.Number

The filter also picks up multiple disks when there are disks without a number and physical disks without a DeviceID

PS > Get-Disk

Number Friendly Name Serial Number HealthStatus OperationalStatus Total Size Partition Style


0 Virtual HD Healthy Online 61 GB MBR 1 Virtual HD Healthy Online 7 GB MBR 3 Msft Virtual Disk Healthy Online 256 GB RAW 2 Msft Virtual Disk Healthy Online 15 GB GPT 4 Msft Virtual Disk Healthy Online 256 GB RAW Msft Virtual Disk Healthy Online 15 GB GPT

PS > Get-PhysicalDisk | ft -auto DeviceId, FriendlyName, Size

DeviceId FriendlyName Size


2 Msft Virtual Disk 16106127360 0 Virtual HD 65498251264 1 Virtual HD 7516192768 Msft Virtual Disk 274877906944 Msft Virtual Disk 274877906944