win_disk_facts: Prefix facts with ansible_ and use raw values (#35326)

* win_disk_facts: Prefix facts with ansible_

* Return raw values in bytes rather than formatted strings

* Fix Fail-Json message

* Improve examples
pull/35386/head
Dag Wieers 7 years ago committed by Jordan Borean
parent 466e1b289b
commit 1c8a872648

@ -8,28 +8,25 @@
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
Set-StrictMode -Version 2.0 Set-StrictMode -Version 2.0
# Create a new result object
$result = @{
changed = $false
ansible_facts = @{
total_disks = 0
disks = @()
}
}
# Functions # Functions
function Test-Admin { function Test-Admin {
$CurrentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) $CurrentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$IsAdmin = $CurrentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) $IsAdmin = $CurrentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
return $IsAdmin return $IsAdmin
} }
# Check admin rights # Check admin rights
if (-not (Test-Admin)) { if (-not (Test-Admin)) {
$result.Remove("total_disks") Fail-Json -obj @{} -message "Module was not started with elevated rights"
$result.Remove("disks") }
Fail-Json -obj $result -message "Cmdlet was not started with elevated rights"
# Create a new result object
$result = @{
changed = $false
ansible_facts = @{
ansible_disks = @()
}
} }
# Search disks # Search disks
@ -39,7 +36,6 @@ try {
Fail-Json -obj $result -message "Failed to search the disks on the target: $($_.Exception.Message)" Fail-Json -obj $result -message "Failed to search the disks on the target: $($_.Exception.Message)"
} }
[int32]$diskcount = $disks | Measure-Object | Select-Object -ExpandProperty Count [int32]$diskcount = $disks | Measure-Object | Select-Object -ExpandProperty Count
$result.ansible_facts.total_disks = $diskcount
foreach ($disk in $disks) { foreach ($disk in $disks) {
$disk_info = @{} $disk_info = @{}
$pdisk = Get-PhysicalDisk -ErrorAction SilentlyContinue | Where-Object { $pdisk = Get-PhysicalDisk -ErrorAction SilentlyContinue | Where-Object {
@ -47,8 +43,8 @@ foreach ($disk in $disks) {
} }
if ($pdisk) { if ($pdisk) {
$disk_info["physical_disk"] += @{ $disk_info["physical_disk"] += @{
size = "$($pSize = "{0:N3}" -f ($pdisk.Size / 1GB))$($pSize)GiB" size = $pdisk.Size
allocated_size = "$($pAllocSize = "{0:N3}" -f ($pdisk.AllocatedSize / 1GB))$($pAllocSize)GiB" allocated_size = $pdisk.AllocatedSize
device_id = $pdisk.DeviceId device_id = $pdisk.DeviceId
friendly_name = $pdisk.FriendlyName friendly_name = $pdisk.FriendlyName
operational_status = $pdisk.OperationalStatus operational_status = $pdisk.OperationalStatus
@ -56,7 +52,7 @@ foreach ($disk in $disks) {
bus_type = $pdisk.BusType bus_type = $pdisk.BusType
usage_type = $pdisk.Usage usage_type = $pdisk.Usage
supported_usages = $pdisk.SupportedUsages supported_usages = $pdisk.SupportedUsages
spindle_speed = "$($pdisk.SpindleSpeed)rpm" spindle_speed = $pdisk.SpindleSpeed
firmware_version = $pdisk.FirmwareVersion firmware_version = $pdisk.FirmwareVersion
physical_location = $pdisk.PhysicalLocation physical_location = $pdisk.PhysicalLocation
manufacturer = $pdisk.Manufacturer manufacturer = $pdisk.Manufacturer
@ -77,29 +73,29 @@ foreach ($disk in $disks) {
$vdisk = Get-VirtualDisk -PhysicalDisk $pdisk -ErrorAction SilentlyContinue $vdisk = Get-VirtualDisk -PhysicalDisk $pdisk -ErrorAction SilentlyContinue
if ($vdisk) { if ($vdisk) {
$disk_info["virtual_disk"] += @{ $disk_info["virtual_disk"] += @{
size = "$($vDSize = "{0:N3}" -f ($vdisk.Size / 1GB))$($vDSize)GiB" size = $vdisk.Size
allocated_size = "$($vDAllocSize = "{0:N3}" -f ($vdisk.AllocatedSize / 1GB))$($vDAllocSize)GiB" allocated_size = $vdisk.AllocatedSize
footprint_on_pool = "$($vDPrint = "{0:N3}" -f ($vdisk.FootprintOnPool / 1GB))$($vDPrint)GiB" footprint_on_pool = $vdisk.FootprintOnPool
name = $vdisk.name name = $vdisk.name
friendly_name = $vdisk.FriendlyName friendly_name = $vdisk.FriendlyName
operational_status = $vdisk.OperationalStatus operational_status = $vdisk.OperationalStatus
health_status = $vdisk.HealthStatus health_status = $vdisk.HealthStatus
provisioning_type = $vdisk.ProvisioningType provisioning_type = $vdisk.ProvisioningType
allocation_unit_size = "$($vdisk.AllocationUnitSize / 1KB)KiB" allocation_unit_size = $vdisk.AllocationUnitSize
media_type = $vdisk.MediaType media_type = $vdisk.MediaType
parity_layout = $vdisk.ParityLayout parity_layout = $vdisk.ParityLayout
access = $vdisk.Access access = $vdisk.Access
detached_reason = $vdisk.DetachedReason detached_reason = $vdisk.DetachedReason
write_cache_size = "$($vdisk.WriteCacheSize)s/byte/bytes/" write_cache_size = $vdisk.WriteCacheSize
fault_domain_awareness = $vdisk.FaultDomainAwareness fault_domain_awareness = $vdisk.FaultDomainAwareness
inter_leave = "$($vDLeave = "{0:N3}" -f ($vdisk.InterLeave / 1KB))$($vDLeave)KiB" inter_leave = $vdisk.InterLeave
deduplication_enabled = $vdisk.IsDeduplicationEnabled deduplication_enabled = $vdisk.IsDeduplicationEnabled
enclosure_aware = $vdisk.IsEnclosureAware enclosure_aware = $vdisk.IsEnclosureAware
manual_attach = $vdisk.IsManualAttach manual_attach = $vdisk.IsManualAttach
snapshot = $vdisk.IsSnapshot snapshot = $vdisk.IsSnapshot
tiered = $vdisk.IsTiered tiered = $vdisk.IsTiered
physical_sector_size = "$($vdisk.PhysicalSectorSize / 1KB)KiB" physical_sector_size = $vdisk.PhysicalSectorSize
logical_sector_size = "$($vdisk.LogicalSectorSize)s/byte/bytes/" logical_sector_size = $vdisk.LogicalSectorSize
available_copies = $vdisk.NumberOfAvailableCopies available_copies = $vdisk.NumberOfAvailableCopies
columns = $vdisk.NumberOfColumns columns = $vdisk.NumberOfColumns
groups = $vdisk.NumberOfGroups groups = $vdisk.NumberOfGroups
@ -114,13 +110,13 @@ foreach ($disk in $disks) {
} }
} }
$disk_info.number = $disk.Number $disk_info.number = $disk.Number
$disk_info.size = "$($disk.Size / 1GB)GiB" $disk_info.size = $disk.Size
$disk_info.bus_type = $disk.BusType $disk_info.bus_type = $disk.BusType
$disk_info.friendly_name = $disk.FriendlyName $disk_info.friendly_name = $disk.FriendlyName
$disk_info.partition_style = $disk.PartitionStyle $disk_info.partition_style = $disk.PartitionStyle
$disk_info.partition_count = $disk.NumberOfPartitions $disk_info.partition_count = $disk.NumberOfPartitions
$disk_info.operational_status = $disk.OperationalStatus $disk_info.operational_status = $disk.OperationalStatus
$disk_info.sector_size = "$($disk.PhysicalSectorSize)s/byte/bytes/" $disk_info.sector_size = $disk.PhysicalSectorSize
$disk_info.read_only = $disk.IsReadOnly $disk_info.read_only = $disk.IsReadOnly
$disk_info.bootable = $disk.IsBoot $disk_info.bootable = $disk.IsBoot
$disk_info.system_disk = $disk.IsSystem $disk_info.system_disk = $disk.IsSystem
@ -139,7 +135,7 @@ foreach ($disk in $disks) {
foreach ($part in $parts) { foreach ($part in $parts) {
$partition_info = @{ $partition_info = @{
number = $part.PartitionNumber number = $part.PartitionNumber
size = "$($pSize = "{0:N3}" -f ($part.Size /1GB))$($pSize)GiB" size = $part.Size
type = $part.Type type = $part.Type
drive_letter = $part.DriveLetter drive_letter = $part.DriveLetter
transition_state = $part.TransitionState transition_state = $part.TransitionState
@ -161,8 +157,8 @@ foreach ($disk in $disks) {
$partition_info["volumes"] += @() $partition_info["volumes"] += @()
foreach ($vol in $vols) { foreach ($vol in $vols) {
$volume_info = @{ $volume_info = @{
size = "$($vSize = "{0:N3}" -f ($vol.Size / 1GB))$($vSize)GiB" size = $vol.Size
size_remaining = "$($vSizeRe = "{0:N3}" -f ($vol.SizeRemaining / 1GB))$($vSizeRe)GiB" size_remaining = $vol.SizeRemaining
type = $vol.FileSystem type = $vol.FileSystem
label = $vol.FileSystemLabel label = $vol.FileSystemLabel
health_status = $vol.HealthStatus health_status = $vol.HealthStatus
@ -171,11 +167,11 @@ foreach ($disk in $disks) {
path = $vol.Path path = $vol.Path
} }
if ([System.Environment]::OSVersion.Version.Major -ge 10) { if ([System.Environment]::OSVersion.Version.Major -ge 10) {
$volume_info.allocation_unit_size = "$($vol.AllocationUnitSize /1KB)KiB" $volume_info.allocation_unit_size = $vol.AllocationUnitSize
} else { } else {
$volPath = ($vol.Path.TrimStart("\\?\")).TrimEnd("\") $volPath = ($vol.Path.TrimStart("\\?\")).TrimEnd("\")
$BlockSize = (Get-CimInstance -Query "SELECT BlockSize FROM Win32_Volume WHERE DeviceID like '%$volPath%'" -ErrorAction SilentlyContinue | Select-Object BlockSize).BlockSize $BlockSize = (Get-CimInstance -Query "SELECT BlockSize FROM Win32_Volume WHERE DeviceID like '%$volPath%'" -ErrorAction SilentlyContinue | Select-Object BlockSize).BlockSize
$volume_info.allocation_unit_size = "$($BlockSize / 1KB)KiB" $volume_info.allocation_unit_size = $BlockSize
} }
$partition_info.volumes += $volume_info $partition_info.volumes += $volume_info
} }
@ -183,7 +179,7 @@ foreach ($disk in $disks) {
$disk_info.partitions += $partition_info $disk_info.partitions += $partition_info
} }
} }
$result.ansible_facts.disks += $disk_info $result.ansible_facts.ansible_disks += $disk_info
} }
# Return result # Return result

@ -16,7 +16,7 @@ version_added: '2.5'
short_description: Show the attached disks and disk information of the target host short_description: Show the attached disks and disk information of the target host
description: description:
- With the module you can retrieve and output detailed information about the attached disks of the target and - With the module you can retrieve and output detailed information about the attached disks of the target and
it's volumes and partitions if existent. its volumes and partitions if existent.
requirements: requirements:
- Windows 8.1 / Windows 2012 (NT 6.2) - Windows 8.1 / Windows 2012 (NT 6.2)
author: author:
@ -27,15 +27,29 @@ notes:
''' '''
EXAMPLES = r''' EXAMPLES = r'''
- name: get disk facts - name: Get disk facts
win_disk_facts: win_disk_facts:
- name: output first disk size
- name: Output first disk size
debug: debug:
var: ansible_facts.disks[0].size var: ansible_facts.disks[0].size
- name: get disk facts - name: Convert first system disk into various formats
win_disk_facts: debug:
- name: output second disk serial number msg: '{{ disksize_gib }} vs {{ disksize_gib_human }}'
vars:
# Get first system disk
disk: '{{ ansible_facts.disks|selectattr("system_disk")|first }}'
# Show disk size in Gibibytes
disksize_gib_human: '{{ disk.size|filesizeformat(True) }}' # returns "223.6 GiB" (human readable)
disksize_gib: '{{ (disk.size/1024|pow(3))|round|int }} GiB' # returns "224 GiB" (value in GiB)
# Show disk size in Gigabytes
disksize_gb_human: '{{ disk.size|filesizeformat }}' # returns "240.1 GB" (human readable)
disksize_gb: '{{ (disk.size/1000|pow(3))|round|int }} GB' # returns "240 GB" (value in GB)
- name: Output second disk serial number
debug: debug:
var: ansible_facts.disks[0].serial_number var: ansible_facts.disks[0].serial_number
''' '''
@ -46,12 +60,7 @@ ansible_facts:
returned: always returned: always
type: complex type: complex
contains: contains:
total_disks: ansible_disks:
description: Count of found disks on the target.
returned: if disks were found
type: int
sample: 3
disks:
description: Detailed information about one particular disk. description: Detailed information about one particular disk.
returned: if disks were found returned: if disks were found
type: list type: list
@ -62,10 +71,10 @@ ansible_facts:
type: int type: int
sample: 0 sample: 0
size: size:
description: Size in Gibibyte of the particular disk. description: Size in bytes of the particular disk.
returned: always returned: always
type: string type: int
sample: "100GiB" sample: 227727638528
bus_type: bus_type:
description: Bus type of the particular disk. description: Bus type of the particular disk.
returned: always returned: always
@ -92,30 +101,30 @@ ansible_facts:
type: string type: string
sample: "Online" sample: "Online"
sector_size: sector_size:
description: Sector size in byte of the particular disk. description: Sector size in bytes of the particular disk.
returned: always returned: always
type: string type: int
sample: "512s/byte/bytes/" sample: 4096
read_only: read_only:
description: Read only status of the particular disk. description: Read only status of the particular disk.
returned: always returned: always
type: string type: bool
sample: "true" sample: True
bootable: bootable:
description: Information whether the particular disk is a bootable disk. description: Information whether the particular disk is a bootable disk.
returned: always returned: always
type: string type: bool
sample: "false" sample: False
system_disk: system_disk:
description: Information whether the particular disk is a system disk. description: Information whether the particular disk is a system disk.
returned: always returned: always
type: string type: bool
sample: "true" sample: True
clustered: clustered:
description: Information whether the particular disk is clustered (part of a failover cluster). description: Information whether the particular disk is clustered (part of a failover cluster).
returned: always returned: always
type: string type: bool
sample: "false" sample: False
manufacturer: manufacturer:
description: Manufacturer of the particular disk. description: Manufacturer of the particular disk.
returned: always returned: always
@ -168,11 +177,10 @@ ansible_facts:
sample: 1 sample: 1
size: size:
description: description:
- Size in Gibibyte of the particular partition. - Size in bytes of the particular partition.
- Accurate to three decimal places.
returned: always returned: always
type: string type: int
sample: "0.031GiB" sample: 838860800
type: type:
description: Type of the particular partition. description: Type of the particular partition.
returned: always returned: always
@ -186,8 +194,8 @@ ansible_facts:
no_default_driveletter: no_default_driveletter:
description: Information whether the particular partition has a default drive letter or not. description: Information whether the particular partition has a default drive letter or not.
returned: if partition_style property of the particular disk has value "GPT" returned: if partition_style property of the particular disk has value "GPT"
type: string type: bool
sample: "true" sample: True
mbr_type: mbr_type:
description: mbr type of the particular partition. description: mbr type of the particular partition.
returned: if partition_style property of the particular disk has value "MBR" returned: if partition_style property of the particular disk has value "MBR"
@ -196,8 +204,8 @@ ansible_facts:
active: active:
description: Information whether the particular partition is an active partition or not. description: Information whether the particular partition is an active partition or not.
returned: if partition_style property of the particular disk has value "MBR" returned: if partition_style property of the particular disk has value "MBR"
type: string type: bool
sample: "true" sample: True
drive_letter: drive_letter:
description: Drive letter of the particular partition. description: Drive letter of the particular partition.
returned: if existent returned: if existent
@ -216,13 +224,13 @@ ansible_facts:
hidden: hidden:
description: Information whether the particular partition is hidden or not. description: Information whether the particular partition is hidden or not.
returned: always returned: always
type: string type: bool
sample: "true" sample: True
shadow_copy: shadow_copy:
description: Information whether the particular partition is a shadow copy of another partition. description: Information whether the particular partition is a shadow copy of another partition.
returned: always returned: always
type: string type: bool
sample: "false" sample: False
guid: guid:
description: GUID of the particular partition. description: GUID of the particular partition.
returned: if existent returned: if existent
@ -240,18 +248,16 @@ ansible_facts:
contains: contains:
size: size:
description: description:
- Size in Gibibyte of the particular volume. - Size in bytes of the particular volume.
- Accurate to three decimal places.
returned: always returned: always
type: string type: int
sample: "0,342GiB" sample: 838856704
size_remaining: size_remaining:
description: description:
- Remaining size in Gibibyte of the particular volume. - Remaining size in bytes of the particular volume.
- Accurate to three decimal places.
returned: always returned: always
type: string type: int
sample: "0,146GiB" sample: 395620352
type: type:
description: File system type of the particular volume. description: File system type of the particular volume.
returned: always returned: always
@ -273,10 +279,10 @@ ansible_facts:
type: string type: string
sample: "Fixed" sample: "Fixed"
allocation_unit_size: allocation_unit_size:
description: Allocation unit size in Kibibyte of the particular volume. description: Allocation unit size in bytes of the particular volume.
returned: always returned: always
type: string type: int
sample: "64KiB" sample: 4096
object_id: object_id:
description: Object ID of the particular volume. description: Object ID of the particular volume.
returned: always returned: always
@ -299,18 +305,16 @@ ansible_facts:
sample: "UnSpecified" sample: "UnSpecified"
size: size:
description: description:
- Size in Gibibyte of the particular physical disk. - Size in bytes of the particular physical disk.
- Accurate to three decimal places.
returned: always returned: always
type: string type: int
sample: "200GiB" sample: 240057409536
allocated_size: allocated_size:
description: description:
- Allocated size in Gibibyte of the particular physical disk. - Allocated size in bytes of the particular physical disk.
- Accurate to three decimal places.
returned: always returned: always
type: string type: int
sample: "100GiB" sample: 240057409536
device_id: device_id:
description: Device ID of the particular physical disk. description: Device ID of the particular physical disk.
returned: always returned: always
@ -359,8 +363,8 @@ ansible_facts:
spindle_speed: spindle_speed:
description: Spindle speed in rpm of the particular physical disk. description: Spindle speed in rpm of the particular physical disk.
returned: always returned: always
type: string type: int
sample: "4294967295rpm" sample: 4294967295
physical_location: physical_location:
description: Physical location of the particular physical disk. description: Physical location of the particular physical disk.
returned: always returned: always
@ -379,8 +383,8 @@ ansible_facts:
can_pool: can_pool:
description: Information whether the particular physical disk can be added to a storage pool. description: Information whether the particular physical disk can be added to a storage pool.
returned: always returned: always
type: string type: bool
sample: "false" sample: False
cannot_pool_reason: cannot_pool_reason:
description: Information why the particular physical disk can not be added to a storage pool. description: Information why the particular physical disk can not be added to a storage pool.
returned: if can_pool property has value false returned: if can_pool property has value false
@ -389,13 +393,13 @@ ansible_facts:
indication_enabled: indication_enabled:
description: Information whether indication is enabled for the particular physical disk. description: Information whether indication is enabled for the particular physical disk.
returned: always returned: always
type: string type: bool
sample: "True" sample: True
partial: partial:
description: Information whether the particular physical disk is partial. description: Information whether the particular physical disk is partial.
returned: always returned: always
type: string type: bool
sample: "False" sample: False
serial_number: serial_number:
description: Serial number of the particular physical disk. description: Serial number of the particular physical disk.
returned: always returned: always
@ -418,25 +422,22 @@ ansible_facts:
contains: contains:
size: size:
description: description:
- Size in Gibibyte of the particular virtual disk. - Size in bytes of the particular virtual disk.
- Accurate to three decimal places.
returned: always returned: always
type: string type: int
sample: "300GiB" sample: 240057409536
allocated_size: allocated_size:
description: description:
- Allocated size in Gibibyte of the particular virtual disk. - Allocated size in bytes of the particular virtual disk.
- Accurate to three decimal places.
returned: always returned: always
type: string type: int
sample: "100GiB" sample: 240057409536
footprint_on_pool: footprint_on_pool:
description: description:
- Footprint on pool in Gibibyte of the particular virtual disk. - Footprint on pool in bytes of the particular virtual disk.
- Accurate to three decimal places.
returned: always returned: always
type: string type: int
sample: "100GiB" sample: 240057409536
name: name:
description: Name of the particular virtual disk. description: Name of the particular virtual disk.
returned: always returned: always
@ -463,10 +464,10 @@ ansible_facts:
type: string type: string
sample: "Thin" sample: "Thin"
allocation_unit_size: allocation_unit_size:
description: Allocation unit size in Kibibyte of the particular virtual disk. description: Allocation unit size in bytes of the particular virtual disk.
returned: always returned: always
type: string type: int
sample: "4KiB" sample: 4096
media_type: media_type:
description: Media type of the particular virtual disk. description: Media type of the particular virtual disk.
returned: always returned: always
@ -490,8 +491,8 @@ ansible_facts:
write_cache_size: write_cache_size:
description: Write cache size in byte of the particular virtual disk. description: Write cache size in byte of the particular virtual disk.
returned: always returned: always
type: string type: int
sample: "100s/byte/bytes/" sample: 100
fault_domain_awareness: fault_domain_awareness:
description: Fault domain awareness of the particular virtual disk. description: Fault domain awareness of the particular virtual disk.
returned: always returned: always
@ -499,46 +500,45 @@ ansible_facts:
sample: "PhysicalDisk" sample: "PhysicalDisk"
inter_leave: inter_leave:
description: description:
- Inter leave in Kibibyte of the particular virtual disk. - Inter leave in bytes of the particular virtual disk.
- Accurate to three decimal places.
returned: always returned: always
type: string type: int
sample: "100KiB" sample: 102400
deduplication_enabled: deduplication_enabled:
description: Information whether deduplication is enabled for the particular virtual disk. description: Information whether deduplication is enabled for the particular virtual disk.
returned: always returned: always
type: string type: bool
sample: "True" sample: True
enclosure_aware: enclosure_aware:
description: Information whether the particular virtual disk is enclosure aware. description: Information whether the particular virtual disk is enclosure aware.
returned: always returned: always
type: string type: bool
sample: "False" sample: False
manual_attach: manual_attach:
description: Information whether the particular virtual disk is manual attached. description: Information whether the particular virtual disk is manual attached.
returned: always returned: always
type: string type: bool
sample: "True" sample: True
snapshot: snapshot:
description: Information whether the particular virtual disk is a snapshot. description: Information whether the particular virtual disk is a snapshot.
returned: always returned: always
type: string type: bool
sample: "False" sample: False
tiered: tiered:
description: Information whether the particular virtual disk is tiered. description: Information whether the particular virtual disk is tiered.
returned: always returned: always
type: string type: bool
sample: "True" sample: True
physical_sector_size: physical_sector_size:
description: Physical sector size in Kibibyte of the particular virtual disk. description: Physical sector size in bytes of the particular virtual disk.
returned: always returned: always
type: string type: int
sample: "4KiB" sample: 4096
logical_sector_size: logical_sector_size:
description: Logical sector size in byte of the particular virtual disk. description: Logical sector size in byte of the particular virtual disk.
returned: always returned: always
type: string type: int
sample: "512s/byte/bytes/" sample: 512
available_copies: available_copies:
description: Number of the available copies of the particular virtual disk. description: Number of the available copies of the particular virtual disk.
returned: if existent returned: if existent
@ -567,8 +567,8 @@ ansible_facts:
request_no_spof: request_no_spof:
description: Information whether the particular virtual disk requests no single point of failure. description: Information whether the particular virtual disk requests no single point of failure.
returned: always returned: always
type: string type: bool
sample: "True" sample: True
resiliency_setting_name: resiliency_setting_name:
description: Type of the physical disk redundancy of the particular virtual disk. description: Type of the physical disk redundancy of the particular virtual disk.
returned: always returned: always

Loading…
Cancel
Save