From 7dfd994a7e3c588bfab668b01860a0da09288db3 Mon Sep 17 00:00:00 2001 From: Varun Chopra Date: Wed, 25 Sep 2019 21:21:26 +0530 Subject: [PATCH] win_partition - Fix handling of maximum partition size (#58225) * Fix handling of maximum partition size * Added changelog fragment --- ...58225-win_partition-maximum-partition-size.yml | 2 ++ lib/ansible/modules/windows/win_partition.ps1 | 15 ++++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) create mode 100644 changelogs/fragments/58225-win_partition-maximum-partition-size.yml diff --git a/changelogs/fragments/58225-win_partition-maximum-partition-size.yml b/changelogs/fragments/58225-win_partition-maximum-partition-size.yml new file mode 100644 index 00000000000..22f82f7aba3 --- /dev/null +++ b/changelogs/fragments/58225-win_partition-maximum-partition-size.yml @@ -0,0 +1,2 @@ +bugfixes: + - win_partition - don't resize partitions if size difference is < 1 MiB diff --git a/lib/ansible/modules/windows/win_partition.ps1 b/lib/ansible/modules/windows/win_partition.ps1 index 0b47674d89f..e633829471c 100644 --- a/lib/ansible/modules/windows/win_partition.ps1 +++ b/lib/ansible/modules/windows/win_partition.ps1 @@ -148,13 +148,8 @@ function New-AnsiblePartition { } } - switch ($SizeMax) { - $True { - $parameters.Add("UseMaximumSize", $True) - } - $False { - $parameters.Add("Size", $Size) - } + if ($null -ne $Size) { + $parameters.Add("Size", $Size) } if ($null -ne $MbrType) { @@ -235,7 +230,7 @@ if ($ansible_partition) { if ($size_is_maximum) { $ansible_partition_size = $max_supported_size } - if ($ansible_partition_size -ne $ansible_partition.Size -and $ansible_partition_size -le $max_supported_size) { + if ($ansible_partition_size -ne $ansible_partition.Size -and ($ansible_partition_size - $ansible_partition.Size -gt 1049000 -or $ansible_partition.Size - $ansible_partition_size -gt 1049000)) { if ($ansible_partition.IsReadOnly) { $module.FailJson("Unable to resize partition: Partition is read only") } else { @@ -281,6 +276,8 @@ else { if ($ansible_partition_size -gt $max_supported_size) { $module.FailJson("Partition size is not supported by disk. Use partition_size: -1 to get maximum size") } + } else { + $ansible_partition_size = (Get-Disk -Number $disk_number).LargestFreeExtent } $supp_part_type = (Get-Disk -Number $disk_number).PartitionStyle @@ -300,7 +297,7 @@ else { } if (-not $module.CheckMode) { - $ansible_partition = New-AnsiblePartition -DiskNumber $disk_number -Letter $drive_letter -SizeMax $size_is_maximum -Size $ansible_partition_size -MbrType $mbr_type -GptType $gpt_type -Style $partition_style + $ansible_partition = New-AnsiblePartition -DiskNumber $disk_number -Letter $drive_letter -Size $ansible_partition_size -MbrType $mbr_type -GptType $gpt_type -Style $partition_style } $module.Result.changed = $true }