1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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
This commit is contained in:
Dag Wieers 2018-01-26 10:46:53 +01:00 committed by Jordan Borean
parent 466e1b289b
commit 1c8a872648
2 changed files with 137 additions and 141 deletions

View file

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

View file

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